I have a form on page 2 and I want the information submitted on the form to be sent to page 3. How do i do that?

Recommended Answers

All 27 Replies

You can save it onto a $_SESSION variable. Assuming that on page1.php you are submitting a POST request (as opposed to a get), then on page2.php:

<?php
session_start();

foreach($_POST as $fieldName=>$value){
  $_SESSION[$fieldName]=$value;
}
?>

Then on page3.php

<?php
session_start();
//for the sake of clarity, let's say that on form1.php you had:
//<input type="text" name="firstName" />
//then page2.php created $_SESSION["firstName"] in the foreach constructed I gave you
//So here you can simply retrieve that value (just make sure you always call 
//session_start() at the beginning of the file first)
echo $_SESSION['firstName'];
?>

You can save it onto a $_SESSION variable. Assuming that on page1.php you are submitting a POST request (as opposed to a get)

page 1 and page 2 are .html not .php files. does that make a difference?

im sorry for double posting I couldnt find an edit option.

this the form I am using
<div id="apDiv1">
<form action="php/sendresults.php" id="email">
<textarea name="Message" cols="50" rows="10">Hi my name is&#8230;&#8230;&#8230;</textarea>
<br />
<br />
<textarea name="Contact info" cols="50">contact info</textarea>
</form>


<form name="input" action="php/sendresults.php" method="get">
<input type="submit" value="Submit" />
</form>

</div>

page 1 and page 2 are .html not .php files. does that make a difference?

Yes. They must be PHP files.

this the form I am using

That is NOT a form. What you have are TWO forms. The submit button applies ONLY to the second form, so you will never see what is entered in the Message and Contact Info because that first form is never submitted. You just merge the forms into one.

<!-- consider this page1.php -->
<div id="apDiv1">
<form action="php/sendresults.php" id="email" method="post">
<textarea name="Message" cols="50" rows="10">Hi my name is………</textarea>
<br />
<br />
<textarea name="Contact info" cols="50">contact info</textarea>

<input type="submit" value="Submit" />
</form>

</div>

Then in sendresults.php

<?php
session_start();
//here you are saving the data sent from previous form onto $_SESSION
foreach($_POST as $fieldName=>$value){
  $_SESSION[$fieldName]=$value;
}

//now we give the user another form to fill. Basically this is acting as page2.php
//notice that we are submitting to emailIt.php (which is basically your page3.php)
?>
<form method="post" action="php/emailIt.php">
<div>Full Name: <input type="text" name="name" value=""/></div>
<div>Email: <input type="text" name="email" value=""/></div>
<input type="submit" name="Send" value="Submit"/>
</form>

Finally on the last page you can process all the data at once:

<?php
session_start();

//here I am printing the stuff that was submitted on the first form
foreach($_SESSION as $k=>$v){
  echo $k . '=' . $v.'<br>';
}

//now I am printing the stuff sent by the second form
foreach($_POST as $k=>$v){
  echo $k . '=' . $v.'<br>';
}

If you wanted, you could combine the data from the forms onto a single array:

<?php
session_start();
$data=array_merge($_SESSION,$_POST);
foreach($data as $k=>$v){
  echo $k . '=' . $v.'<br>';
}
?>

PS: Whenever you post code on this forum/site, please click on the button labeled CODE on the editor and then paste you code within the CODE tags generated by the editor

thanks.

So i tried it, I made 3 new files with the names you said to test it.
page1.php
sendresults.php
emailIt.php

What happens is that when i press submit it goes to a blank page, sendresults.php.
I also get an email from myself with the form data submited.

You said that "//now we give the user another form to fill. Basically this is acting as page2.php", why would we make the person fill out another form?

I know in the second code I can get rid of action="sendresults.php" for the person to not be sent to another page which is what I want to do.

Im just really lost, why there are 3 pages for this code?

I want it be like a comment, kind of like the way this works and it posts on the same page just posting the message on a different page.

Thanks!

So i tried it, I made 3 new files with the names you said to test it.
page1.php
sendresults.php
emailIt.php

OK, but did you change the action attribute on the <form> . If you left <form action="php/sendresults.php" id="email" method="post"> it will not work. Assuming you created all those new files in the same folder, then you should have: <form action="sendresults.php" id="email" method="post"> and <form method="post" action="emailIt.php">

OK, but did you change the action attribute on the <form> . If you left <form action="php/sendresults.php" id="email" method="post"> it will not work. Assuming you created all those new files in the same folder, then you should have: <form action="sendresults.php" id="email" method="post"> and <form method="post" action="emailIt.php">

these are the files I have made

I didnt get what you meant at first. I tried changing it and it worked the way you described it. What it seems to do is post the info on emailit.php but it doesnt save the info. I still dont get why we need the whole sendresults.php page with the second form.

Thank you so much! If it will help I can make a pdf file of how I am trying to get it to work.

why would we make the person fill out another form?...Im just really lost, why there are 3 pages for this code?

Your ORIGINAL problem description was:

I have a form on page 2 and I want the information submitted on the form to be sent to page 3. How do i do that?

So YOU made it sound like there are THREE pages involved.

Your ORIGINAL problem description was:

So YOU made it sound like there are THREE pages involved.

sorry about that, it was my fault. I should have been clearer.

OK, then basically all you need is file1.php and emailIt.php. Just make sure that in file1.php you use <form action="emailIt.php" method="post">

OK, then basically all you need is file1.php and emailIt.php. Just make sure that in file1.php you use <form action="emailIt.php" method="post">

and it will save the info submitted so it can be viewed anytime? The php file cane have the same things in it that an html file can have?

and it will save the info submitted so it can be viewed anytime?

No. None of what I posted will save the data permanently. It saves it only for the duration of the Session - meaning, while the browser remains opened AND while the user remains making request with your site for X number of minutes, where X depends on your server configuration.

To save the data permanently, you will need a database OR save it to a file. I suggest you read PHP tutorials before attempting this.

No. None of what I posted will save the data permanently. It saves it only for the duration of the Session - meaning, while the browser remains opened AND while the user remains making request with your site for X number of minutes, where X depends on your server configuration.

To save the data permanently, you will need a database OR save it to a file. I suggest you read PHP tutorials before attempting this.

ok thanks Im trying to read some of the stuff from w3schools but they seem to only teach about what you tried helping me do. Do you know any sites I can look into or should I just google permantly posting data php?

What EXACTLY are you trying to do?

What i am trying to do is use the form as a way of sending a message. The person can type their message press submit and their message will be sent and saved on a different page. Its the same concept as what we are doing on this site. Instead of this message being saved to this page it would be saved to another page.

Try:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form method="post" action="processIt.php">
  <div><label for="">Name</label><input type="" name="Hi_my_name_is_" value="" /></div>
  <div><label for="">Email</label><input type="" name="Email" value="" /></div>
  <div><label for="">Phone Number</label><input type="" name="Phone_Number" value="" /></div>
  <div><input type="Submit" name="_SendIt" value="Submit"/></div>
</form> 
</body>
</html>


processIt.php
<?php
//specify the name of the file where you will be saving the data
$filename=getcwd().'/data.txt';

//check to see if anything was posted
if( isset($_POST) && !empty($_POST) )
{
	//if you make it here, then something was submitted via post, which would
	//be contained in the $_POST array
	
	//here I am "adding" the timestamp to the $_POST array so that it will be recorded
	//in the textfile along with the posted data
	$_POST['timestamp']=date('Y-m-d H:i:s');

	//here I am iterating over the contents of the $_POST array
	foreach($_POST as $fieldName=>$fieldValue)
	{
		//if you look at the HTML form I gave you, the Submit button has
		// name="_SendIt". You most likely are NOT interested in saving the value
		//of the button, since this will be the same always. So you can check to see if
		//the value is "_SendIt" and choose to skipt it, but a more general solution
		//would be to prefix all the field that you do NOT want saved with an underscore
		//and then filter out ANY field that starts with an underscore, which is what 
		//the following is doing.
		
		//this locates the fields whose name starts with an underscore
		if( '_'==$fieldName[0] )
		{
			//this removes it from $_POST
			unset($_POST[$fieldName]);
		}
		else
		{
			//if the fieldName contain any underscores in between, change them to spaces
			//so <input name="Hi_my_name_is_"> should ultimately result in
			//"Hi my name is " in the file
			if(preg_match('#_#',$fieldName))
			{
				$temp=str_replace('_',' ',$fieldName);
				$_POST[$temp]=$fieldValue;
				unset($fieldName);
			}
		}
	}

	//at this point $_POST contains only the info you DO want. So use implode()
	//to convert it to a string. I am also adding a couple of extra newlines at the end
	//PHP_EOL is the php constant for new line on the sever's OS
	$data=implode(PHP_EOL,$_POST) . PHP_EOL . PHP_EOL;

	//here you save the newly posted data to the file
	file_put_contents($filename,$data,FILE_APPEND);

}

//here you read whatever is in the file:
echo nl2br(htmlentities(file_get_contents($filename),ENT_QUOTES));
?>

hi, just use this code.It is really simple.
Let us consider the page1 contains a input field "name"

page1.php

<form id="form1" action="page2.php" method="post">
<input type="text" name="name"/>
</form>

page2.php

<?php echo $_POST['name'];?>

hi, just use this code.It is really simple.
Let us consider the page1 contains a input field "name"

page1.php

<form id="form1" action="page2.php" method="post">
<input type="text" name="name"/>
</form>

page2.php

<?php echo $_POST['name'];?>

thanks but If i remember correctly that does not save the messages. I will in an hour or so try hielo's code.

Thanks!

On my previous post, replace line 69 with the following: $data=implode(PHP_EOL, array_combine( array_keys($_POST), $_POST) . PHP_EOL . PHP_EOL;

On my previous post, replace line 69 with the following: $data=implode(PHP_EOL, array_combine( array_keys($_POST), $_POST) . PHP_EOL . PHP_EOL;

Thank you so much!

Now I just need to change it to one form and to change the order of how things are saved. To make it send and save to the php file without being forwarded to it and then save it to work for my website.

Thank You!

I have a form on page 2 and I want the information submitted on the form to be sent to page 3. How do i do that?

Via passing your get/post values from your form from page 1 to page 2 forms

Example:

page 1

<form action="page2.php" method="get">
<input type="text" name="page1value" value="testvalue" />
</form>

page2

<form action="page3.php">
<input type="text" name="recipientfield" value="<? echo $_GET['page1value']; ?>" />
</form>

Via passing your get/post values from your form from page 1 to page 2 forms

Example:

page 1

<form action="page2.php" method="get">
<input type="text" name="page1value" value="testvalue" />
</form>

page2

<form action="page3.php">
<input type="text" name="recipientfield" value="<? echo $_GET['page1value']; ?>" />
</form>

thanks. heiro's codes are 98% exactly what I need for my website. With a few tweaks and changing file names it should be exactly what I want to do.

Thanks Hielo!

I feel bad that I may not use it because I'm having little luck so far getting it to change.

On my previous post, replace line 69 with the following: $data=implode(PHP_EOL, array_combine( array_keys($_POST), $_POST) . PHP_EOL . PHP_EOL;

The best I was able to do is keep the form you made with the 3 fields, sending them to a blank page (the php file you wrote the code for) and it saving the message to data.txt.

I know im asking too much and you have done way more than enough already. If i post my files up would you be able to help me some more?

Thanks!

...To make it send and save to the php file without being forwarded to it and then save it to work for my website.

I don't quite understand what you are saying. Can you be more clear?

I don't quite understand what you are saying. Can you be more clear?

Explanation if you look at the files i uploaded. On contact.html the form I want to be submitted to password.html. I don't want people to be redirected to password.html, only the message. Password.html will be password protected by password.php. I dont want people to be redirected to a page with only a username and password that only I know.

So basically the content from contact.html will be sent to password.html and the user will stay on contact.html. The messages will be sent and stored on password.html( and the *.txt file.)

I hope this will help clarify what I wanted to do.

Just wondering, is what I wanted to do possible?
Having the messages saved and also apply attributes from a css file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.