Jump to content

Checking to see if a form value matches number of certain populated fields


mourningreign

Recommended Posts

Within a form I'd like to compare 2 values. The first, a value selected froma menu (1-4). The second, taken from counting how many of 4 specific fields contain data.

If the values match pass the data to the database, if not die and give an error.

This is probably simple for a pro but I don't have a clue.
[code]
$num=$_POST[number];

for($count=0;$count<3;$count++)
{
if($_POST[$count]=="" && $count>$num || $count<$num)
{
die("The number of fields do not match");
}
}
[/code]
That takes care checking the fields if the menu name is num and the other fields are named 0-3
I really should be more specific when posting.

Purpose: On a request for a hotel room reservation the user selects how many people in a room.
The user has to fill in the full names of each additional person in the room. Rooms are only booked as doubles, triples or quads. So if name fields name1 & name2 are set to not null we need only to start at 2 and +1 for name3 and +1 for name4.

Could you also explain what each line does so that I can have a clue?

We could then compare the menu value to this calculation.

I'm thinking this is a pass through script situation using session variables to hold the data or maybe a calculation table in the database would be better.
[code]
$num=$_POST[number]; //make num equal to $_POST[number];

for($count=0;$count<3;$count++)//loop 4 times
{
if($_POST[$count]=="" && $count>$num || $count<$num) //check if the field is NULL and if the number is too big or too small
{
die("The number of fields do not match"); //die
}
}[/code]
mabey you should try using javascript and PHP it would be easier
Javascript
[code]
if(document.form_name.menu_name.value=='2')
{
document.form_name.text1.enabled=true;
document.form_name.text2.enabled=true;
document.form_name.text3.enabled=false;
document.form_name.text4.enabled=false;
}
else
if(document.form_name.menu_name.value=='3')
{
document.form_name.text1.enabled=true;
document.form_name.text2.enabled=true;
document.form_name.text3.enabled=true;
document.form_name.text4.enabled=false;
}
else
if(document.form_name.menu_name.value=='4')
{
document.form_name.text1.enabled=true;
document.form_name.text2.enabled=true;
document.form_name.text3.enabled=true;
document.form_name.text4.enabled=true;
}
[/code]

and the PHP code
[code]
$cn=@mysql_connect("host","password","username") or die(mysql_error());
if($_POST[menu_name]==2)
{
      @mysql_query("QUERY",$cn) or die(mysql_error());
}
else
if($_POST[menu_name]==3)
{
      @mysql_query("QUERY",$cn) or die(mysql_error());
}
else
if($_POST[menu_name]==4)
{
      @mysql_query("QUERY",$cn) or die(mysql_error());
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.