geroido Posted August 17, 2008 Share Posted August 17, 2008 Hi I have a variable passing over from a previous page 'numseats'. If it's empty, I want the form returned to the user, if not do something else. The problem is when I first enter this page, it recognises that the variable is empty and displays the correct code. If I then leave the input box empty again and send the data to this page again, it thinks there's something in it and goes to the else statement. Can you see what the problem is? As long as the variable is empty, I want it to always do the first part of this code. If the user enters something, only then go to the 'else' part. if (empty($numseats)) { ?> <h3>Please enter the number of seats in your restaurant. numseats is empty</h3> <form method="post" name="seats" action="updateseats.php"> <TABLE> <TR><TD><H3>Update seating</H3></TD></TR> <TR><TD>Number of seats</TD></TR> <TR><TD><textarea rows="2" cols="20" name="numseats" value=<?echo $numseats?>> </textarea></TD></TR> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Save" /></TD> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Finish" /></TD></TR> </TABLE><? } else{ $query = ("select clientID, NumSeats FROM $table where clientID = '".$_SESSION['userid']."'"); $results = mysql_query($query, $link) or die("Error performing query"); if(mysql_num_rows($results) > 0){ while($row = mysql_fetch_object($results)){ //$query = mysql_query("update $table set NumSeats = $numseats where clientID = '".$_SESSION['userid']."'"); echo $row->clientID; echo ("<BR>"); echo $row->NumSeats; } } else{ //$query = mysql_query("insert into $table (clientID, NumSeats) VALUES ('" .$_SESSION['userid']. "', '".$numseats."')"); echo "no records here my friend"; } ?> <h3>Please enter the number of seats in your restaurant. Numseats is not empty</h3> <form method="post" name="seats" action="updateseats.php"> <TABLE> <TR><TD><H3>Update seating</H3></TD></TR> <TR><TD>Number of seats</TD></TR> <TR><TD><textarea rows="2" cols="20" name="numseats" value=<?echo $numseats?>> </textarea></TD></TR> <TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Save" /></TD> <TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Finish" /></TD></TR> </TABLE><?}?> Quote Link to comment https://forums.phpfreaks.com/topic/120057-empty-variable-check-not-working-properly/ Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2008 Share Posted August 17, 2008 Is $numseats from a GET or POST request as you should use $_GET['numseats'] or $_POST['numseats'] Also using the empty() function is not the best approach. If you are expecting the nomber of seats to be a numeric value: $numseats = $_GET['numseats']; if(!is_numeric($numseats)) { // back to the form header("Location:formpage.php"); exit; } else { //process $numseats } Quote Link to comment https://forums.phpfreaks.com/topic/120057-empty-variable-check-not-working-properly/#findComment-618463 Share on other sites More sharing options...
geroido Posted August 17, 2008 Author Share Posted August 17, 2008 Hi neil I've put that code in and it's staying in the right place if the variable is not numeric. However, if I enter a numeric value it still doesn't go to the else statement and is returning the empty form again as if it where not numeric. <? if(!is_numeric($numseats)) { ?> <h3>Please enter the number of seats in your restaurant. numseats is empty</h3> <form method="post" name="seats" action="updateseats.php"> <TABLE> <TR><TD><H3>Update seating</H3></TD></TR> <TR><TD>Number of seats</TD></TR> <TR><TD><textarea rows="2" cols="20" name="numseats" value=<?echo $numseats?>> </textarea></TD></TR> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Save" /></TD> <TD><BR><input id="inputsubmit1" type="submit" name="savemenu" value="Finish" /></TD></TR> </TABLE><? } else{ $query = ("select clientID, NumSeats FROM $table where clientID = '".$_SESSION['userid']."'"); $results = mysql_query($query, $link) or die("Error performing query"); if(mysql_num_rows($results) > 0){ while($row = mysql_fetch_object($results)){ //$query = mysql_query("update $table set NumSeats = $numseats where clientID = '".$_SESSION['userid']."'"); echo $row->clientID; echo ("<BR>"); echo $row->NumSeats; } } else{ //$query = mysql_query("insert into $table (clientID, NumSeats) VALUES ('" .$_SESSION['userid']. "', '".$numseats."')"); echo "no records here my friend"; } ?> <h3>Please enter the number of seats in your restaurant. Numseats is not empty</h3> <form method="post" name="seats" action="updateseats.php"> <TABLE> <TR><TD><H3>Update seating</H3></TD></TR> <TR><TD>Number of seats</TD></TR> <TR><TD><textarea rows="2" cols="20" name="numseats" value=<?echo $numseats?>> </textarea></TD></TR> <TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Save" /></TD> <TD><BR><input id="inputsubmit1" type="submit" name="updateseats" value="Finish" /></TD></TR> </TABLE><?}?> Quote Link to comment https://forums.phpfreaks.com/topic/120057-empty-variable-check-not-working-properly/#findComment-618465 Share on other sites More sharing options...
JonnoTheDev Posted August 17, 2008 Share Posted August 17, 2008 Then the value isnt being passed to or read by the page page correctly. Is it a GET or POST value? Have you echoed it to the screen to test it exists? Quote Link to comment https://forums.phpfreaks.com/topic/120057-empty-variable-check-not-working-properly/#findComment-618527 Share on other sites More sharing options...
geroido Posted August 17, 2008 Author Share Posted August 17, 2008 Hi Neil The variable does indeed have a value. I think it's something to do with the opening and closing brackets of the if and else statement (},{). Can you see any problem with them. they always cause me problems. Quote Link to comment https://forums.phpfreaks.com/topic/120057-empty-variable-check-not-working-properly/#findComment-618550 Share on other sites More sharing options...
Winston_Smith Posted August 17, 2008 Share Posted August 17, 2008 Not sure if this will help, I'm fairly new to php. But I was playing around and this seemed to work for me. <?php $numseats = $_POST['numseats']; if (empty($numseats)) { ?> <h3>Please enter the number of seats in your restaurant. numseats is empty</h3> <form method="post" name="seats" action="seats.php"> <TABLE> <TR><TD><H3>Update seating</H3></TD></TR> <TR><TD>Number of seats</TD></TR> <TR><TD><input name="numseats" type="text" value="<? print "$numseats"; ?>" > </textarea></TD></TR> <TD><BR><input type="submit" value="Save" /></TD> <TD><BR></TD></TR> </TABLE> <? } else { print "error";} ?> On first load it loads the table, if I resubmit without entering it also loads the table and if I enter a value in to the field it goes to the error. I'd switch to a drop down list of numbers though, also I don't understand the point of printing the value of numseats in the text area. It's going to be false and print nothing everytime, If it evaluates to true it jumps to the else section. Quote Link to comment https://forums.phpfreaks.com/topic/120057-empty-variable-check-not-working-properly/#findComment-618638 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.