Helping ordinary people create extraordinary websites!

Go Back   Web Development Forum > Website Programming > Server-Side Scripting
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-01-2008, 10:13 PM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 708
Default Saving users options

I'm trying to sort an online program, which allow users to choose a bunch of options, then those options will be outputted in a certain format on screen for them to copy and paste for their own purposes.

I can generate something fairly basic like so:

PHP Code:
<form method="post" action="index.php">
    <select name="bla">
        <option>Red</option>
        <option>Green</option>
        <option>Blue</option>
    </select>
    <input type="submit" name="Submit" />
</form>

<?php    $bla $_POST['bla']; ?>

<p>You chose the "<?php echo $bla?>" option</p>
This does exactly what I want.

However ... when the user clicks submit, the original option "Red" is always chosen by default. How would I go about ensuring that when that user clicks submit, that whatever option they chose comes first?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 10:35 PM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 708
Default

Okay, I've nutted out a crude fix for it that seems to do the job. It could get a little messy once I have a couple of hundred options on the page, but it will do the job.

PHP Code:
<form method="post" action="index.php">
    <select name="bla">
        <?php
            
if ($bla == "Red") {echo '<option>Red</option><option>Green</option><option>Blue</option>';}
            elseif (
$bla == "Green") {echo '<option>Green</option><option>Red</option><option>Blue</option>';}
            elseif (
$bla == "Blue") {echo '<option>Blue</option><option>Red</option><option>Green</option>';}
            else {echo 
'<option>Red</option><option>Green</option><option>Blue</option>';}
        
?>
    </select>
    <input type="submit" name="Submit" />
</form>

<?php    $bla $_POST['bla']; ?>

<p>You chose the "<?php echo $bla?>" option</p>

Is there a better way of doing this?

Also, if the user refreshes the page, the options go back tot he default. Is there a way to save this data? Perhaps on a per IP basis?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-02-2008, 05:14 AM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 291
Send a message via MSN to BigAlReturns
Default

There's an attenuated version of if else syntax that makes for much clearer reading, the basic format is:
Code:
(statement) ? "return if true" : "return if false";
e.g.
echo ($bla==$colour) ? 'selected' : 'not selected';
You can also use the selected attribute of an option tag to determine which is initially selected, i.e.:
Code:
<option value="red" selected="selected">Red</option>
So in your case it would look something like this:
Code:
    <select name="bla">
       <option value="red" selected="<?php echo ($bla=="red") ? 'selected="selected"';?>">Red</option>
<option value="green" selected="<?php echo ($bla=="green") ? 'selected="selected"';?>">Green</option>
<option value="blue" selected="<?php echo ($bla=="blue") ? 'selected="selected"';?>">Blue</option>
</select>
Another, probably more elegant, and definitely more extensible way to do this is to store colour options in an array, and loop through this array to create the options:
Code:
<select>
<?php
$colours=array('red','blue','green');
FOREACH ($colours AS $colour) {
$selector = ($bla==$colour) ? 'selected="selected"' : '';
echo '<option value="' . $colour . '"' . $selected . '>' . $colour . ' </option>';
}
?>
</select>
The first method is easier on the eye, but the method in this post means you can easily add or remove options, and easily fill the options dynamically, say from a database.

As for the refresh issue, I personally don't worry about it - it's unlikely to occur very often. If you did really need to do it, then IP address won't work, because not only can it change, but people can share IPs as well. The way forward with this would be to use sessions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
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

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



All times are GMT -5. The time now is 03:21 AM.


Website Design by Ducani Media Group
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.