Winston_Smith Posted August 17, 2008 Share Posted August 17, 2008 I seem to have a problem is isset, I've looked around a number of sites and from what I can tell it's correct. <?php //set Variables $town = $_POST['town']; $lname = $_POST['lname']; $full = $_POST['full']; //Check if box is checked, if true unset other variables if ( $full == true ) { unset ($town); unset ($lname); } //Check if $town and $lname are set, if true set new variables and unset old if (isset($town, $lname)) { $town2 = "$town"; $lname2 = "$lname"; unset ($town); unset ($lname); } ?> If I comment out the if (isset($town, $lname)) {... section the rest of the code on the page works fine. This is the section i'm setting the variables for. <?php elseif (isset($town2, $lname2)){ print <<<_HTML_ <center> <img src="ecfiber.jpg"> <table width="800" border="1"> <tr> <th scope="col">First Name</th> <th scope="col">Last Name</th> <th scope="col">E-911 Address</th> <th scope="col">Town</th> <th scope="col">E-Mail</th> </tr> _HTML_; $result = mysql_query("SELECT * FROM `ecfiber` WHERE `town` ='".$town2."' AND `lname` ='".$lname2."'"); while($row = mysql_fetch_array($result)){ print "<tr><td>"; print $row['fname']; print "</td><td>"; print $row['lname']; print "</td><td>"; print $row['address']; print "</td><td>"; print $row['town']; print "</td><td>"; print $row['mail']; print "</td></tr>"; } print "</table>"; print "<a href = \"index.html\"> Return To Main Index</a><br>"; print "<a href = \"listdb.html\">View Database</a></center>"; } ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 You have to separate each. <?php if(iseet($var1) && isset($var2)){ // do this }else { // fail, do this } ?> Quote Link to comment Share on other sites More sharing options...
Winston_Smith Posted August 17, 2008 Author Share Posted August 17, 2008 Ok, that works thank you. But It gives me another problem, it seems it always unsets the $town and $lname vars. I'm using a form using post to get my value's if nothing is entered into the fields they should be unset currect? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 No, if there's no value in the variable already, technically it won't exist. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 Ok, that works thank you. But It gives me another problem, it seems it always unsets the $town and $lname vars. I'm using a form using post to get my value's if nothing is entered into the fields they should be unset currect? Basically as mgall stated.. if nothing is entered, that $_POST variable doesn't exist. Hence, when you test if $_POST['whatever'] is set, it will fail (which is the whole point of testing using isset(). Also, looking at the first snippet of code, you can unset multiple variables in one fell swoop. unset ($town); unset ($lname); could be written as: unset ($town, $lname); Cheers, NRG Quote Link to comment Share on other sites More sharing options...
Winston_Smith Posted August 17, 2008 Author Share Posted August 17, 2008 The problem is that if I enter just the $town field in the form it prints nothing. But if a fill in both the $town and $lname fields it works great. From what I can tell the isset ($var && $var) should returns as false if I only fill in one field. Yet it seems that it is returning as true. I see the new reply also, but if either one was false the code after the if shouldn't run correct? If one of the vars was not set it should skip that and insted continue till it hits the if statement for whichever variable was set. But it seems to set the $town2 and $lname2 vars and then it jumps up to the elseif for that and prints the table with no data. Here's a little more of the code <?php //set Variables $town = $_POST['town']; $lname = $_POST['lname']; $full = $_POST['full']; //Check if box is checked, if true unset other variables if ( $full == true ) { unset ($town); unset ($lname); } //Check if $town and $lname are set, if true set new variables and unset old if (isset($town) && isset($lname)){ $town2 = "$town"; $lname2 = "$lname"; unset ($town); unset ($lname); } // if ( $town == true ) {... // elseif( $lname == true ){... // elseif( $full == true ){... // elseif (isset($town2) && isset($lname2)){... // else {... ?> That's the basic layout of the page. This is my form page <html> <head> <title>ECFiber Pre-Registration</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#CCCCCC"> <center><img src="ecfiber.jpg"></center> <form action="listdb.php" method="post"> <table width="800" border="0" align="center"> <tr><th scope="col">Search Database by Town</th></tr> <tr> <td><center><select name="town" style="background-color:Yellow;width:160px;"> <option value="">Select Town</option> <option value="Barnard">Barnard</option> <option value="Bethel">Bethel</option> <option value="Brookfield">Brookfield</option> <option value="Chelsea">Chelsea</option> <option value="Granville">Granville</option> <option value="Hancock">Hancock</option> <option value="Hartford">Hartford</option> <option value="Montpelier">Montpelier</option> <option value="Norwich">Norwich</option> <option value="Pomfret">Pomfret</option> <option value="Randolph">Randolph</option> <option value="Reading">Reading</option> <option value="Rochester">Rochester</option> <option value="Royalton">Royalton</option> <option value="Sharon">Sharon</option> <option value="Stockbridge">Stockbridge</option> <option value="Strafford">Strafford</option> <option value="Thetford">Thetford</option> <option value="Tunbridge">Tunbridge</option> <option value="Vershire">Vershire</option> <option value="West Windsor">West Windsor</option> <option value="Williamstown">Williamstown</option> <option value="Woodstock">Woodstock</option> </select></center></td> </tr> <tr><td><center>Search Database by Last Name</center></td></tr> <tr> <td><center><input name="lname" type="text"></center></td> </tr> <tr><td><center>Check to display all names in Database</center></td></tr> <tr><td><center><input name="full" type="checkbox"></center></td></tr> <tr><td><center><input type="submit" value="Submit Data"></center></td></tr> </table> </form> <center><a href = "index.html"> Return To Main Index</a></center> </body> </html> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 The problem is that if I enter just the $town field in the form it prints nothing. But if a fill in both the $town and $lname fields it works great. From what I can tell the isset ($var && $var) should returns as false if I only fill in one field. Yet it seems that it is returning as true. Well, isset ($var && $var) is an incorrect format.. it should be: if(isset($var1) && isset($var2)){ // do this, as both $var1 AND $var2 is set (in otherwords, they both have something entered into them)... } else { // do that, as either $var1 or $var2 (or both) don't have anything entered... } I see the new reply also, but if either one was false the code after the if shouldn't run correct? If one of the vars was not set it should skip that and insted continue till it hits the if statement for whichever variable was set. But it seems to set the $town2 and $lname2 vars and then it jumps up to the elseif for that and prints the table with no Well, again, if your isset isn't setup properly, then you'll have problems. Quote Link to comment Share on other sites More sharing options...
Winston_Smith Posted August 17, 2008 Author Share Posted August 17, 2008 I have it correct in the page, i just wrote it out wrong on the forum. if ( isset($town) && isset($lname)){ $town2 = "$town"; $lname2 = "$lname"; unset ($town, $lname); } Quote Link to comment Share on other sites More sharing options...
trq Posted August 17, 2008 Share Posted August 17, 2008 You can pass isset as many parameters as you wish. It will return false if just one of those parameters is not set. if (isset($town,$lname)) { Quote Link to comment Share on other sites More sharing options...
Winston_Smith Posted August 17, 2008 Author Share Posted August 17, 2008 You can pass isset as many parameters as you wish. It will return false if just one of those parameters is not set. if (isset($town,$lname)){ That's the way I had it coded out before, I changed it over to what you see from some advice I got in another post. I want it to do exactly what you are saying it does. If both variables are not valid to not unset the $town and $lname variables. <?php //set Variables $town = $_POST['town']; $lname = $_POST['lname']; $full = $_POST['full']; //Check if box is checked, if true unset other variables if ( $full == true ) { unset ($town); unset ($lname); } //Check if $town and $lname are set, if true set new variables and unset old if (isset($town,$lname)){ $town2 = "$town"; $lname2 = "$lname"; unset ($town, $lname); } That way I have the form setup is if you enter just a town it searches the database and displays all rows that have that town in them. If you enter just a last name it displays all rows with that last name. But if you enter both a town and a last name it displays only rows that have both. Also if you check the box it unsets both other vars if they are set and displays the whole database. When I enter just a town I get a blank table that prints from my last elseif statement that is supposed to display the town and last name. If I put in a town and a last name it works correctly, the check box also works correctly. The only thing I can think of is for some odd reason the code is running everytime. If I comment out if (isset($town,$lname)){ $town2 = "$town"; $lname2 = "$lname"; unset ($town, $lname); } I can enter just a town and get all rows, and also all last names if I enter only a last name. So It must be that that code is running everytime, when it should be false when I don't enter both a town and last name. Quote Link to comment Share on other sites More sharing options...
Winston_Smith Posted August 17, 2008 Author Share Posted August 17, 2008 I think I have it figured out, now I need to figure out how to do this. The php manual says that even if a var is empty such as $name = ""; it will still come back as true. So even though it's set to nothing it still is true. So how would I get around this? Can I use empty? But you can only check one var with it? Quote Link to comment Share on other sites More sharing options...
trq Posted August 17, 2008 Share Posted August 17, 2008 [Can I use empty? But you can only check one var with it? That is correct. You'll need to use multiple if() statments. Quote Link to comment Share on other sites More sharing options...
Winston_Smith Posted August 17, 2008 Author Share Posted August 17, 2008 I have it figured out now, this is how I ended up doing it. if ($town == true && $lname == true){ $town2 = "$town"; $lname2 = "$lname"; unset ($town, $lname); } else {} Quote Link to comment 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.