• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 823
  • Last Modified:

In PHP, if a form POSTS to itself, must I use a SESSION variable to get that info to another page?

In PHP, if a form POSTS to itself, must I use a SESSION variable to get that info to another page?
0
LB1234
Asked:
LB1234
  • 4
  • 3
  • 2
  • +1
7 Solutions
 
dimmergeekCommented:
It depends on what you are doing.  I have used session variables, cookies, query string and databases to pass data from page 1 to page 2 when page 1 posts to itself.
Can you tell us what you are trying to accomplish?
0
 
Cornelia YoderArtistCommented:
A SESSON variable is definitely the easiest, as long as the other page is within the same domain.  Session variables don't exist across domains.
0
 
LB1234Author Commented:
Just a simple single-page validation create user form.  To clarify, the POST information in the array is not available to other pages by default without additional steps to transfer the data?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
LB1234Author Commented:
The form submits to itself.
0
 
Cornelia YoderArtistCommented:
Correct.  The POST information is available to the page that is called by the form.

If you want other pages, in that same domain, to be able to access the data, simply put it into a SESSION variable instead of the POST variable.
0
 
LB1234Author Commented:
So should i just set the following POST array to a SESSION array to capture the data from the form for an "account created" page which shows what the user entered?

	if(isset($_POST["submit"])) {
	  	$first_name = $_POST["first_name"];
	  	$last_name = $_POST["last_name"];
	  	$user_name = $first_name[0] . "_" . $last_name;
	  	$access_level = $_POST["access_level"];
		$department = $_POST["department"];
		$password = $_POST["password"];
	  	$password2 = $_POST["password2"];
		 	

Open in new window

0
 
oheilCommented:
I would say: Yes.

Lets say we start with start.php.
We enter the data into the form, press submit, and again we call start.php.
The POST data is than only available inside the start.php during this last call.
From the result page in the browser (after submit, and after start.php again has finished) we open a new page linked inside start.php, say it is named second.php.
We call second.php by clicking on a link produced by start.php.
The script/page second.php does not see any POST data. You need to write the POST data during the second call to start.php into a session. This session remains than accessible in second.php.

Oli
0
 
dimmergeekCommented:
That is correct.  If the form submits to itself, then the POST array will NOT be available to other pages without other work such as session variables.
0
 
Cornelia YoderArtistCommented:
If the first page uses the data, you can get it from the POST array to use, and later set it into a SESSION variable.

$dataval = $_POST["dataval"];
$_SESSION["dataval"] = $dataval;

If all you need to do is pass it on, just

$_SESSION["dataval"] = $_POST["dataval"];

Don't forget to use SESSION_START at the beginning of each script using SESSION variables.
0
 
LB1234Author Commented:
When I do a var_dump on the page redirected to (account_created.php) it says the array is empty.  Any ideas why this is happening?

<?php session_start();?> 
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/db_connection.php"); ?>


<?php
      
$result = "";

/*$existing_user_check = "SELECT user_name from users";

$result = mysqli_query($connection, $existing_user_check);

	  
while($row = mysqli_fetch_assoc($result)) {
var_dump($result);
}  */
	  
	  
	  $errors = array();  
		
		if(isset($_POST["submit"])) {
	  	$first_name = $_POST["first_name"];
	  	$last_name = $_POST["last_name"];
	  	$user_name = $first_name[0] . $last_name;
	  	$access_level = $_POST["access_level"];
		$department = $_POST["department"];
		$password = $_POST["password"];
	  	$password2 = $_POST["password2"];
		 	
			 if ($password !== $password2) {
				$errors[] = "Passwords do not match, please re-enter";  
					 if (empty($password)) {
						 $errors[] = "Password is empty, please enter a password";
					 }
					 
					  } else {
						$query = "INSERT INTO users (first_name, last_name, user_name,  department, password, password2, access_level) VALUES (";
						$query .= "'{$first_name}', '{$last_name}', '{$user_name}',  '{$department}', '{$password}', '{$password2}', '{$access_level}')";
						$result = mysqli_query($connection, $query);
					
		$first_name = $_SESSION["first_name"];
	  	$last_name = $_SESSION["last_name"];
	  	$user_name = $first_name[0] . $last_name;
	  	$access_level = $_SESSION["access_level"];
		$department = $_SESSION["department"];
		$password = $_SESSION["password"];
	  	$password2 = $_SESSION["password2"];
	
		redirect_to("account_created.php");
					  }
	} else {
  
	  	$first_name = "";
	  	$last_name = "";
	  	$user_name = "";
	  	$access_level = "";
		$department = "";
		$password = "";
	  	$password2 = "";
	  	$message = "Please log in";
	}



?>

<?php
$options = array("user", "administrator");
$html_options = "";
?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="default.css" rel="stylesheet" type="text/css">
</head>

<body>
<div>
<h1>Please create user</h1>


<form action="create_user.php" method="post">
<table  width = "400" border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td>Enter First Name</td>
    <td><input name="first_name" type="text" value="<?php echo $first_name ?>"></td>
  </tr>
  <tr>
    <td>Enter Last Name</td>
    <td><input type="text" name="last_name" value="<?php echo $last_name ?>"> </td>
  </tr>
  <tr>
    <td>Enter Password</td>
    <td> <input type="password" name="password"> </td>
  </tr>
  <tr>
    <td>Re-Enter Password</td>
    <td><input type="password" name="password2"></td>
  </tr>
   <tr>
    <td>Set Access level</td>
    <td><select name="access_level" id="access_level">
      
<?php
		
foreach ($options as $option) {
	
	$selected = ($access_level == $option)  ? "selected" : "";
	$html_options .=  "<option value='{$option}' {$selected}>{$option}</option>";
	}

echo $html_options;

?>
    
      
    </select></td>
  </tr>
    <td>Department</td>
  <td><select name="department" id="department">
    <option value="administration">Administration</option>
    <option value="research">Research</option>
    <option value="sales">Sales</option>
    <option value="trading">Trading</option>
  </select></td>
  </tr>
</table>





<input name="submit" type="submit" id="submit">
</form>

</div>




</body>
</html>

Open in new window

0
 
oheilCommented:
You only read from the SESSION array.
It seems that from line 42
it should be
$_SESSION["first_name"]=$first_name;
...

Oli
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now