Variable Fails To Be Set

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zzarkc-20
Forum Newbie
Posts: 13
Joined: Tue Jul 29, 2008 2:09 am
Location: Indiana

Variable Fails To Be Set

Post by Zzarkc-20 »

I recently decided to clean my page up with making an include file for functions. The problem is, I run a field checking script, that will see if someone filled out a field. I have tried it using the filter_has_var method and the isset method (since on the PHP site it said that they were the same thing) for checking if there was something in the fields. In any case, whenever I have it check all the fields, $fail goes funky and a lot of my code doesn't work right. If I take out 1 !, then all the fields work but that one. I'm figuring this is a syntax issue, but I can't find the problem (for a few days now). Here's the files:

orderform1.php

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
        include('functions.php');
        if ( $_POST ){
        ordercheck();
        }
    ?>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Title</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\" >";
                ordererror();       
                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>
        <?php
            if( !$_POST || $fail == true ){
                printform();
            }
        ?>
            
            <?php
                if ( isset( $fail ) && $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>
functions.php

Code: Select all

 
<?php
/*
This code includes functions for displaying and checking an order form. 
 
functions:
    -ordercheck()
    -ordererror()
    -form()
*/
    function ordercheck(){
        global $fail;
        $fail = false;
        
        if( !isset( $_POST["fname"] ) ){
            $_POST["fname"]="";
            $fail=true;
        }
        if( !isset( $_POST["lname"] ) ){
            $_POST["lname"]="";
            $fail=true;
        }
        if( !isset( $_POST["email"] ) ){
            $_POST["email"]="";
            $fail=true;
        }
        if( !isset( $_POST["email"] ) ){
            if( !filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL ) ){
                $_POST["email"]="";
                $fail=true;
            }
        }
        if( !isset( $_POST["phonenumbera"] ) ){
            $_POST["phonenumbera"]="";
            $fail=true;
        }
        if( !isset( $_POST["phonenumberb"] ) ){
            $_POST["phonenumberb"]="";
            $fail=true;
        }
        if( !isset( $_POST["phonenumberc"] ) ){
            $_POST["phonenumberc"]="";
            $fail=true;
        }
        if( !isset( $_POST["address"] ) ){
            $_POST["address"]="";
            $fail=true;
        }
        if( !isset( $_POST["city"] ) ){
            $_POST["city"]="";
            $fail=true;
        }
        if( !isset( $_POST["zip"] ) ){
            $_POST["zip"]="";
            $fail=true;
        }
        if( !isset( $_POST["cd"] ) ){
            $_POST["cd"]="";
            $fail=true;
        }
        if( $fail == false ){
            if( !isset ($_POST["shipping"] ) ){
                $_POST["shipping"] = "No";
            }
        }
    }
    
    function ordererror(){
        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 />";
        }
    }
    
    function printform(){
    /*--CODE FOR A FORM HERE--(let me know if you want to see the form It displays content based on if the user has already input valid responses. Like it will set the input from last time and code it as the value="input" from the last accept.)*/
}
I believe the problem may be in function ordercheck(). That is the area that can make things go funky. Thanks for the help, I appreciate it!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Variable Fails To Be Set

Post by Christopher »

The logic for your checks could be:

Code: Select all

if( !isset( $_POST["fname"] ) || ($_POST["fname"] == '')){
You could also just loop through the fields in that function rather than all those if()'s.
(#10850)
Zzarkc-20
Forum Newbie
Posts: 13
Joined: Tue Jul 29, 2008 2:09 am
Location: Indiana

Re: Variable Fails To Be Set

Post by Zzarkc-20 »

I'll look into the looping deal. I wasn't familiar with that loop (I've been teaching myself), so I'll check that out. Was that logic suggestion the problem? What I was doing was giving the form elements a value that I could check later on and give error reporting on the page. I've been looking at the loop and I'm also wondering how I limit it to only certain values. It looks like a really good loop, but there are certain elements in the form that I don't want to have checked because the user doesn't need to to fill them out. How do I keep the loop from going through those?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Variable Fails To Be Set

Post by Christopher »

Zzarkc-20 wrote:How do I keep the loop from going through those?
Don't put them in the array you loop through.
(#10850)
Post Reply