Jump to content
Skittle

Why does my code automatically think the textbox is empty?

Recommended Posts

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";
?>

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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>';

 

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.