Avatar of LB1234
LB1234
 asked on

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?
PHP

Avatar of undefined
Last Comment
oheil

8/22/2022 - Mon
SOLUTION
dimmergeek

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Cornelia Yoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
LB1234

ASKER
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?
LB1234

ASKER
The form submits to itself.
SOLUTION
Cornelia Yoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
LB1234

ASKER
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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
oheil

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
dimmergeek

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Cornelia Yoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
LB1234

ASKER
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

SOLUTION
oheil

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.