Send post values with header location

Hello everyone,

I’m using a form to send some values to another page, but how does it work if I use the header function? Once the form has been submitted, how do I access the $name and $employer variables on the receiving page? The form also has a hidden input with a value.

Please help me out. Thank you so much!

<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array();
	
if (empty($_POST['name'])) {
		$errors['name'] = 'Please enter your name.';
	} else {
		$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
	}
	
	
	if (empty($_POST['employer'])) {
		$errors['employer'] = 'Please add your employer.';
	} else {
		$employer = filter_var($_POST['employer'], FILTER_SANITIZE_STRING);
	}
	
	if ($name && $employer ) { //	
header("location: page3.php ");
exit ();	
}

}
<form name="myform" action="" method="post">

<p>Name</p>
<input type="text" name="name" value="" />
<span class="warning"><?php if (isset($errors['name'])) echo $errors['name']; ?> </span>

<p>Employer</p>
<input type="text" name="employer" value="" />
<span class="warning"><?php if (isset($errors['employer'])) echo $errors['employer']; ?> </span>

<input name="somename" type="hidden" value="somevalue"/>
<input type="submit" value="send">
</form>

Use a PHP session. On the redirect page, start the session and assign the $_POST data to the $_SESSION:

<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array();
	
if (empty($_POST['name'])) {
		$errors['name'] = 'Please enter your name.';
	} else {
		$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
	}
	
	
	if (empty($_POST['employer'])) {
		$errors['employer'] = 'Please add your employer.';
	} else {
		$employer = filter_var($_POST['employer'], FILTER_SANITIZE_STRING);
	}

        session_start();
        $_SESSION = $_POST;
        session_write_close();
	
	if ($name && $employer ) { //	
header("location: page3.php ");
exit ();	
}

}
<form name="myform" action="" method="post">

<p>Name</p>
<input type="text" name="name" value="" />
<span class="warning"><?php if (isset($errors['name'])) echo $errors['name']; ?> </span>

<p>Employer</p>
<input type="text" name="employer" value="" />
<span class="warning"><?php if (isset($errors['employer'])) echo $errors['employer']; ?> </span>

<input name="somename" type="hidden" value="somevalue"/>
<input type="submit" value="send">
</form>

Then on page3.php, restart the session and set the $_SESSION data to the $_POST. Then use as normal:


<?php
session_start();
$_POST = $_SESSION;
?>

Hi there threehundred,

thanks for your help.

I didn’t think that it would be necessary to use a session. I have found that this also works:

header("location: page3.php&name=$name ");

But not sure if this is an acceptable solution. Normally when I use forms I use the mail function to send form values to an email address. This is the first time that I’m sending the values to another page, and I’m uncertain if the header function has to be used at all. Without validating the values I could send the values as is to another page, but with validation I don’t know what to do after: if ($name && $employer ).

Is the header function the standard way to send form data to another page?

Thank you!

The header function would be the standard way, at least when a redirect is needed.

You can do header("location: page3.php&name=$name ") which is much easier but that uses $_GET rather than $_POST.

$_POST is typically used when handling forms

Ok, thank you.

Could someone else please confirm that using header(location) together with either a session or appending a query string to the location URL is the only way to send post values via a form to another page - given how I have structured my validation code. And without using JavaScript/Ajax.

I just need to be sure.

Thanks!

Hi,
yes, session is the best way for it.

Thanks mushira.

Hi fretburner, cpradio and others…

what would you recommend? I don’t really want to use sessions because not every visitor will have cookies enabled.

Please point me in the right direction. Thank you very much!

Hey RedBishop,

Good weekend?

Do you have a reason to think that many of your visitors would have cookies disabled? If it is an issue, PHP can pass the session ID as a URL parameter rather than in a cookie.

Good weekend?

Yeah, good thanks and you?

Do you have a reason to think that many of your visitors would have cookies disabled?

I for one mostly have cookies disabled and probably quite a few other people do so as well. But if I know I need to enable cookies to use a site like sitepoint.com then it isn’t such a big deal. Thank you for the link to the PHP manual. I have come across some info advising against using session IDs in URLs because of negative SEO consequences. But don’t know if this is true or not.

Yes, because session ID is part of the URL and it is different every time a search engine robot crawls your site in effect the URL’s of your pages change all the time and this may not be SEO friendly. But if you use the session ID only for your form then what’s the problem? Search engines don’t and aren’t supposed to be filling in and submitting forms on your site anyway, so appending session ID’s to form URL’s will not do any harm.

Yes, because session ID is part of the URL and it is different every time a search engine robot crawls your site in effect the URL’s of your pages change all the time and this may not be SEO friendly. But if you use the session ID only for your form then what’s the problem? Search engines don’t and aren’t supposed to be filling in and submitting forms on your site anyway, so appending session ID’s to form URL’s will not do any harm.

Yes, that all makes sense. Thanks for the clarification.