Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,920 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,769 people online right now. Registration is fast and FREE... Join Now!




How to Create Sticky Stay-Filled Forms

 
Reply to this topicStart new topic

How to Create Sticky Stay-Filled Forms

atomiccafe
14 Mar, 2008 - 07:50 PM
Post #1

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 9

Hi

I have a form that I want to keep the fields filled out exactly as it was submitted when the user refreshes the browser. My code is
<input name="first_name" type="text" id="first_name" value="<?php echo $first_name ?>" />

But I want to pre-populate the name field first so the field is not empty when the user is presented with the form for the first time like so.
<input name="first_name" type="text" id="first_name" value="George" />

How do I keep it pre-populated but when the user enters a different name and refreshes the page, his new name will stick and not the value "George"?

I tried this code and it didn't work.. <input name="first_name" type="text" size="20" id="first_name" value="George"."<?php echo $first_name ?>" />

========================

How do I apply this to Radio Buttons and Checkboxes?

<td>
<input type="radio" name="marital_status" value="Single" checked="checked">Single &nbsp;
<input type="radio" name="marital_status" value="Married">Married &nbsp;
<input type="radio" name="marital_status" value="Divorced">Divorced
</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" name="employment[]" value="Full Time" checked="checked">Full Time &nbsp;
<input type="checkbox" name="employment[]" value="Part Time" >Part Time &nbsp;
<input type="checkbox" name="employment[]" value="Per Diem" checked="checked">Per Diem &nbsp;
</td>



I hope this is clear.


Thank you in advance

This post has been edited by atomiccafe: 14 Mar, 2008 - 08:46 PM
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: How To Create Sticky Stay-Filled Forms
14 Mar, 2008 - 08:55 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 477



Thanked: 22 times
Dream Kudos: 350
My Contributions
well, it depends on how this is goiong to be done... you could use $_GET in which case it would be in the URL, and easily bookmarked, or if it's posted, you could use:

$first_name = $_POST['first_name'];

along with the <?php echo $first_name ?>

but it all depends where you're getting the data from.... the less secure way, though one you might want to use (for various purposes) would be to use $_REQUEST, which involves both $_POST and $_GET functions into one...
User is offlineProfile CardPM
+Quote Post

atomiccafe
RE: How To Create Sticky Stay-Filled Forms
14 Mar, 2008 - 09:07 PM
Post #3

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 9

I am learning PHP and this is just a homework. This one page form should echo the values at the top of the page AND it should stick on the form itself even if the user refreshes the page.

I got everything working except the pre-populated fields, the radio buttons and check boxes.

Example: the first name field.. <input name="first_name" type="text" id="first_name" value="<?php echo $first_name ?>" />
The name sticks if I don't put a name in the value="George" but if I pre-populate the name field with something like George and the user types a different name and hit the submit button. His name doesn't stick to the Form but the name George.


I'm using $_POST




QUOTE(JBrace1990 @ 14 Mar, 2008 - 09:55 PM) *

well, it depends on how this is goiong to be done... you could use $_GET in which case it would be in the URL, and easily bookmarked, or if it's posted, you could use:

$first_name = $_POST['first_name'];

along with the <?php echo $first_name ?>

but it all depends where you're getting the data from.... the less secure way, though one you might want to use (for various purposes) would be to use $_REQUEST, which involves both $_POST and $_GET functions into one...


User is offlineProfile CardPM
+Quote Post

mocker
RE: How To Create Sticky Stay-Filled Forms
14 Mar, 2008 - 09:57 PM
Post #4

D.I.C Regular
Group Icon

Joined: 14 Oct, 2007
Posts: 259



Thanked: 15 times
Dream Kudos: 25
My Contributions
There is a big difference between refreshing a page, and submitting the form. If a user goes to hit the refresh button, it does not pass any data to your script, so you cannot see what they had typed in. If they submit it, then the form value for 'first_name' will go in $_POST['first_name'], so
<input name="first_name" type="text" id="first_name" value="<?php echo $_POST['first_name'] ?>" /> would correctly populate that field.

If you are using $first_name as the variable without putting $first_name = $_POST['first_name']; first, then you are using an obselete setting in php called register_globals, which is being completely phased out so you should avoid doing it that way.
User is offlineProfile CardPM
+Quote Post

atomiccafe
RE: How To Create Sticky Stay-Filled Forms
14 Mar, 2008 - 10:23 PM
Post #5

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 9

Here's the link to my homework http://www.webdeveloperny.com/form.php

The First Name field has this code <input name="first_name" type="text" value="Mary" size="40" id="first_name">
The Last Name field has this code <input name="last_name" type="text" value="<?php echo $last_name ?>" size="40">

Notice the first name value "Mary". That value sticks everytime you change the name and hit submit but it prints that submitted name on top of the page.
if I change the code to <?php echo $first_name ?> Mary disappears and sticks whatever name I enter. What I want to accomplish is keeping the name Mary when the page loads and stick whatever name the user types after hitting the submit button. How should I code this? I'm a newbie.

Second, how do I do this on the radio buttons and check boxes as well?


Thanks in advance.



QUOTE(mocker @ 14 Mar, 2008 - 10:57 PM) *

There is a big difference between refreshing a page, and submitting the form. If a user goes to hit the refresh button, it does not pass any data to your script, so you cannot see what they had typed in. If they submit it, then the form value for 'first_name' will go in $_POST['first_name'], so
<input name="first_name" type="text" id="first_name" value="<?php echo $_POST['first_name'] ?>" /> would correctly populate that field.

If you are using $first_name as the variable without putting $first_name = $_POST['first_name']; first, then you are using an obselete setting in php called register_globals, which is being completely phased out so you should avoid doing it that way.


User is offlineProfile CardPM
+Quote Post

atomiccafe
RE: How To Create Sticky Stay-Filled Forms
15 Mar, 2008 - 09:18 AM
Post #6

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 9

Can anyone help please?
User is offlineProfile CardPM
+Quote Post

atomiccafe
RE: How To Create Sticky Stay-Filled Forms
16 Mar, 2008 - 01:29 PM
Post #7

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 9

Hi guys

How come noone is helping me? Did I say anything wrong?



QUOTE(atomiccafe @ 15 Mar, 2008 - 10:18 AM) *

Can anyone help please?


User is offlineProfile CardPM
+Quote Post

mocker
RE: How To Create Sticky Stay-Filled Forms
16 Mar, 2008 - 03:46 PM
Post #8

D.I.C Regular
Group Icon

Joined: 14 Oct, 2007
Posts: 259



Thanked: 15 times
Dream Kudos: 25
My Contributions
you'll probably want an if statement then as IF $first_name isn't set, you want it to echo Mary , so
<?php if(isset($first_name)){ echo $first_name; } else { echo "Mary"; } ?>
User is offlineProfile CardPM
+Quote Post

atomiccafe
RE: How To Create Sticky Stay-Filled Forms
16 Mar, 2008 - 05:33 PM
Post #9

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 9

Thank you mocker. I really appreciate it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:12PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month