Current location: Hot Scripts Forums » Programming Languages » PHP » Save and Load Values of a Form


Save and Load Values of a Form

Reply
  #1 (permalink)  
Old 03-15-04, 10:06 PM
veeco veeco is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Question Save and Load Values of a Form

I have a php generated page that contains 63 drop-down menus (with names v1, v2, v3, etc).

I was wondering if I go to the page and select 10 of the 63 dropdowns, can I save those selections so the next time I visit the page the drop-down menus will already be on the respective choices? I'm not sure if this can be done in PHP, or javascript.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 03-16-04, 01:40 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Here's more or less what I'd do (or have done) with PHP in such situations. If you are using cookie to store the selections, I'm sure you can do it with JavaScript, too:

PHP Code:

<?php


// Generate an array to hold your combobox value/options.
// This is for this particular script only - I guess you have this kind of array already anyway.
for ($i 1$i <= 63$i++) {
    
$a_options['v'.$i] = 'Option '.$i;
}

// Pre-selected options' values without 'v' - this way, you can save one char-space per entry.
// Again, this is for this demo only. You should get this array from cookie (that you have set before) or database or any kind of storage you want to use.
// What I'd do perhaps is to setcookie() previous selections, serialized with implode(',', substr($_REQUEST['mySelect']), 1) or something and explode() it when pre-selecting the options.
$a_selected = array(5610203538);

// Show the combobox, options pre-selected in accordance with $a_selected.
echo '<select name="mySelect[]" multiple="multiple" size="10">';
foreach (
$a_options as $key => $val) {
    
// It's important that $a_selected be an array or you'll get an error on in_array().
    // substr() is not necessary if we choose to store the selections as they are (i.e. with "v" prefix)
    
echo '<option value="'.$key.'"'.(in_array(substr($key1), $a_selected) ? ' selected="selected"' '').'>'.$val.'</option>'."\n";
}
echo 
'</select>';

?>
You can literally copy-and-paste the above to see the effect, but please read the comments, too. Good luck.

EDIT: Forgot the preceeding white space before selected="selected".
__________________
Blavv =|
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
'save as' option for XML files hulkphil JavaScript 1 03-04-05 09:15 AM
Generating thumbnails: to save or show only? borgo PHP 1 08-12-03 09:38 AM


All times are GMT -5. The time now is 10:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.