I am currently building a multiple pages form using $_SESSION, everything has been working fine so far. Yet, even though I am able to retrieve all the data at the end of the 6 pages, it seems that it is not possible to go back in the form if the user made a mistake.
Let's say he is at page 5 and he realizes that he needs to go back to page 3 to change something, he won't be able to do so. Firefox gives this error message :
Document Expired
This document is no longer available.
The requested document is not available in Firefox's cache.As a security precaution, Firefox does not automatically re-request sensitive documents.Click Try Again to re-request the document from the website.
This is what my forms look like, this one is Form-1.php :
<form action="Form-2.php" method="post">
<fieldset>
<legend>Information</legend>
<p>
<label for="name">Name</label>
<input id="name" type="text" name="name" required/>
</p>
<p>
<label for="age">Age</label>
<input id="age" type="number" name="age" placeholder="Ex. 1988" required/>
</p>
<p>
<input id="sex-m" type="radio" name="sex" value="Male" required/>
<label for="sex-m">Male</label>
<input id="sex-f" type="radio" name="sex" value="Female" required/>
<label for="sex-f">Female</label>
</p>
</fieldset>
<input type="submit" value="Next page"/>
</form>
I am using SESSION to retrieve data from the previous form, this one is from Form-2.php :
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
And so on for the six pages.
It works fine, it retrieves all the values correctly, but it is not possible to go back when filling the form.
What am I doing wrong? How can I make sure the user will be able to go back if he makes a mistake?
Thanks for your help.
EDIT :
I ended up with the following code, solving half of my problem. Still from Form-2.php :
session_start();
if (isset($_SESSION['location']) && isset($_SESSION['job'])) {
$location = $_SESSION['location'];
$job = $_SESSION['job'];
} else {
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
$location = '';
$job = '';
}
Then I simply display the values in the fields with :
If text : <?php echo $location; ?>
If radio : <?php if(isset($job) && $job == "Yes"){echo "checked=\"checked\" ";} ?>
It works quite well, the different forms - if the values have been set - are pre-populated just fine.
My problem is, as you can see, if I go back and change a value, it won't be taken into consideration as I am ignoring POST if there is a corresponding value stored in SESSION.
I can't figure out how to make this work properly... Any hint?
Not the best solution, but does the job: Do not use back buttons, provide your own forward, backwards, "step" buttons. Then populate the form with the data stored in e.g. the session.
djot is right, you need a << back >> and << forward >> button, what you can do in functionality is if back button is clicked pass the values of the page/form as hidden input fields and populate the fields using the values in the hidden..
Related
I have three pages. One of which there is a list of texts the user can select. Upon clicking on one of the texts they will be redirected to another page by using:
<a href='second.php?text=whatever>Whatever</a>
A page where they will input the username they wish to send those texts to - using forms. I wish to proceed to the third page with those two variable - texts and username. I only manage to proceed to third page with username only.
I am getting third.php?username=inputtedUsername.
I want to get third.php?username=inputtedUsername&&text=whatever.
I am aware that I can do by storing the text to a SESSION on page two and than transfer it over to third page.
I wish to know if there is another secure way to do this - maybe something needed to be changed in the form action=thirdpage.php? I dont know. Thank you. ö.ö.
Solved: After reading comments and answer, the thing I need was type=hidden. It is now working on my part. Thanks everyone for helping me. :).
'second.php?text=whatever'? You can't just put whatever to the text, you are doing it wrong. Try this.
firstpage.php
<?php
$whatever = 'Tom & Jerry, 1 + 2 = 3';
echo '' . $whatever . '';
?>
secondpage.php
<form action="thirdpage.php" method="post">
<input type="text" name="username" value="" />
<input type="hidden" name="text" value="<?php echo base64_decode($_GET['text']); ?>" />
<input type="submit" value="Submit" />
</form>
thirdpage.php
<?php
echo 'Username: ' . $_POST['username'];
echo '<br />';
echo 'Text: ' . $_POST['text'];
?>
I have a normal form in a PHP page, I send data to the php page from another for using POST. The PHP page runs some scripts to update data to SQL but on that page I have a second form that needs to be completed with data from the initial form prior to updating the SQL.
$recipient_nr = $_REQUEST['recipient_nr'];
Here I draw the info from the first form
Now I want to use this in a new form on the current PHP page how do I state this in the new form
<input type="text" name="recipient_nr" id="recipient_nr" value=".$recipient_nr.">
This is what I am attempting but it is not working I know I have too many "'" xxx"'" in the lines but not sure how to remidy this
Do you generate the new form in PHP? If so, where is the code where you do that?
If this is some kind of ...?> <input type="..."...> <?php ... page generation then you'll need to echo that $recipient_nr into the PHP-generated response:
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?php echo $recipient_nr; ?>">
<?php
...
Or, if you have short echos turned on,
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?= $recipient_nr ?>">
<?php
...
use this:
<input type="text" name="recipient_nr" id="recipient_nr" value="<?php echo $recipient_nr; ?>">
Something like this:
$recipient_nr = array_key_exists('recipient_nr', $_REQUEST)
? (string) $_REQUEST['recipient_nr']
: 'default value or empty string';
And output it:
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?=$recipient_nr?>">
But if you want store this data for/between other pages you can use $_SESSION global array for save it.
I'm building a website in PHP and I'm trying to implement asynchonous behaviour on some occasions, in this case to load an HTML form into an overlay and making it visible. This works as intended, however I'm now testing everything considering existing data.
So I basically created a variables.php file that sets values to the $_SESSION global and was working from there. Everything was working as expected on index.php, but as soon as I click the overlay I notice the values aren't passing through to populate the form that was added.
I already poked google for a few hours to no avail. I've added echo var_dump($_SESSION); on the index.php file and the values are all there. However on the overlay it returns NULL. I've even include_once("loginForm.php") right in the middle of index.php and that gave me the values. So there's something I'm missing in order to get the values to apply to .load() elements.
Here's some code:
variables.php
//added values to the $_SESSION global for testing purposes
$_SESSION['email'] = 'john#john.com';
$_SESSION['password'] = 'johnny';
$_SESSION['name'] = 'John';
$_SESSION['surname'] = 'Smith';
$_SESSION['country'] = 'UK';
$_SESSION['phoneOption'] = 'Mobile';
$_SESSION['phone'] = '987654321';
header-login.php
//this form accepts an email to check ifExists() and decide what's next
//the input #preLoginEmail assumes the value correctly
<form action="header-login.php" name="preLoginForm" id="preLoginForm" method="post">
<div id="login-part2">
<table id="preLoginTable">
<tr>
<td colspan="2">
<input type="text" id="preLoginEmail" title="Email" name="test-email" tabindex="1" size="10" maxlength="60" placeholder="Email" value="'. $email .'" />
</td>
</tr>
<tr>
<td><a title="forgotten password" href="header-login.php" id="preLoginForgot">forgot password?</a></td>
<td><input type="submit" class="btn1" name="preLoginRegisterButton" id="preLoginRegisterButton" tabindex="1" value="Login / Register" /></td>
</tr>
</table>
</div>
echo var_dump($_SESSION);//works
</form>
onClickEvents.js
//this call retrieves the HTML correctly although the variables dont get assigned to the input's value
$( "#preLoginForm" ).submit(function( event ) {
event.preventDefault();
var $form = $( this ),
term = $form.find( "input[name='test-email']" ).val(),
url = $form.attr( "action" );
verifiedEmail = validateEmail(term);
if(verifiedEmail){
// Put the results in a div
$('#olContainer').load("../inc/loginForm.php");
overlayOn();
}
else {
$('.session-stat').css({ "background-color": "#A60000" });
}
});
loginForm.php
//when this form is loaded there are no values in the inputs and var_dump($_SESSION) returns NULL
<form id="loginForm" name="loginForm" method="post" action="booking.php">
//some blocks are static and created in plain html
<input name="email" type="text" class="dDown12" id="agentuser" size="20" maxlength="20" value="<?php echo $email; ?>" />
//others are php variables to make the if/else statement more readable
$countryBlock ='<input name="agentuser" type="text" class="dDown12" id="agentuser" size="20" maxlength="20" value="'. $country .'" />';
echo var_dump($_SESSION); //NULL
I kinda ran out of ways to figure out what's going wrong, and I just started learning about AJAX this week. If u need anything else just let me know in comments I'll try to be quick to edit. Thanks in advance.
#Fernando - I didn't know which way you decided to go, but if you have to use $_SESSION for this, include:
session_start();
at the beginning of each file you plan to use sessions on, before any content is rendered. Also, be careful to have a means for your users to overwrite their values, ie. with a post, so that once a value gets put in session, there is a way to change it and it doesn't keep overwriting the (new) value. I usually clear out my sessions on Page one of the form. You can do a
unset($_SESSION['test-email']);
...to unset the values. You can use a foreach loop here too.
A great site to compare the speed of loops in PHP is http://www.phpbench.com/ also.
Best of luck!
so if i fill all the fields and submit everything is great,once i do that again everything is ok,but when i press back i can see what was echoed before and once again back and then i can see the result from 1st time. i want that i cant press back or i can press back only once.
<form action="Alauris.php" method="POST">
question1 <input type="text" name="answer1"><br>
question2 <input type="text" name="answer2"><br>
question3 <input type="text" name="answer3"><br>
question4 <input type="text" name="answer4"><br>
question5 <input type="text" name="answer5"><br>
question6 <input type="text" name="answer6"><br>
question7 <input type="text" name="answer7"><br>
question8 <input type="text" name="answer8"><br>
<input type="submit" value="Make">
</form>
<?php
if(isset($_POST['answer1'])&&isset($_POST['answer2'])&&isset($_POST['answer3'])&&isset($_POST['answer4'])&&isset($_POST['answer5'])&&isset($_POST['answer6'])&&isset($_POST['answer7'])&&isset($_POST['answer8'])){
$answer1=$_POST['answer1'];
$answer2=$_POST['answer2'];
$answer3=$_POST['answer3'];
$answer4=$_POST['answer4'];
$answer5=$_POST['answer5'];
$answer6=$_POST['answer6'];
$answer7=$_POST['answer7'];
$answer8=$_POST['answer8'];
if(!empty($answer1)&&!empty($answer2)&&!empty($answer3)&&!empty($answer4)&&!empty($answer5)&&!empty($answer6)&&!empty($answer7)&&!empty($answer8)){
$content = "asdasda" .$answer1. '<br>'."asdas".'<br>'.$answer2."5t64356456a".'<br>'.$answer3. "asdasdasda".$answer4."5t643564".$answer5. "aasda".'<br>'.$answer6."5t64356456as".$answer7. "asdasdas45".$answer8."5t64356456a45";
echo $content;
}else{
echo "fill all fields,my friend!";
}
}
thanks !
Disabling the back button cannot be guaranteed to work (due to browser implementations), but it is discussed here: how to stop browser back button using javascript
There is no way to guarantee that the browser will not submit the same data twice (while loading, the client can press reload). Also, you can't ensure that the browser will not redisplay an old page to the user when pressing back.
If it is important not to process old data, you should design the app for idempotence. This can be achieved with SESSION variables or a database. On easy example is
if (isset($_SESSION['submitted']) {
doWork();
$_SESSION['submitted'] = 1;
}
This code will only run once and it wont hurt if the user reloads the page.
If you want to allow the user to run the action again (e.g. after the transaction is completed) you can just:
unset ($_SESSION['submitted'])
I have url variables that was submitted from a form from the previous page. So my url looks something like site.com/submitted.php?first_name=hello&last_name=bye.
Now I am using a link to keep my submitted variables while I go to my second page
<a href="secondPage.php?first_name=hello&last_name=bye>pageLink</a>
On this second page, it's basically a drop-down using the select tag with a submit button that generates a table from mysql server which then links back into the same page. It looks like:
<FORM ACTION="secondPage.php?first_name=hello&last_name=bye" METHOD="GET">
<select name='selectedOption' >
<option value="op1">option1</option>
<option value="op2">option2</option>
<option value="op3">option3</option>
</select>
<INPUT TYPE="SUBMIT" VALUE = "Search">
</FORM>
But lets say I choose option1 and submit, my url does not keep the variables first_name and last_name but it just replaces it :
secondPage.php?selectedOption=op1
instead of:
secondPage.php?first_name=hello&last_name=bye&selectedOption=op1
Any help is appreciated.
I'm not sure how you want to pass data around but here is a suggestion to get the values you want with php.
Change this:
<FORM ACTION="secondPage.php?first_name=hello&last_name=bye" METHOD="GET>
to this:
<form action="secondPage.php" METHOD="post">
Your code for secondPage.php coud look something like this:
// the 'if' statements aren't necessary. just an idea for simple server-side
// validation
if(isset($_REQUEST['selectedOption']) && $_REQUEST['selectedOption'] != ''){
$option = $_REQUEST['selectedOption'];
}
if(isset($_REQUEST['first_name']) && $_REQUEST['first_name'] != ''){
$fname = $_REQUEST['first_name'];
}
if(isset($_REQUEST['last_name']) && $_REQUEST['last_name'] != ''){
$lname = $_REQUEST['last_name'];
}
All the values you need to do whatever with are now in $option, $fname, and $lname
You say your new to php, welcome :), and don't ever trust user input. You should take some time and read about SQL Injection.
Hope some of this helps and good luck.
ON THE SECOND PAGE. In form make two hidden fields <input type="hidden" value="<?php echo $_REQUEST['first_name'] ?>" name="first_name"/> and
<input type="hidden" value=""<?php echo $_REQUEST['last_name'] ?>"" name="last_name"/>
my suggestion use post method
Basically, I've been working myself with something exactly like that, but I've found a great solution and a great approach in my own opinion.. let me give you some bit and piece of code as an example if you don't mind ^_^..
to be organised and clear what you did is : -
##**FIRST PAGE** : -
the url: - secondPage.php?first_name=hello&last_name=bye <--- this was the result..
Then you travelled to the 2nd page: - <-- result was still associated
##**SECOND PAGE** : -
$firstname = $_GET['first_name'];
$lastname= $_GET['last_name'];
<FORM ACTION="secondPage.php?first_name=<?php echo $firstname; ?>&last_name=<?php echo $lastname ?>" METHOD="POST">
<select name='selectedOption' >
<option value="op1">option1</option>
<option value="op2">option2</option>
<option value="op3">option3</option>
</select>
<INPUT TYPE="SUBMIT" name ="submit" VALUE = "Search">
</FORM>
##**THIRD PAGE** The page that the form is going to go to: -
$option = $_GET ['selectedOption'];
// you can also get the other data on this page through same method i've done on page two..