cookies/javascript to retrieve data in forms

cookies/javascript to retrieve data in forms

Posted by: gonewthewind
Posted on: 2005-02-03 06:20:00

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


Re: cookies/javascript to retrieve data in forms

Posted by: decswxaqz
Posted on: 2005-02-03 06:53:00

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
<form action="index2.php" method="post">
<input type="text" name="first_name">
<input type="submit" value="submit">
</form>

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.

Re: cookies/javascript to retrieve data in forms

Posted by: gonewthewind
Posted on: 2005-02-03 08:26:00

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...

Re: cookies/javascript to retrieve data in forms

Posted by: gonewthewind
Posted on: 2005-02-03 09:37:00

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
<form action="<?php if(!isset($_SERVER)){$_SERVER=$HTTP_SERVER_VARS;}echo $_SERVER['PHP_SELF'];?>" method="post" name="Form1">

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

Re: cookies/javascript to retrieve data in forms

Posted by: gonewthewind
Posted on: 2005-02-03 09:45:00

unless I need to use

session_write_close();

on the first index.php?

Re: cookies/javascript to retrieve data in forms

Posted by: decswxaqz
Posted on: 2005-02-03 09:46:00

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?

Re: cookies/javascript to retrieve data in forms

Posted by: gonewthewind
Posted on: 2005-02-03 09:54:00

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(); !!


Re: cookies/javascript to retrieve data in forms

Posted by: decswxaqz
Posted on: 2005-02-03 09:58:00

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

Re: cookies/javascript to retrieve data in forms

Posted by: everinjoy
Posted on: 2005-02-04 15:53:00

Why not just use
<input type=hidden name="$var" value="varvalue">
to make it pass the information to itself? Be easier than trying to use PHP. :-) (Then you can use
$var = $_POST[var]
to get the information back out of it once you're back on your original page again.

Tags: php codeemailphone numberbarbaratool