I am not sure what this thing is called, in layman terms I can think of calling it 'form data retention':
I have a search page on my website, the search options appears at the top of the page, when someone selects his/her options and do a search, the page redirects to itself, performs some sorting and filtering, and finally displays results below the search options (yes, the search options will show up again at the top of the page, for users to do another search). This is a full reload of the page, not AJAX reload for the results.
The problem here is that the search form has ALOT of options that users can specify in their search, and if they are going to have to re-select all options everytime they perform a search, its horribly inefficient and definately turns people off. What I'm trying to do is to retain user selected options in checkboxes, drop down lists and radio buttons on my form, even after they click on the search button and the page reloads. In other words, the search form retains all the the user selected options from their previous search.
What I am doing now is grabbing data off the GET parameters in my URL, (i pass my search variables by GET anyway so I am kind of reusing them) and then try to manually check the check boxes, select the radio buttons etc with some javascript. This seems.. Inelegant.. Is there a 'correct way' to do this?
'Data retention' in forms
Moderator: General Moderators
Re: 'Data retention' in forms
This is the correct way to do it. use GET or POST to retrieve the value posted and put them back in the correct field. So no there are no other ways to store keep the value in the field in php.
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
Re: 'Data retention' in forms
Look into SESSIONS. By utulizing them you can have your form page check to see if a session variable has been set for each form item and take the appropriate action
Re: 'Data retention' in forms
I wouldn't recommend using session for these kind of things, they are good but not useful, just think of what will happen if that session dies, what will happen to the data??
Re: 'Data retention' in forms
Use PHP to populate the fields again:
Code: Select all
<?php
$select_value = $_GET['select_box_value'];
$checkbox_value = (isset($_GET['checkbox_value']);//will be TRUE if the checkbox was checked, FALSE if not
?>
<html>
...
<select name = "select_box_value">
<option value = "oranges" <?php if($select_value == "oranges"): ?>selected="selected"<?php endif; ?>>Oranges</option>
<option value = "apples" <?php if($select_value == "apples"): ?>selected="selected"<?php endif; ?>>Apples</option>
...
</select>
<input type = "checkbox" name= "checkbox_value" <?php if($checkbox_value): ?>checked="checked"<?php endif; ?> />Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: 'Data retention' in forms
Thanks pickle for the code on retention of data on checkboxes. It was just what I was looking for. Between HTML and PHP syntax they are going to drive me crazy.