Jump to content

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


mourningreign

Recommended Posts

[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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.