mourningreign Posted November 19, 2006 Share Posted November 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/27748-checking-to-see-if-a-form-value-matches-number-of-certain-populated-fields/ Share on other sites More sharing options...
fert Posted November 19, 2006 Share Posted November 19, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/27748-checking-to-see-if-a-form-value-matches-number-of-certain-populated-fields/#findComment-126963 Share on other sites More sharing options...
mourningreign Posted November 19, 2006 Author Share Posted November 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/27748-checking-to-see-if-a-form-value-matches-number-of-certain-populated-fields/#findComment-126967 Share on other sites More sharing options...
fert Posted November 19, 2006 Share Posted November 19, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/27748-checking-to-see-if-a-form-value-matches-number-of-certain-populated-fields/#findComment-126978 Share on other sites More sharing options...
fert Posted November 19, 2006 Share Posted November 19, 2006 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;}elseif(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;}elseif(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());}elseif($_POST[menu_name]==3){ @mysql_query("QUERY",$cn) or die(mysql_error());}elseif($_POST[menu_name]==4){ @mysql_query("QUERY",$cn) or die(mysql_error());}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27748-checking-to-see-if-a-form-value-matches-number-of-certain-populated-fields/#findComment-126981 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.