Solved

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

Posted on 2014-01-07
11
803 Views
Last Modified: 2014-01-07
In PHP, if a form POSTS to itself, must I use a SESSION variable to get that info to another page?
0
Comment
Question by:LB1234
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 7

Assisted Solution

by:dimmergeek
dimmergeek earned 75 total points
ID: 39762178
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
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 225 total points
ID: 39762194
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
 
LVL 1

Author Comment

by:LB1234
ID: 39762196
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:LB1234
ID: 39762198
The form submits to itself.
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 225 total points
ID: 39762204
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
 
LVL 1

Author Comment

by:LB1234
ID: 39762205
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
 
LVL 9

Accepted Solution

by:
oheil earned 200 total points
ID: 39762206
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
 
LVL 7

Assisted Solution

by:dimmergeek
dimmergeek earned 75 total points
ID: 39762207
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
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 225 total points
ID: 39762222
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
 
LVL 1

Author Comment

by:LB1234
ID: 39762239
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
 
LVL 9

Assisted Solution

by:oheil
oheil earned 200 total points
ID: 39762259
You only read from the SESSION array.
It seems that from line 42
it should be
$_SESSION["first_name"]=$first_name;
...

Oli
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question