Cookies/javascript to retrieve data in forms

apps

#1

I have a form, that the user fills out email, name and phone number. This form calls itself and sends the data to us, then goes to a second form, where the user can fill out more information if they want.

I was trying to figure out a way to pass data between individual forms with php code…but somewhat got stuck due to lack of php knowledge.

Does anyone know of a way to store and retrieve data between forms using cookies and javascript…I know if the user has javascript blocked it would fail. But would be an alternative until I get more skills in php. Or is there a better tool out there?

thanks,
barbara


#2

You can use session variables, get variables, and post varibles.

If you are passing data from lots of forms to each other, the easiest way is to use session variables.
At the top of every page, use
session_start();
You can now use $_SESSION[‘variable_name’] to store and retrieve information.

For example, if you have a simple form in index1.php

and in index2.php

<?php session_start(); $name = $_POST['first_name']; //get name from form if(issset($_SESSION['name'])){ //see if we have already put the person's name in the session varible echo "You have already been here " . $_SESSION['name']; } else{ echo "Welcome to the site " . $name; $_SESSION['name'] = $name; //create a variable called name in the session and give it the value the user put in } ?>

This is just a very simple example. Go to index1.php and input a name, press submit, and you should see “Welcome to the site” message. Refresh the page and it should change…

If you need more help, you can email me at henderson_luke@hotmail.com.


#3

cool, I got that basic form to work, and then did the <?php echo $name;?> to get the name in the form field…now to see if I can get it to work on my form!

Thanks : )

PS, I did have to change issset to isset…


#4

okay, now that won’t work.

I think it is because I am not posting my form to index2.php

rather, I am posting the form in index.php to

and redirecting to index2.php

Form1 sends the information in the form in index1.php to me, then the user gets redirected to index2.php in case they want to send additional information.

I think I need to use javascript/cookies to get the name, email, phone number from the form in index.php to propagate the fields in index2.php

of course, like I said…I think…I actually don’t know! I have been googling myself to a headache this morning on this…

Thanks for your help though, I did learn something new!!!

Barbara


#5

unless I need to use

session_write_close();

on the first index.php?


#6

What I posted was only an example.

Your action php script just seems far too long…
You can’t redirect form information. You can post it to one page only and then preform your checks/validation/storage, and then redirect leaving your information behind.
You should go to the page that you are submitting your information to and then try to adapt the code I posted to make it work for you.

Oh I think I understand now…
You get a ‘copy’ of the information from form1, and if the user needs to add more info, they can do that on another form?


#7

yes…that is what happens.

if I use the

<?php $_SESSION["Message"] = "The task was completed."; session_write_close(); header('Location:' . $_SERVER["PHP_SELF"]); ?>

of course somehow modify it for myself, do you think that it could work? I am now googling session_write_close(); !!


#8

I really don’t know. I need context/example before I can help more.


#9

Why not just use

to make it pass the information to itself? Be easier than trying to use PHP. :slight_smile: (Then you can use
$var = $_POST[var]
to get the information back out of it once you’re back on your original page again.