Simple question regarding isset

Discussion in 'PHP' started by absentx, Nov 4, 2011.

  1. #1
    Okay so I have a self referencing form like so:

    
    <form enctype="multipart/form-data" method="post" action=<?php echo $_SERVER['PHP_SELF']; ?>>
    
    <p><label for="first_name">First Name:</label>
    <input type="text" id="first_name"  value="<?php echo $first_name; ?>" /></p>
    <p><label for="last_name">Last Name:</label>
    <input type="text" id="last_name"  value="<?php echo $last_name; ?>" /></p>
    
    <p><input name="submit" class = "submit" type="submit" value="Submit Information" /></p>
    </form>
    
    
    HTML:
    I started running some error logs today and I found that everytime I would get an error like this:

    Unknown error [#8]: Undefined variable: first_name
    in /home/mysite.com/layout_includes/contact.php on line 7
    Unknown error [#8]: Undefined variable: last_name
    in /home/mysite.com/layout_includes/contact.php on line 9

    So my question is would something like below be the actual "proper" way to code these variables, meaning we need to check if they exist before trying to echo them?

    
    <form enctype="multipart/form-data" method="post" action=<?php echo $_SERVER['PHP_SELF']; ?>>
    
    <p><label for="first_name">First Name:</label>
    <input type="text" id="first_name"  value="<?php if(isset($first_name)) {echo $first_name;} ?>" /></p>
    <p><label for="last_name">Last Name:</label>
    <input type="text" id="last_name"  value="<?php if(isset($last_name)) {echo $last_name;} ?>" /></p>
    
    <p><input name="submit" class = "submit" type="submit" value="Submit Information" /></p>
    </form>
    
    
    HTML:
    I am not only looking for a debate on whether or not to use self referencing forms, if you will first be kind enough to answer my main question, then feel free to expand on other ideas!
     
    absentx, Nov 4, 2011 IP
  2. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you should use $_POST along with isset

    value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>"

    Do the same thing for other variables.
     
    littlejohn199, Nov 4, 2011 IP
  3. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry I left out some of my script:

    
    If (isset($_POST['submit'])) {
    	
    	$validation = new Validate;
    	
    	
    	$main = $_POST['main'];
    	$contact = $_POST['contact'];
    	$first_name = $_POST['first_name'];
    	$last_name = $_POST['last_name'];
    	$phone = $_POST['phone_number'];
    	$email = $_POST['email'];
    	$comment = $_POST['comment'];
    	$email_fail = false;
    	$phone_fail = false;
    	
    
        if (!($validation->isEmail($email))){
    		$email_fail = true;
    	}
    	if (!($validation->isPhone($phone))){
    		$phone_fail = true;
    	}
    	if (!$phone_fail && !$email_fail){
    		include ('utilities/email.php');	
    	}
    
    
    }
    
    
    
    
    
    
    PHP:
    Now, with that said, do I still need to be checking to see if the POST variables are set in the form include? As my code works, I suppose the POST variables are only set to the non global variables if they exist...so I suppose checking for POST is proper?

    The whole point of this thread was to start a discussion so I better understand what the best way to work this is, so I appreciate the feedback, please keep it coming.
     
    Last edited: Nov 4, 2011
    absentx, Nov 4, 2011 IP