Jump to content
Sign in to follow this  
dudleylearning

passing a variable from page2 to page1

Recommended Posts

Hi All,

 

couldn't seem to figure how to pass data from page 2 to page 1. On page 2 I have this code block:

# if the form has been posted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	# check for empty fields
	if (!empty(trim(($_POST['name'] == ''))))
	{
		$nameErr = "<span class=\"error\">Name is required</span>";
	}
	if (!empty(trim(($_POST['email'] == ''))))
	{
		$emailErr = "<span class=\"error\">Email is required</span>";
	}
	 
	# if all is fine, then add data to the database
	else 
	{
		$sql = 'INSERT INTO author SET
		name = :name,
		email = :email';
		
		$query = $dbConnection -> prepare($sql);
		$query -> bindValue(':name', $_POST["name"]);
		$query -> bindValue(':email', $_POST["email"]);
		$query -> execute();
		
		$message = 'done';
		header('Location: index.php');
		
	}
}

The variable $message I wanted to pass to page 1, where I have this in:

<?php if(isset($_POST['message'])) { echo $_POST['message']; } ?>

I can't seem to figure out the problem? Any tips?

 

Thanks

Share this post


Link to post
Share on other sites

While you "could" post from one page to another, don't and use sessions.

<?php
// Page 1  
session_start();
$_SESSION['message']=$message;



 

<?php
// Page 2  
session_start();
echo($_SESSION['message']);

Share this post


Link to post
Share on other sites

You can't, at least not in your current workflow. You'd have to create an extra form which the user then has to submit only to help you pass internal data around – which obviously makes no sense. This also violates the purpose of POST request (which are meant to modify data).

 

Use a cookie, session variable or URL parameter. Anything but a POST request.

Edited by Jacques1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.