SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue passing Session Variable

    I am trying to pass a value from one form to another using sessions.

    Form1:
    Code:
    <?php
      // Address error-handling.
      error_reporting(E_ALL & ~E_NOTICE);
    
      // Include Configuration Settings.
      require_once ('includes/config.inc.php');
    
      // Start output buffering.
      ob_start();
    
      // Initialize a session.
      session_start();
      echo "session_id() = " . session_id() . "<br />";
    
      // FUNCTION: Handle POST values.
      function input_post($name, $default = NULL) {
        return isset($_POST[$name]) ? $_POST[$name] : $default;
      }
    
      // Define variables.
      $email = input_post('email');
      $new_cust = input_post('new_cust');
      $password = input_post('password');
      $submitted = input_post('submitted');
    
      // Initialize array.
      $errors= array();
    
      // Handle form.
      if (isset($submitted)) {
        
        // Path to DB Connection Script.
        require_once (MYSQL);
        
        // Trim email.
    	$email = trim($email);
        
       	// Validate E-mail address.
    	if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $email)) {
    		$e = mysqli_real_escape_string ($dbc, $email);
    	} else {
            $errors['email']='Please enter a valid E-mail address.';
    	}
    
        // Check for errors.
        if(empty($errors)) {
            // Data Valid.
            // Build query.
            $q = "SELECT user_id FROM users WHERE email='$e'";
    
            // Run query.
    		$r = mysqli_query ($dbc, $q)
                        or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    
            // Check for duplicate E-mail.
    		if (mysqli_num_rows($r) == 0) {
              // Available.
              // Assign validated Email to session variable.
              $_SESSION['email'] = $e;
              echo '$_SESSION[\'email\'] = ' . $_SESSION['email'];
              // Continue Registration.
              header('Location: 104_CreateUser.php');
            } else {
              // Not Available.
              $errors['email']='This e-mail has already been registered!!';
            }
        }
      }
      ob_end_flush();
    ?>

    Form 2:

    Code:
    <?php
      //This page processes...
    
      // Address error-handling.
      error_reporting(E_ALL & ~E_NOTICE);
    
      // Include Configuration Settings.
      require_once ('includes/config.inc.php');
    
      // Start output buffering.
      ob_start();
    
      // Initialize a session.
      session_start();
      echo "session_id() = " . session_id() . "<br />";
    
      // FUNCTION: Handle POST values.
      function input_post($name, $default = NULL) {
        return isset($_POST[$name]) ? trim($_POST[$name]) : $default;
      }
    
      echo "POST = " . input_post('$_POST['email1']');
    
      // Define variables.
      $email1 = input_post('email1');
      echo "email1 = " . $email1;
    
    
    Question:

    Why can't I reference the value in my Email1 input box?

    The session variable is copied into the Input Box, however, when I reference $_POST['email1'] I'm getting some Undefined Index error.

    And no value is printing for echo $email1?!

    What is happening?


    Amy

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You put it in $_SESSION['email'], so read it from $_SESSION['email'], not $_POST

    Sessions are not in any way tied to forms

  3. #3
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You put it in $_SESSION['email'], so read it from $_SESSION['email'], not $_POST

    Sessions are not in any way tied to forms
    But I assigned the session value to the form, so it is in $_POST.

    The problem was I left out method="post" in my form...


    Amy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •