Skittle Posted December 6, 2019 Share Posted December 6, 2019 I've been working on this html/php page for class and I just don't understand why my code is acting like this. I have this form that gets the user input and then it validates the code with appropriate error messages but even when I test it and type in something for each textbox it automatically makes the textbox go blank again and displays the error messages and i dont know why. This is my code <?php //error_reporting(E_ALL); //ini_set('display_errors', 1); $title = "Listing Search"; $file = "listing-search.php"; $description = "Listing Search page for real estate website (WEBD3201)"; $date = "2019-10-01"; $banner = "Listing Search page"; require "header.php"; ?> <?php $headline = ""; $minPrice = ""; $maxPrice = ""; $city = ""; $error = ""; $output = ""; $error = ""; $output = ""; if(isset($_GET["city"])) { $city = $_GET["city"]; setcookie('city',$city,COOKIE_LIFESPAN); $_SESSION['city'] = $city; } else if (isset($_COOKIE['city'])) { $city = $_COOKIE['city']; $_SESSION['city'] = $city; } if(!isset($city)) { $_SESSION['RedirectError'] = "Please select a city<br/>"; header("Location:listing-select-city.php"); } if(isPost()) { $minPrice = trim($_POST["minPrice"]); $output .=$_POST["minPrice"]; $maxPrice = trim($_POST["maxPrice"]); $output .=$_POST["maxPrice"]; $headline = trim($_POST["headline"]); $output .=$_POST["headline"]; $city = trim($_POST["city"]); $output .=$_POST["city"]; $error = ""; $output = ""; if($headline == "") { $error .= "<br/>Headline was not specified"; } if($city == "") { $error .= "<br/>City was not specified"; } if($minPrice == "") { $error .= "<br/>Minimum price was not specified"; } elseif (preg_match(PRICE_FILTER, $minPrice) == '0') { $error .= "<br/>Minimum price needs to be a number"; $minPrice = ""; } if($maxPrice == "") { $error .= "<br/>Maximum price was not specified"; } elseif (preg_match(PRICE_FILTER, $maxPrice) == '0') { $error .= "<br/>Maximum price needs to be a number"; $minPrice = ""; } if($maxPrice < $minPrice) { $error .= "<br/>Minimum price must be smaller than maximum price"; } } ?> <?php echo $error; ?> <?php echo $output;?> <h2>Search for a home.</h2> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table style="margin-left: auto; margin-right: auto;"> <tr> <td> Listing Name </td> <td> <input type="text" name="newlist" value="<?php echo $headline ?>" /> </td> <td> City </td> <td> <input type="text" name="city" value="<?php echo $city ?>" /> </td> </tr> <tr> <td> Min Price </td> <td> <input type="text" name="minPrice" value="<?php echo $minPrice ?>" /> </td> <td> Max Price </td> <td> <input type="text" name="maxPrice" value="<?php echo $maxPrice ?>" /> </td> </tr> </table> <table style="margin-left: auto; margin-right: auto;"> <tr> <td> <input type="submit" value = "Search" /> </td> <td> <input type="reset" value = "Clear" /> </td> </tr> <tr> <td> <?php echo build_checkbox("internet", "DSL"); ?> </td> <td> <?php echo build_checkbox("heating", ""); ?> </td> </tr> </table> </form> <?php require "footer.php"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/ Share on other sites More sharing options...
chhorn Posted December 6, 2019 Share Posted December 6, 2019 2 hours ago, Skittle said: displays the error messages You have multiple error messages there. So if you get e.g. "City was not specified" you can check for var_dump($city); then for var_dump(trim($_POST["city"])); then for var_dump($_POST["city"]); then for var_dump($_POST); and look what each variable contains. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572286 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 If you are having problems why have you commented out the two lines at the top that might tell you why you are having those problems? Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572290 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 I have used those by the way but it does not tell me why I am having this one particular issue Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572299 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 If you put those 2 lines back in, what error messages do get? Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572300 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 Warning: A non-numeric value encountered in /var/www/html/webd3201/group07/includes/functions.php on line 51 Notice: Undefined index: userType in /var/www/html/webd3201/group07/header.php on line 59 Notice: Undefined index: userId in /var/www/html/webd3201/group07/header.php on line 15 A bunch of lines of these three errors Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572301 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 First thing to do then is fix those errors. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572302 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 3 minutes ago, Barand said: First thing to do then is fix those errors. Yeah trying to figure out those errors, its been a long frustrating process Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572303 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 It might be an idea to check what your script is receiving in the GET and POST array. At top of the script, put echo '<pre>GET = ', print_r($_GET, 1), '</pre>'; echo '<pre>POST = ', print_r($_POST, 1), '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572304 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 1 minute ago, Barand said: It might be an idea to check what your script is receiving in the GET and POST array. At top of the script, put echo '<pre>GET = ', print_r($_GET, 1), '</pre>'; echo '<pre>POST = ', print_r($_POST, 1), '</pre>'; GET = Array ( ) { } POST = Array ( ) { } This is all i get when I put that in Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572305 Share on other sites More sharing options...
cyberRobot Posted December 6, 2019 Share Posted December 6, 2019 Just now, Skittle said: This is all i get when I put that in After adding the lines, did you try submitting the form? $_POST doesn't get populated until your form is submitted. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572306 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 1 minute ago, cyberRobot said: After adding the lines, did you try submitting the form? $_POST doesn't get populated until your form is submitted. Im dumb GET = Array ( ) POST = Array ( [newlist] => house for sale [city] => oshawa [minPrice] => 100 [maxPrice] => 1000 ) thisis what I get Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572307 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 Note, from that output, there is no $_GET['city']. This isn't surprising as you form's method is POST. Try checking for $_POST['city'] instead. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572308 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 2 minutes ago, Barand said: Note, from that output, there is no $_GET['city']. This isn't surprising as you form's method is POST. Try checking for $_POST['city'] instead. Wait what how do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572309 Share on other sites More sharing options...
cyberRobot Posted December 6, 2019 Share Posted December 6, 2019 2 minutes ago, Barand said: Try checking for $_POST['city'] instead. If you look further down the script, you'll see where $_POST is being used. I'm guessing this script is being used for more than just the form submission. 6 minutes ago, Skittle said: POST = Array ( [newlist] => house for sale [city] => oshawa [minPrice] => 100 [maxPrice] => 1000 ) Note that the script that processes the form submission is looking for 'headline', which isn't in the above output. Do you know how to change the form so "newlist" is "headline"? Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572310 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 One way is to edit the code, changing $_GET['city'] to $_POST['city']. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572311 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 @cyberRobot seems keen on this topic, so I'll leave you in his capable hands. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572312 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 2 minutes ago, cyberRobot said: If you look further down the script, you'll see where $_POST is being used. I'm guessing this script is being used for more than just the form submission. Note that the script that processes the form submission is looking for 'headline', which isn't in the above output. Do you know how to change the form so "newlist" is "headline"? Oh wow I didnt even notice that, no I don't know how to change that, but that still doesnt fix why minprice automatically goes blank too Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572313 Share on other sites More sharing options...
cyberRobot Posted December 6, 2019 Share Posted December 6, 2019 1 minute ago, Skittle said: ...I didnt even notice that, no I don't know how to change that... Take a closer look at the following line from your form: <input type="text" name="newlist" value="<?php echo $headline ?>" /> 1 minute ago, Skittle said: ...doesnt fix why minprice automatically goes blank too Try outputting the corresponding variable here: $minPrice = trim($_POST["minPrice"]); var_dump($minPrice); What does it give you? Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572314 Share on other sites More sharing options...
mac_gyver Posted December 6, 2019 Share Posted December 6, 2019 if you are just starting out, i recommend that you start with a form and form processing code for just one form field. once you get to the point of being able to design, write, test, and debug the code for one field, you can deal with the code needed for the rest of the fields, which you should add one a a time. btw - your empty min/max prices are likely due to the preg_match validation failing, where you are setting the min/max prices to an empty string. there's two problems with this validation - 1) your regex pattern probably has some problem, and 2) you should leave any value that didn't pass validation as is so that it will re-populate the form field. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572315 Share on other sites More sharing options...
cyberRobot Posted December 6, 2019 Share Posted December 6, 2019 14 hours ago, Skittle said: ...textbox go blank again and displays the error messages... You mentioned that the script "displays the error messages". For the "minPrice" field, I assume that you see one of the error messages below, correct? If so, which one? if($minPrice == "") { $error .= "<br/>Minimum price was not specified"; } elseif (preg_match(PRICE_FILTER, $minPrice) == '0') { $error .= "<br/>Minimum price needs to be a number"; $minPrice = ""; } Are you seeing "Minimum price needs to be a number"? If so, you might want to review the manual for preg_match(). The Return Values section should be of use...especially the Warning.https://www.php.net/manual/en/function.preg-match.php#refsect1-function.preg-match-returnvalues Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572316 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 This is for a class assignment technically I am just starting out but I am also on my second year of school, I've been working on this page for a week and still having these issues that's why I came here for help and the error message I am getting is the price needs to be a number so I will take a look at that manual thank you very much also the headline is now fixed to so that is good Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572333 Share on other sites More sharing options...
cyberRobot Posted December 6, 2019 Share Posted December 6, 2019 5 minutes ago, Skittle said: ...I am getting is the price needs to be a number... At least that means the lines processing the $_POST variables are executing. To make it so "minPrice" gets repopulated in the form, you should only need to remove the $minPrice = ""; line from the following code block: elseif (preg_match(PRICE_FILTER, $minPrice) == '0') { $error .= "<br/>Minimum price needs to be a number"; $minPrice = ""; } Of course, that won't fix the problem with the call to preg_match(). However, you should be one stop closer. Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572334 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 4 minutes ago, cyberRobot said: At least that means the lines processing the $_POST variables are executing. To make it so "minPrice" gets repopulated in the form, you should only need to remove the $minPrice = ""; line from the following code block: elseif (preg_match(PRICE_FILTER, $minPrice) == '0') { $error .= "<br/>Minimum price needs to be a number"; $minPrice = ""; } Of course, that won't fix the problem with the call to preg_match(). However, you should be one stop closer. if i take out the $minPrice = ""; yes it stays populated but I'm still getting the error message all I want to do is not to get that error message and execute the next part of my code. I changed the preg_match stuff to elseif(is_numeric($minPrice)) instead Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572335 Share on other sites More sharing options...
Skittle Posted December 6, 2019 Author Share Posted December 6, 2019 1 minute ago, Skittle said: if i take out the $minPrice = ""; yes it stays populated but I'm still getting the error message all I want to do is not to get that error message and execute the next part of my code. I changed the preg_match stuff to elseif(is_numeric($minPrice)) instead which now that I test it doesnt work either Quote Link to comment https://forums.phpfreaks.com/topic/309643-why-does-my-code-automatically-think-the-textbox-is-empty/#findComment-1572336 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.