简体   繁体   中英

Get previous value of select option

Is there a way to get the previous value of select option?

the previous value is Red

在此处输入图片说明

If i change it to green it will echo the selected color

How can i echo the previous color which is red ?

绿色

this is my code : thanks :)

<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];  
echo "You have selected :" .$selected_val . "<p>"; 
echo "The previous value is :" ; 
}
?>

Use hidden field to store current value. On form post, check if the current value is different than selected value show it.

Pro tip: Always encrypt your hidden field data

store the previous value in your form as hidden value and get it

<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
$previous = $_POST['previous'];
}
?>
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="hidden" name="previous" value="<?php echo $selected_val; ?>"/>
<input type="submit" name="submit" value="Get Selected Values" />
</form>  
<?php echo "You have selected :" .$selected_val . "<p>"; 
echo "The previous value is :".$previous ; ?> 

Since you must never trust user submitted content, another way is to build your form from PHP and do the test on the server side.

Example:

<?php
$colors = Array('Red', 'Green', 'Blue', 'Pink', 'Yellow');
?>
<form method="POST">
 <select name="Color"><?php
foreach($colors as $color)
{
    printf('  <option value="%1$s">%1$s</option>'."\n", $color);
}
?>
 </select>
 <input type="submit">
</form>
<?php
if (!empty($_POST['Color']))
{
    $previous_index = array_search($_POST['Color'], $colors) - 1;
    // wrap around if the color is the first
    if ($previous_index < 0) {
        $previous_index = count($colors);
    }
    printf(
        "You have selected <q>%s</q>, the previous value is: %s\n",
        $_POST['Color'],
        $colors[ $previous_index ]
    );
}
?>

The code is obviously longer than it could for educative purpose.

Here is another solution using jQuery, as a bonus you keep track of whole history and not just previous selection.

I have not tested the PHP part, but you get the idea.

HTML

<form action="#" method="post">
<select name="Color" onChange="updateHistory(this);">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="hidden" id="history" name="history" value="">
</div>
</div>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color']; 
$hist = explode(',',$_POST['history']);

// pop the last value, which is current selection
array_pop($hist);

// get the last value, which now is previous
$last_valaue = end($hist);
echo "You have selected :" .$selected_val . "<p>"; 
echo "The previous value is :" . $last_valaue ; 
echo 'DEBUG:<br>';
var_dump($hist);
}
?>

JS

function updateHistory(el){
var val = $(el).val();
var hist = $('#history').val();
if (hist.length > 0) {hist += ','}
hist += val;
$('#history').val(hist);
}

Example JS Fiddle

UPDATE 1:

You can further manipulate history array in PHP to your needs, for example, if you need unique values, you can use array_unique() ( http://php.net/manual/en/function.array-unique.php )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM