Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

storing a TextBox value in php array which is then stored in a session 1

Status
Not open for further replies.

swathip

Programmer
Oct 10, 2009
17
US
Hi,

I am totally new to coding and i would need some help regarding this question of mine. the question is that,

I have around 5 html pages.I am trying to walk the user through each phase and thats the reason i have diffent page for entering the info.Each html page has certain text fields.For ex

Page1:lastname,firstname,mi...
Page2:address,phone,city,state,zip
Page3:date of join,time of joining,person who referred you
Page4:insurance info,insurance numberPage
5:directions to the place, and so on

Now the thing is that, i need to store the field values using sessions in php.

Suppose the user goes from page 1 to page 2(after filling all the details), and then if he wants to visit page 1,he should have all the fields populated with the data which he sent before(these details can be changed now).I want to store all the fields in a PHP array and then store the array using sessions.

My question is that how do we set the value of the php variable to the Value in the Textbox(the textbox will have the value entered by the user) and how do i design the array, and store it in the session.

Also can i use the same session variables across all the pages,so that i could save all the details entered by one particular in the array(which is then stored in the session).

Any help is greatly appreciated.

I would like to see some sample code about how this could be done(including both html and php)
 
The basic of it is to get each form, and submit it to a script, most likely the script exists in the following forms page. so that it takes the values from the form, and sticks them in your array, and then into the session variable.

Code:
$myarray['firstname']=$_POST['firstnametextbox'];
$myarray['lastname']=$_POST['lastnametextbox'];
...
session_start();
$_SESSION['mysession']=$myarray;

Then when you need to use them:

Code:
<?PHP
session_start();
?>
<form action="formpart2.php" method=POST>
<input type="text" value="<?PHP if(isset($_SESSION['mysession']['firstname'])){echo $_SESSION['mysession']['firstname']; }?>" name="firstnametextbox">

...

And so on and so forth for the rest of the forms inputs.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hello Vacunita,

Thank you very much ,but i also wanted to find out why is it showing the " value="<?PHP if(isset($_SESSION['information']['lastname'])){echo $_SESSION['information']['lastname']; }?> " in the textbox section whenever i open the page in interner explorer.

I have saved the file in php.
 
Looks like the PHP is not getting parsed.

Is any other bit of PHP getting parsed o.k? PHP not inside the input value.


If you run a page with just:
Code:
<?PHP
phpinfo();

?>
Does it run o.k?


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
hey Vacunita,thank you very much.your suggestion worked.I had one more question too, for my original question

i filled the details in page1 and visit page2 by clicking the [red]continue[/red]link.but when i visit page1 by clicking the [red]goback[/red] link the data which i prev filled is lost.I have to store the data.

I wrote the code just the way you mentioned.do you think i am missing something.

Anyways i am reproducing part of that code

page1.php
[red]
session_start();
include("script.php");
<html>
<body>
<form name="addReferral" method="post" action="script.php">
<input type="text" name="rlastname" value="<?PHP if(isset($_SESSION['information']['lastname'])){echo $_SESSION['information']['lastname']; }?>" >

<a href="page2.php">Continue >> </a>
<body>
</html>
</form>
[/red]

page2.php
[red]
session_start();
include("script.php");
<html>
<body>
<form name="addReferral2" method="post" action="script.php">
<input type="text" name="raddress" value="<?PHP if(isset($_SESSION['information']['address'])){echo $_SESSION['information']['address']; }?>" >

<a href="page1.php">Back</a>
<a href="page3.php">Continue >> </a>
</form>
</body>
</html>
[/red]

script.php
[red]
<? $referralinfo=new array();
$referralinfo['lastname']=$_POST['rlastname'];
$referralinfo['address']=$_POST['raddress'];
session_start();
$_session['information']=$referralinfo;
?>
[/red]
 
You aren't submitting the form to the script.php. Your link just moves the browser to page2.php but no info is stored because its no submitted there.


You need to use an actual submit button, and submit the form, so that the values form the textboxes get transferred over to the $_POST array, and you can actually store the values in the sessions.

If you want to use script.php as the main processor, then you'll have to submit the form to it, and then re-direct form there to the next page.

You can use hidden fields to identify the page that submitted the form so you know what values to add to the SESSION, and to what page redirect next.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
hey vacunita,

thank you very much.
also could you please let me know how do I
o I start to do the processing in "script.ph
p" in the way you mentioned using the hidd
dden fields.any sample scripts would be really helpful
really helpful as I really don't know how t
o do this.

thanks again
 
As an example:

Code:
session_start();
<form name="addReferral" method="post" action="script.php">
    <input type="text" name="rlastname" value="<?PHP if(isset($_SESSION['information']['lastname'])){echo $_SESSION['information']['lastname']; }?>" >
[red]<input type="hidden" name="nextpage" value="[blue]page2.php[/blue]">[/red]
[green]<input type=submit name="send" value="Continue to Page 2>>">[/green]
</form>
Code:
<?PHP
session_start();
?>
<form name="addReferral2" method="post" action="script.php">
    <input type="text" name="raddress" value="<?PHP if(isset($_SESSION['information']['address'])){echo $_SESSION['information']['address']; }?>" >
[red]<input type="hidden" name="nextpage" value="[blue]page3.php[/blue]">[/red]
[green]<input type="submit" name="send" value="Continue to Page 3>>">;[/green]
</form>

Code:
session_start();
if(isset($_POST['send'])){
if($_POST['nextpage']=='page2.php'){
$_SESSION['info']['firstname']==$_POST['firstname'];
$_SESSION['info']['lastname']==$_POST['lastname'];

header("Location:page2.php");
}
if($_POST['nextpage']=='page3.php'){
$_SESSION['info']['raddress']==$_POST['raddress'];

header("Location:page3.php");
}
}


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thank you very much vacunita,

i understood the concept now.but i have one more question.

I introduced [red]<a href="page1.php">back</a>[/red] in the script in page2.php inorder to go back to page1.php.now when i click this link the data which i filled seems to be lost.how do i fix this problem..

thanks in advance
 
Again:
Vacunita said:
You aren't submitting the form to the script.php. Your link just moves the browser to page1.php but no info is stored because its no submitted there.

Your link is not submitting the form. Script.php is the one storing the information when the form is submitted. The link just moves the browser without storing any information in the SESSION because script.php is never executed.

If you really must have a text link then you'll have to use Javascript or AJAX to force the submissions to script.php to store your values in the SESSION and then redirect back to page 1.

I'm not that good with AJAX but a JS option would look kind of like this:

Code:
<a href="#" onClick="document.forms.[red]addReferral2[/red].action='[green]script.php?[blue]topage[/blue]=1&source=2&send=true[/green]'; document.forms.addReferral2.submit();">back</a>

Code:
session_start();
if(isset($_POST['send'])[blue]||isset($_GET['send'])){
if($_POST['nextpage']=='page2.php'){
$_SESSION['info']['firstname']==$_POST['firstname'];
$_SESSION['info']['lastname']==$_POST['lastname'];

header("Location:page2.php");
}

if(($_POST['nextpage']=='page3.php')[blue]||($_GET['source']==2)[/blue]){
$_SESSION['info']['raddress']==$_POST['raddress'];
if($_GET['topage']==1){
header("Location:page1.php");
}
else{
header("Location:page2.php");
}
}
}










----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The other viable option for this sort of form steps are using one big form but each "page" is in a different div which you swap in and out using javascript which then when you submit it, it validates it or use JS / AJAX as you go along to validate the fields.

Just my two cents =]

Regards,

Martin

Computing Design And Services:
 
hey Vacunita,and thanks martin

i had a question.in the above process i introduced a back button to see if its working.is it a preferred method or should i avoid it.

also when [red]i click the back button on page2(after filling the details) i goto page1.here i fill up the details in page1 and then hit hit continue(submit button) why is it that the fields which i previously filled in page2 are empty.[/red]

is this related to something to back button or is it how it normally goes
 
Sorry, I had to leave for the weekend.

Anyway, what do you have now in script<b>2</b>.php why the change from script.php?

As far as the links go, as I said before, you need to store the information in the form for ti to be available when you return to it. A link simply redirects the browser to another page bu since ti doesn't go through script.php the datais not stored in the session variable.

My example of a link used javascript to force the form to be submitted so the data could be stored. It is then script.php the one that redirects you back to the first page.

Script.php will be the hub for every action, so that all information is stored in the session.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top