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
805 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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