PHP - How to send values in Multiple pages Form - php

So, I was working on a PHP Application Center (Form), and I'm stuck at something.
I have 3 pages of form and 1 sumbit page. All are in .php extention. So, I want to take the <input name="name" type="text" placeholder="Name" required> from page one and:
<label>About yourself (Atleast 50 Words): <span>*</span></label><br />
<textarea name="yourself" placeholder="About yourself (Atleast 50 Words)" ></textarea> to the submit page.
I can take the values of Textarea & Textbox from page 3 to submit page through <?php echo $_POST['somethinghere']?>. But I can't take values from the First and second page.
Here are links to my php codes with style.css (pastebin.com):- http://pastebin.com/81vgHh5H

I had a look at the Pastebin and I think I can see the problem. You're setting the Session data on the same page that they're filling out (so you're assigning those fields to the session before they've even been filled in). You need to do that on page 2, not page 1. So your pattern is thus:
Show page 1 form.
User fills it out and hits submit.
Send user to page 2.
Page 2 grabs the information posted from Page 1, stores it in the session.
Page 2 shows the second form page.
User fills out page 2 and hits submit.
Send user to page 3.
Page 3 grabs the information posted from Page 2, stores it in the session.
And so on...
By the time you get to the last page, your session will contain all the information from the form and you can then process it however you need to.
Example:
Page 1:
<form action="page2.php">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
$_SESSION['page1text'] = $_POST['page1text'];
?>
<form action="page3.php">
<input type="text" name="page2text"/>
<input type="submit"/>
</form>
Page 3:
<?php
$_SESSION['page2text'] = $_POST['page2text'];
?>
<form action="page4.php">
<input type="text" name="page3text"/>
<input type="submit"/>
</form>
Page 4:
<?php
$_SESSION['page3text'] = $_POST['page3text'];
?>
<ul>
<li>Page 1: <?php echo $_SESSION['page1text'] ?></li>
<li>Page 2: <?php echo $_SESSION['page2text'] ?></li>
<li>Page 3: <?php echo $_SESSION['page3text'] ?></li>
</ul>
As you can see, page 4 winds up with access to all of the information from the previous pages.
Edit: This answer previously read: I would recommend having a model that contains all of the form's properties. As they fill out each page, populate the model with the data the user has entered and then when they successfully complete the form you can destroy the session data.

You can store them in session like, pass your data from page one to page two and in page two do following
$_SESSION['name'] = $_POST['name'];
Again, pass your data from page two to page three and in page three do following
$_SESSION['yourself'] = $_POST['yourself'];
This will set your name and yourself data in session and will be accessible from any page. Do not forget to include session_start() at the top of your page if that page uses session.
Now, you can access those session variable from submit page like,
echo $_SESSION['name'];
echo $_SESSION['yourself'];

Related

Using PHP to change pages content

I have a few pages, PAGE 1 is an input form that allows accesed users to change PAGE 2/3 etc.... I am hoping to use PHP to do this but im not too PHP savvy.
Scenario
Page 1 has two drop down menus that are made in HTML (master page) the first has the persons name "Mike" & "Jim" and the second contains a score from 0 to 10. I want that if i select mike and 7 and then click submit, it will update page 2 (mikes page) and display "Mike" & "7" and then if i click "Jim" & "3" it will update jims page to display Jim and 3
How will i most efficiently go about doing this? preferably simple PHP if its possible.
All these pages + scripts will be on a server & i know jscript only is client side meaning it cannot change another page.
Any help is greatly appreciated
you can achieve this by creating sessions. for example, there i have taken 2 pages and sending data from input field of one page to another
page1.php
<?php
session_start(); //this must be the first line in php page
$_SESSION['variable']=$_POST['anything']; // this will save value from input tag in form in session.
header( "Location: page2.php" );
?>
<html>
<head>
<title>Session</title>
</head>
<body>
Welcome to page 1.
<br/>
<br/>
<form method="post" action="page1.php"> // this will call page2.php on button click
Type anything
<input type="text" name="anything" />
<input type="submit" value="Send value to page 2" />
</form>
</body>
</html>
page2.php
<?php
session_start();
echo $_SESSION['variable'];
?>

submit a form with a link from another page

I have created 3 php pages. The 1st includes the following form:
<form method="POST" name="go" action="search_form_all.php" >
<input name="value" type="text" id="search_form_1" size="65" placeholder="enter name"/>
<input type="submit" value="" name="submit" />
</form>
The 2nd page called "search_form_all.php" includes the php codes that displays the results of the above form. So if the user type a name in form and press submit, the "search_form_all.php" displays all names from my database according to what the user inserts in the search form.
All I want is to have in my 3rd php page a link, that when the user press it to execute the form in 1st page. For example if I enter a name in search form like "john", then I want to be able to go to my third page and press the link, the form to be executed and to return all names "john" from my database. Is this possible?
Yes, this is possible with session variables. An example:
<?php
session_start();
if(isset($_SESSION['views'])) {
$_SESSION['views']=$_SESSION['views'];
} else {
$_SESSION['views']=1;
}
echo "Views=". $_SESSION['views'];
?>
You can also refer to Document Link
Let me know if you need more help.

Passing variable to third page

MY Problem is that i have a link on a PAGE 1 with a variable associated with it
for ex
Spring
when user click this link i forward him to an html page Say PAGE 2 containing registration form when he fills form and click submit the details of form the data he inserted is grabbed by another php page Say PAGE 3 with request.
What i want is that the Variable associated with the link (Spring) on Page 1 may also be passed to this third page where the form data and the variable associated with link on page 1 may be processed
What you have to do is take the passed data from the link (Spring) and keep the in the hidden field as below. You will need php $_GET array for this and your page should have .php extension.
<input type="hidden" name="Spring" value="Spring value">
<input type="hidden" name="Spring2" value="Spring value 2">
Keep the hidden input fields within your registration form. When the registration form is submitted these hidden filed will be submitted along with the form. Then you can retrive them as you are now retrieving the registation values.
change your registration page extension to .php and then inside form include an hidden field like below
<input type="hidden" name="family" id="family" value="<?php echo $_GET["family"];?>">
Take one hiddne field in registration form such as
<input type="hidden" name="passed_var" value="<?php echo $_GET['family']; ?>" />
You may wish to look into Sessions in order to this
Sessions are Superglobals that can be activated by using the PHP
session_start()
command.
You can then store and access the SESSION variables like this:
PAGE1.PHP
<?php
session_start();
$_SESSION['foo'] = "bar";
?>
PAGE2.PHP
<?php
session_start()
$foobar = $_SESSION['foo'];
echo $foobar;
//echo's: "bar";
?>
You can read more about Sessions here and here

How to correctly navigate to same and different page

Below is my code for a simple form in the create_session.php page. I am using the forma action method to navigate the user to the "QandATable.php" page when the user submits the form. But what I want is that if the user types in the number 1 in the number of sessions textbox, then navigate to the QandATable.php page wheh the user submits the form, else if it is any other number, then when the user submits the form, I want it to go back to the "create_session.php" (Back to its own page). Imagine it like you click on the submit button and it refreshes the page so it goes back to being a blank form, thats what I want to do if the number of sessions textbox contains a number which is bigger than '1'. How can this be done? I am using php and jquery code as well as basic html.
Thank You
<form action="QandATable.php" method="post" id="sessionForm">
<p><strong>Number of Sessions you Require:</strong> <input type="text" id="sessionNo" name="sessionNum" onkeypress="return isNumberKey(event)" maxlength="5" /></p>
<p><input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion" onClick="myClickHandler(); return false;"/></p> <!-- Prepare Questions here-->
</form>
At the very top of the QandATable.php script add:
if ($_POST['sessionNum']!=1) {
header("Location: create_session.php");
exit();
}
But as others have mentioned in the comments, you might as well just make the form submit to the create_session.php page and if it's successful then redirect to the next page.

Post information from 1st page to 3rd page

I need to consider something in the future after I finish my application. What is going to happen is that a teacher will need to first login (login page is page 1), after login the page will go straight to a menu where the teacher selects a hyperlink link to open up a page (hyperlink menu is page 2), On third page I want a message displaying welcome to the teacher who has accessed the page (page 3 is welcome page).
Is there a way to get the teacher username from page 1 and displaying it on page 3 is what I am asking?
Below is coding and example:
Page 1: InputTest.php
<body>
<form action="InputTest2.php" method="post">
<p>Please enter your name</p><p><input type="text" name="user" /></p>
<p><input type="submit" value="Send" /></p>
</form>
<!-- The above allows a name to be entered and submitted to "InputTest2" by clicking on send button -->
</body>
Page 2: InputTest2.php
<body>
<p>Welcome</p>
<p>...</p>
<p>...</p>
</body>
Page 3: InputTest3.php
<body>
<?php print "Welcome <b>".$_POST['user']."</b><br/>\n"; ?>
</body>
in your InputTest2.php
add the following code in the start of the page
session_start();
$_SESSION['usernameLogged'] = $_POST['user'];
and then you can print it like this
also you should start a session
<?php
session_start();
print "Welcome <b>".$_SESSION['usernameLogged']."</b><br/>\n";
?>
Save it in the $_SESSION superglobal.
$_SESSION['user'] = $_POST['user'];
Then $_SESSION['user'] will be available on the third page.
Note: you will need to have session_start(); on both pages in order to store/retrieve session data like that.
Possible duplicate of: php: Save the entire $_POST variable in the session

Categories