Jump to content

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
	# 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
		$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?



Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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.

  • 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.