Page 1 of 1

Variable Fails To Be Set

Posted: Tue Jul 29, 2008 2:42 am
by Zzarkc-20
I've been working on a PHP Form, that has a field checker. The page is not finished, and there's still quite a bit of work, but meanwhile I have a problem. Here's the code:

Code: Select all

 
<head>
    <?php
        if ( $_POST ){
            $fail = false;
            if ( filter_has_var( INPUT_POST, $_POST["fname"] ) ) {
                $_POST["fname"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["lname"] ) ) {
                $_POST["lname"]="";
                $fail=true;
            }
(MORE ERROR CHECKING CODE HERE...)
            if ( filter_has_var(INPUT_POST,$_POST["zip"] ) ) {
                $_POST["zip"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["cd"] ) ) {
                $_POST["cd"]="";
                $fail=true;
            }
        }
    ?>
</head>
<body>
    <?php
        if ( $_POST ){
            if ( $fail == true ){
                echo "<div id=\"error\" >";
            }
            if ( $_POST["fname"] == "" ){
                echo "*Please enter your first name.<br />";
            }
(MORE CODE HERE)
            if ( $_POST["cd"] == "" ){
                echo "*Please enter the number of CD's you are buying.<br />";
            }
            if ( $fail == true ){
                echo "</div><hr />";
            }
        }
    ?>
</body>
 
Meanwhile, when I execute the code, this is what would show:

Code: Select all

 
*Please enter your first name.<br />
*Please enter your last name.<br />
*Please enter your zip code.<br />
 
Now, the rendered page shows none of the styling techniques through my CSS page on the div. The generated code shows that the if statements weren't executed. I can't figure out why, and when I echoed the value of $fail, it came out blank. I tried making $fail a global, using both, global $fail; and $GLOBALS["fail"], methods.

I am sending the form to itself right now because I can't think of a way to redirect the POST through pages without making a form (and the user submitting). If you'd like the full file, contact me, and let me know. I can email you the file.

Re: Variable Fails To Be Set

Posted: Tue Jul 29, 2008 2:48 am
by WebbieDave
Check the contents of $_POST at the top of your script by using print_r($_POST);

If you still have trouble figuring out what's happening, post the html form being used.

Re: Variable Fails To Be Set

Posted: Tue Jul 29, 2008 2:59 am
by Zzarkc-20
Alright... I suppose it'll save some time since I can't post the whole file... The problem isn't in the $_POST variable, but a variable created in the script.

Here's the full page:

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
        if ( $_POST ){
            $fail = false;
            if ( filter_has_var( INPUT_POST, $_POST["fname"] ) ) {
                $_POST["fname"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["lname"] ) ) {
                $_POST["lname"]="";
                $fail=true;
            }
        
            if ( filter_has_var(INPUT_POST, $_POST["email"] ) ) {
                $_POST["email"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["email"] ) ) {
                if (!filter_input(INPUT_POST, $_POST["email"], FILTER_VALIDATE_EMAIL) ){
                    $_POST["email"]="";
                    $fail=true;
                }
            }
            
            if ( filter_has_var(INPUT_POST, $_POST["phonenumbera"] ) ) {
                $_POST["phonenumbera"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["phonenumberb"] ) ) {
                $_POST["phonenumberb"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["phonenumberc"] ) ) {
                $_POST["phonenumberc"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["address"] ) ) {
                $_POST["address"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["city"] ) ) {
                $_POST["city"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST,$_POST["zip"] ) ) {
                $_POST["zip"]="";
                $fail=true;
            }
            if ( filter_has_var(INPUT_POST, $_POST["cd"] ) ) {
                $_POST["cd"]="";
                $fail=true;
            }
            if ( $fail == false ){
                if (!isset($_POST["shipping"])){
                $_POST["shipping"] = "No";
                }
                $total = money_format('%.2n', $_POST["cd"]*6.99*1.06 );
            }
        }
    ?>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Milton Roe - Audio Eye Candy - CD Order Form</title>
<link rel="stylesheet" type="text/css" href="style.css"  />
<link rel="shortcut icon" href="favicon.gif" type="image/vnd.microsoft.icon" />
<link rel="icon" href="favicon.gif" type="image/vnd.microsoft.icon" />
</head>
<body>
<div id="ocontainer">
    <h1>
        <center>
            CD Order Form
        </center>
    </h1>
    <hr />
    <?php
        if ( $_POST ){
            if ( $fail == true ){
                echo "<div id=\"error\" >";
            }
            if ( $_POST["fname"] == "" ){
                echo "*Please enter your first name.<br />";
            }
            if ( $_POST["lname"] == "" ){
                echo "*Please enter your last name.<br />";
            }
            if ( $_POST["email"] == "" ){
                echo "*Please enter a valid email address.<br />";
            }
            if ( $_POST["phonenumbera"] == "" || $_POST["phonenumberb"] == "" || $_POST["phonenumberc"] == "" ){    
                echo "*Please enter your phone number.<br />";
            }
            if ( $_POST["address"] == "" ){
                echo "*Please enter your address.<br />";
            }
            if ( $_POST["city"] == "" ){
                echo "*Please enter your city.<br />";
            }
            if ($_POST["state"]== "--State--") {
                echo "*Please select your state.<br />";
            }
            if ( $_POST["zip"] == "" ){
                echo "*Please enter your zip code.<br />";
            }
            if ( $_POST["cd"] == "" ){
                echo "*Please enter the number of CD's you are buying.<br />";
            }
            if ( $fail == true ){
                echo "</div><hr />";
            }
        }
    ?>
    <div id="picture">
        <h2> You are ordering:<br />
            <br />
            Audio Eye Candy<br />
            <br />
            <img src="aec.gif" /><br />
        </h2>
    </div>
    <div id="content3">
        <h3>
            <form action="orderform1.php" method="post">
                First Name:&nbsp&nbsp&nbsp&nbsp&nbsp Last Name:<br />
                <input type="text" name="fname" size="10"
                <?php
                    if ( isset( $_POST["fname"] ) ){
                        echo " value=\"".$_POST["fname"]."\"";
                    }
                ?>
             />
                &nbsp
                <input type="text" name="lname" size="12"
                <?php
                    if ( isset( $_POST["lname"] ) ){
                        echo " value=\"".$_POST["lname"]."\"";
                    }
                ?>
            />
                <br />
                <br />
                Email: <br />
                <input type="text" name="email"
                <?php
                    if ( isset( $_POST["email"] ) ){
                        echo " value=\"".$_POST["email"]."\"";
                    }
                ?>
            />
                <br />
                <br />
                Phone Number: <br />
                (
                <input type="text" name="phonenumbera" maxlength="3" size="3"
                    <?php
                        if ( isset( $_POST["phonenumbera"] ) ){
                            echo " value=\"".$_POST["phonenumbera"]."\"";
                        }
                    ?>
                />
                )
                <input type="text" name="phonenumberb" maxlength="3" size="3"
                    <?php
                        if ( isset( $_POST["phonenumberb"] ) ){
                            echo " value=\"".$_POST["phonenumberb"]."\"";
                        }
                    ?>
                />
                -
                <input type="text" name="phonenumberc" maxlength="4" size="4"
                    <?php
                        if ( isset( $_POST["phonenumberc"] ) ){
                            echo " value=\"".$_POST["phonenumberc"]."\"";
                        }
                    ?>
                />
                <br />
                <br />
                Address:
                <input type="text" name="address"
                    <?php
                        if ( isset( $_POST["address"] ) ){
                            echo " value=\"".$_POST["address"]."\"";
                        }
                    ?>
                />
                <br />
                City:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                <input type="text" name="city"
                    <?php
                        if ( isset( $_POST["city"] ) ){
                            echo " value=\"".$_POST["city"]."\"";
                        }
                    ?>
                />
                <br />
                <br />
                State:
                <select name="state" >
                    <?php
                        if ( isset( $_POST["state"] ) ){
                            echo "<Option value=\"".$_POST["state"]."\">".$_POST["state"]."</Option>";
                        }
                    ?>
                    <Option value="--State--">--State--</Option>
                    <Option value="Alabama (AL)">Alabama (AL)</Option>
                    <Option value="Alaska (AK)">Alaska (AK)</Option>
                    <Option value="Arizona (AZ)">Arizona (AZ)</Option>
                    <Option value="Arkansas (AR)">Arkansas (AR)</Option>
                    <Option value="California (CA)">California (CA)</Option>
                    <Option value="Colorado (CO)">Colorado (CO)</Option>
                    <Option value="Connecticut (CT)">Connecticut (CT)</Option>
                    <Option value="Delaware (DE)">Delaware (DE)</Option>
                    <Option value="District of Columbia (DC)">District of Columbia (DC)</Option>
                    <Option value="Florida (FL)">Florida (FL)</Option>
                    <Option value="Georgia (GA)">Georgia (GA)</Option>
                    <Option value="Hawaii (HI)">Hawaii (HI)</Option>
                    <Option value="Idaho (ID)">Idaho (ID)</Option>
                    <Option value="Illinois (IL)">Illinois (IL)</Option>
                    <Option value="Indiana (IN)">Indiana (IN)</Option>
                    <Option value="Iowa (IA)">Iowa (IA)</Option>
                    <Option value="Kansas (KS)">Kansas (KS)</Option>
                    <Option value="Kentucky (KY)">Kentucky (KY)</Option>
                    <Option value="Louisiana (LA)">Louisiana (LA)</Option>
                    <Option value="Maine (ME)">Maine (ME)</Option>
                    <Option value="Maryland (MD)">Maryland (MD)</Option>
                    <Option value="Massachusetts (MA)">Massachusetts (MA)</Option>
                    <Option value="Michigan (MI)">Michigan (MI)</Option>
                    <Option value="Minnesota (MN)">Minnesota (MN)</Option>
                    <Option value="Mississippi (MS)">Mississippi (MS)</Option>
                    <Option value="Missouri (MO)">Missouri (MO)</Option>
                    <Option value="Montana (MT)">Montana (MT)</Option>
                    <Option value="Nebraska (NE)">Nebraska (NE)</Option>
                    <Option value="Nevada (NV)">Nevada (NV)</Option>
                    <Option value="New Hampshire (NH)">New Hampshire (NH)</Option>
                    <Option value="New Jersey (NJ)">New Jersey (NJ)</Option>
                    <Option value="New Mexico (NM)">New Mexico (NM)</Option>
                    <Option value="New York (NY)">New York (NY)</Option>
                    <Option value="North Carolina (NC)">North Carolina (NC)</Option>
                    <Option value="North Dakota (ND)">North Dakota (ND)</Option>
                    <Option value="Ohio (OH)">Ohio (OH)</Option>
                    <Option value="Oklahoma (OK)">Oklahoma (OK)</Option>
                    <Option value="Oregon (OR)">Oregon (OR)</Option>
                    <Option value="Pennsylvania (PA)">Pennsylvania (PA)</Option>
                    <Option value="Puerto Rico (PR)">Puerto Rico (PR)</Option>
                    <Option value="Rhode Island (RI)">Rhode Island (RI)</Option>
                    <Option value="South Carolina (SC)">South Carolina (SC)</Option>
                    <Option value="South Dakota (SD)">South Dakota (SD)</Option>
                    <Option value="Tennessee (TN)">Tennessee (TN)</Option>
                    <Option value="Texas (TX)">Texas (TX)</Option>
                    <Option value="Utah (UT)">Utah (UT)</Option>
                    <Option value="Vermont (VT)">Vermont (VT)</Option>
                    <Option value="Virginia (VA)">Virginia (VA)</Option>
                    <Option value="Washington (WA)">Washington (WA)</Option>
                    <Option value="West Virginia (WV)">West Virginia (WV)</Option>
                    <Option value="Wisconsin (WI)">Wisconsin (WI)</Option>
                    <Option value="Wyoming (WY)">Wyoming (WY)</Option>
                </select>
                <br />
                <br />
                Zip Code:
                <input type="text" name="zip" maxlength="5" size="5"
                    <?php
                        if ( isset( $_POST["zip"] ) ){
                            echo " value=\"".$_POST["zip"]."\"";
                        }
                    ?>
                />
                <br />
                <br />
                How many CD's would you like:
                <input type="text" name="cd" maxlength="2" size="2"
                    <?php
                        if ( isset( $_POST["cd"] ) ){
                            echo " value=\"".$_POST["cd"]."\"";
                        }
                    ?>
                />
                x $6.99 + tax <br />
                <input type="checkbox" name="shipping" value="Yes"
                    <?php
                        if ( isset( $_POST["shipping"] ) && $_POST["shipping"]=="Yes" ){
                            echo " checked=\"checked\"";
                        }
                    ?>
                />
                Shipping and Handling (Adds $2.95 to the total)<br />
                <br />
                How did you find out about the CD: (opt.)<br />
                <textarea name="found" cols="30" rows="5" wrap="soft">
                    <?php
                        if ( isset( $_POST["found"] ) ){
                            echo $_POST["found"];
                        }
                    ?>
                </textarea>
                <br />
                <br />
                Comments: (opt.)<br />
                <textarea name="comments" cols="30" rows="8" wrap="soft">
                    <?php
                        if ( isset( $_POST["comments"] ) ){
                            echo $_POST["comments"];
                        }
                    ?>
                </textarea>
                <br />
                <br />
                <input type="hidden" name="sent" value="1" />
                <center>
                    <input type="Submit" value="Send Order" />
                    <input type="reset" value="Reset Form" />
                </center>
            </form>
            <?php
                if (isset( $GLOBALS["fail"] ) && $GLOBALS["fail"] == false ){
                    echo "Please verify this information is correct.<br /><br />";
                    echo "Name: ".$_POST["fname"]." ".$_POST["lname"]."<br />";
                    echo "Email: <a href=\"mailto:".$_POST['email']."\">".$_POST['email']."</a><br />";
                    echo "Address: ".$_POST["address"]."<br />";
                    echo "City: ".$_POST["city"]."<br />";
                    echo "State: ".$_POST["state"]."<br />";
                    echo "Zip Code: ".$_POST["zip"]."<br />";
                    echo "CD's: ".$_POST["cd"]."<br />";
                    echo "Total: $".$total."<br />";
                    echo "Shipping: ".$_POST["shipping"]."<br />";
                    echo "How did you find out about our CD: ".$_POST["found"]."<br />";
                    echo "Comments: ".$_POST["comments"]."<br />";
                }
            ?>
        </h3>
    </div>
</div>
</body>
</html>
 
Now, I know the page isn't finished yet. I'm going to do more formatting with the error checking, and once the fields are all O.K., I'm going to make the form disappear. I'm sending the file to itself because I can't figure out how to send the $_POST through another page and then redirect it back to another page. That would help with make a cleaner page with a few includes, but this is what I have.

Anyway, the if statements in the body dealing with $fail aren't executing and I can't understand why.

Re: Variable Fails To Be Set

Posted: Tue Jul 29, 2008 4:13 am
by EverLearning
Because, as the PHP Manual for filter_has_var states, second parameter for filter_has_var() is a variable name, which is not $_POST['fname'], but just fname, because you already specified input type, with the first parameter - INPUT_POST.

So, this

Code: Select all

filter_has_var( INPUT_POST, $_POST['fname'] )
should be

Code: Select all

filter_has_var( INPUT_POST, 'fname' )

Re: Variable Fails To Be Set

Posted: Tue Jul 29, 2008 1:29 pm
by Zzarkc-20
Thanks a ton. That fixed the problem. It was weird because it was still working with the variable checking (I think), but that solved the problem. Thanks again!