DarkPrince2005 Posted March 12, 2008 Share Posted March 12, 2008 I have two pages, one with a form that has a collection of checkboxes and textboxes to define the extent of a search, and the second page that depending on which checkboxes are checked executes a query, but i'm having trouble on how to modify the if statements to ignore other if statements. eg. I want the script to ignore the if statement for checkbox a when both checkbox a and b are checked. <html> <head> <SCRIPT LANGUAGE="JavaScript"><!-- function codename() { if(document.form.a1.checked) { document.form.a.disabled=false; document.form.a.focus(); } else { document.form.a.disabled=true; } if(document.form.b1.checked) { document.form.b.disabled=false; document.form.b.focus(); } else { document.form.b.disabled=true; }} //--> </SCRIPT> </head> <body><form name="form" method="post" action="1.php"> a<input type="checkbox" name="a1" value="a" onclick="codename()"><br/> <input type="text" disabled name="a" size="25"> b<input type="checkbox" name="b1" value="b" onclick="codename()"><br/> <input type="text" disabled name="b" size="25"> <input type="submit" value="submit" /> </form> <?php if (isset($_POST['a'])) { mysql_connect("localhost","root",""); mysql_select_db("a"); $sql=mysql_query ("select * from a where id like '$_POST[a]'"); while ($row = mysql_fetch_array($sql)) { echo "$row[id]<br>"; };}; if (isset($_POST['b'])) { mysql_connect("localhost","root",""); mysql_select_db("a"); $sql=mysql_query ("select * from a where id like '$_POST[b]'"); while ($row = mysql_fetch_array($sql)) { echo "$row[id]<br>"; };}; if (isset($_POST['a1']) && isset($_POST['b1'])) { mysql_connect("localhost","root",""); mysql_select_db("a"); $sql=mysql_query ("select * from a where id like '$_POST[a]' or id like '$_POST[b]'"); while ($row = mysql_fetch_array($sql)) { echo "$row[id]<br>"; };}; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95848-if-statements-problem/ Share on other sites More sharing options...
Psycho Posted March 12, 2008 Share Posted March 12, 2008 You might want to consider using formatting within your code (i.e. indenting) and comments to help make it easier to understand and follow the logic. <?php //At least one checkbox selected if (isset($_POST['a']) || isset($_POST['b'])) { //Connect to database mysql_connect("localhost","root",""); mysql_select_db("a"); if (isset($_POST['a1']) && isset($_POST['b1'])) { //Both checkboxes selected $query = "select * from a where id like '$_POST[a]' or id like '$_POST[b]'"; } else if (isset($_POST['a']) { //Only A checkbox selected $query = "select * from a where id like '$_POST[a]'"; } else { //Only B checkbox selected $query = "select * from a where id like '$_POST[b]'"; } //Query & Display the results $result = mysql_query ($query) or die (mysql_error()); while ($row = mysql_fetch_array($result)) { echo "$row[id]<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-490686 Share on other sites More sharing options...
DarkPrince2005 Posted March 14, 2008 Author Share Posted March 14, 2008 would something like this work? would it save the problem? <?php if (isset($_POST['a1']) && !isset($_POST['b'])) { mysql_connect("localhost","root",""); mysql_select_db("a"); $sql=mysql_query ("select * from a where id like '$_POST[a]'"); while ($row = mysql_fetch_array($sql)) { echo "$row[id]<br>"; };}; if (!isset($_POST['a1']) && isset($_POST['b'])) { mysql_connect("localhost","root",""); mysql_select_db("a"); $sql=mysql_query ("select * from a where id like '$_POST[b]'"); while ($row = mysql_fetch_array($sql)) { echo "$row[id]<br>"; };}; if (isset($_POST['a1']) && isset($_POST['b'])) { mysql_connect("localhost","root",""); mysql_select_db("a"); $sql=mysql_query ("select * from a where id like '$_POST[a1]' or id like '$_POST[b]'"); while ($row = mysql_fetch_array($sql)) { echo "$row[id]<br>"; };}; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-491955 Share on other sites More sharing options...
haku Posted March 14, 2008 Share Posted March 14, 2008 You were the one with the problem - did it solve it? Can you see what is going on easier? If not, then no, that wouldn't work. If so, then yes, it would work. Personally I prefer (and use) the method that mjdamato used. Quote Link to comment https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-491958 Share on other sites More sharing options...
DarkPrince2005 Posted March 14, 2008 Author Share Posted March 14, 2008 thanx, was actually just wondering, but it works Quote Link to comment https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-491973 Share on other sites More sharing options...
Psycho Posted March 14, 2008 Share Posted March 14, 2008 That code you posed may work, but it is inefficient and doesn't follow a logical flow. For instance you have several places where you have code to connect to the database. That is a waste of code. You just need to check if at least one checkbox is checked - and then connect to the db. That code also has the same problem with the code to display the results. By "copying and pasting" the same code to different places the code will be difficult to debug and will be error prone. For example, if you discovered an error in the display code it might get fixed in one place and not another. Quote Link to comment https://forums.phpfreaks.com/topic/95848-if-statements-problem/#findComment-492241 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.