if (empty)....do - can't figure this code out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Madrid
Forum Newbie
Posts: 1
Joined: Sat Feb 01, 2003 10:11 am

if (empty)....do - can't figure this code out

Post by Jim_Madrid »

1) How do I write a simple code that says something to the effect that if form field $old is empty (i.e. no entered values) than values from form field $new in the same form should be sent to the database instead.

And, 2) I would put this code in the page that processes the form and not the in the form page itself, correct? Is there any special place it need to go on that page?

Many thanks
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
if(empty($_POST[old_field_name]))
{
$new_var = $_POST[new_field_name];
//rest of the code goes in here
}


?>
you don't have to put it in another page, just make a hidden form field and use this:

Code: Select all

<?php
if(isset($_POST[field_name]))
{
//..code here
}

?>
you can put it anwhere you like, i usellu put it at the top of the page.
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

Post by jarow »

Thanks alot...being very new to php I still don't understand this completely and have a few questions. Let's say I have the following:

text box form field name = "phylum"
select box form field name = "new_phylum"
database field name = "phylum"

According to the script you sent:
1. does old_field_name refer to the database field "phylum" or the text box form field of the same name?
2. does $new_var refer to my select box "new_phylum"
3. what does new_field_name refer to?
4. I am also not sure what remaining code is to be placed in //rest of the code goes in here.


<?php
if(empty($_POST[old_field_name]))
{
$new_var = $_POST[new_field_name];
//rest of the code goes in here
}

?>

I am also pretty much at a loss to understand what fields correspond to the hidden form field.

<?php
if(isset($_POST[field_name]))
{
//..code here
}

1. Does field name refer to the database field "phylum" in this case?
2. what code goes in the //....code here? the previous code from above?

As you can see I am very new....your patience and assistance is greatly appreciated, believe me

Thanks

Jim
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

Post by jarow »

This is a modification of my previous message. I believe my questions may be clearer here.

In essence this is what I have created. I have a text box in which researchers enter new phylum names into a database. Once those names are entered they appear in the new_phylum select box once the form is refreshed. What I would like to do is make it possible for the researchers to either select a name from the select box or type in a new name...one of the two will then be sent to the database.

text box form field name = "phylum"
select box form field name = "new_phylum"
database field name = "filo"
database table name= museum

According to the script you sent have I filled in the statement correctly with the names I am using?

<?php
$sql="";
if(empty($_POST['phylum']))
{
$new_phylum= $_POST['new_phylum'];
$sql="INSERT INTO museum (filo) values ('$new_phylum');
}

?>
Is this the correct place to put the INSERT INTO statement or should that go in the hidden field as a form variable for example

Regarding the hidden field:

1. What should the field_name be for my above example? Does it need to match one of my other field names?
2. Is this where I should put the INSERT INTO statement?

<?php
if(isset($_POST[field_name]))
{
//..code here
}

Again, your patience and assistance is greatly appreciated, believe me

Thanks

Jim
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

Just as a side note .. you can edit previous messages, you don't need to write a whole new message.
Post Reply