I have an exam and what I am looking to do use session to save every value, whether it's textboxes or selects or radio button groups choices. So if the user submits the form and misses something, coming back to the form from a different page, it keeps all the value that was entered and auto fills it in. I would also like to keep the session alive for 2 hours. I know I can use something of the following:
FORM PAGE:
<form method=post action="submit.php">
<input type=text value="" name="first" />
<input type=radio value="A" name="acct" /> A
<input type=radio value="B" name="acct" /> B
<input type=radio value="A" name="birt" /> A
<input type=radio value="B" name="birt" /> B
</form>
On submission of the form in the submit.php page I have this:
<?php
session_start();
$_SESSION['textbox1'] = isset($_POST[first]) ? $_POST[first] : null;
$_SESSION['radiogroup1'] = isset($_POST[acct]) ? $_POST[acct] : null;
$_SESSION['radiogroup2'] = isset($_POST[birt]) ? $_POST[birt] : null;
** PLUS OTHER CODES I USE **
?>
The above should start a session and save those values so no matter if I exit the page or not, it should store it. Correct?
Now questions I have are:
How do I make the session alive for ONLY 2 hours
Let's say I exit the page or go to another page and I come back to the FORM page, how do I auto enter the value which was originally saved in the session?
In WHAT SHOULD I ENTER HERE How do I accomplish, if the user selected a value fill it in otherwise leave it alone? SOLVED
I really appreciate the help.
1st answer:
SESSIONS remain alive until the user closes his/her browser so you need to make a cookie like this:
setcookie("TestCookie", $value, time()+(3600*2)); //save for 2 hours
2nd answer:
Use the array : $_COOKIE['session_name']
3rd answer:
Just use null there.
Simple :)
To add the submitted data to the form, jut output the session to the value of all the inputs:
<input type=text value="<?php echo $_SESSION['textbox1'];?>" name="first" />
To autofill do something like:
<input type=radio value="A" name="acct" <?php echo ($_SESSION['acct'] == A) ? "checked" : "" ?>/>
Related
Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.
I have a question related to php. If anyone have an idea,please share with me. (I am a beginner in php).
I received a button value from an HTML page and displayed corresponding figure in the second page using code "param1.php". In the same program itself ( ie, "param1.php"), there is a set of radio box and i need to receive the radio value using another php program. But here i have confusion how to receive the radio value using another php program say "param2.php". also how the page redirect (to the php program where i receivvee the radio value) on selecting a radio button.
Thanks
I attach the "param1.php" below,
<!DOCTYPE html>
<html>
<body>
<br>
<?php
$data = $_POST['btn'];
$data2=$data.".png";
?>
<img src="<?php echo $data2;?>">
<h3>SCALE</h3>
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
</body>
</html>
No form tags
There are no form tags, so your radio tags won't get POST'd to param2.php
<form action="param2.php" method="POST">
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
<input type="submit" value="Process" />
</form>
Incorrect $_POST key
Now, in your param2.php file, you will be able to get the value of POST['group1']. In your code you're using the wrong key. btn doesn't exist within $_POST (in the code you've given anyway). The radio button name is group1, so access it as such;
$data = $_POST['group1'];
The value of $data will either be 4,10,15 or 5,9,13. Ensure you've got an image named 4,9,13.png and 4,10,15.png else your image won't show. Because they're comma separated, I'm going to assume these are unique file names. So;
foreach( explode(",", $_POST['group1']) as $file) {
echo "<img src='". $file .".png' />";
}
Also, ensure you do some checks on the posted data to validate the user input
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 the following form:
<form name='progObj_form' method='POST' enctype='multipart/form-data' action='processpage.php'>
<select name='manageObj[]' id='objectives' multiple="multiple">
<option value=0>there are no objectives for this program</option>
</select><br />
<a href='#nogo' onclick="delItem(objectives,0,'objEditBtn')" class='shiftOpt'>delete selected</a><br />
<input name='newObjective' type='text' id='newObjective'/>
<input name='addNew' type='button' onclick="AddItem(newObjective.value,6,'objectives','objEditBtn');" value='add objective'/>
<input name="passProgID" type="hidden" value="1" /><br />
<input name="objectiveEdit" id="objEditBtn" type="submit" value="save changes" disabled=disabled/>
</form>
that allows data (objectives in this case) to be added and deleted from a list box. That all works well but for some reason the updated listbox values aren't being passed to the process page.
I'm catching the data like so (simplified):
if (isset($_POST['objectiveEdit'])){
$progID=$_POST['passProgID'];
for ($v=1;$v<count($_POST['manageObj']);$v++){
$value=$_POST['manageObj'][$v];
$sqlObj="INSERT INTO progObjective (progID,objective,objectiveOrder) VALUES ($progID,$value,$v)";
$result = mssql_query($sqlObj,$linkProbation) or die('Query failed: '.$sqlObj);
}//end for ($a=0;$a<count($_POST['manageObj']);$a++)
$objMsg=print_r($_POST['manageObj']).$sqlObj;
}//end if (isset($_POST['objectiveEdit'])
For $objMsg, I get a response of 1 and the array doesn't print because ostensibly, it's empty which means that it also doesn't enter the for loop.
I can include the javascript as well but started with just this for simplicity since I'm probably just overlooking something obvious?!
Option elements are never sent to the server. Only the value of the selected option will be sent.
In your case, something like manageObj=x will be sent to the server, where x is the value of the option element that is selected by the user. It is possible that you misunderstand the [] when it's used in a name attribute.
You should try to find a different method if you want to send the objectives created by the user to the server. You could store the data in hidden inputs for example.
So I finally figured it out! There's no array because there are no selections made! I updated the submit button to call a SELECT ALL function and now it's all good.
<input name="objectiveEdit" id="objEditBtn" type="submit" value="save changes" onclick="selectAll('objectives',true)" />
name='manageObj[]' - shouldn't that be name='manageObj' ?
I'm trying to make a simple survey in php
I have a set of radio buttons on a page called sja.php that sends its to sjamail.php page
the problem is that when I go to get
$answer = $_POST['ans'];
I can't seen to do anything like
echo "$answer";
but if I were to throw some logic at it
like
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
It will display correct or incorrect (edit: The if/else works correctly and will display the correct answer )
so why is it I can't access the value of the radio button "ans" as a string?
http://www.markonsolutions.com/sja.php
print_r($_POST); will return Array ( [ans] => )
Perhaps the value is something other than text.
Try
var_dump($answer);
or
print_r($answer, TRUE);
Your page works correctly if you select any of the first 4 radio buttons (ans1/2/3/4). But the rest of the radio buttons next to all those images have blank values, which would explain why your posted value is empty if you selected any of those to test with.
You need to make sure that the field in HTML has...
<input type="radio" name="ans" value="ans1" />
<input type="radio" name="ans" value="ans2" />
Also make sure your form method is POST
I had a similar problem with the following:
<input name="03 - Gender" type="radio" value="Masculino"/>Male<br/>
<input name="03 - Gender" type="radio" value="Femenino" required="required"/>Female <br/>
<input type="hidden" name="03 - Gender" value=""/>
but when I removed the third input line (the hidden one) the problem desapeared.
Try this:
$answer = (string)$_POST["ans"];
echo $answer;
You must convert $_POST["ans"] to string.