最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - I want the last selected option to be selected in my <select> form after the user selected the option - Stac

programmeradmin1浏览0评论
<form method="get" action="index.php">
<select name="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">C</option>
</select></form>

The code above displays a dropdown menu of options, such as a, b, or c, the user can click on. After the user clicks on a, b, or c from the drop down menu, that option is submitted in the form, and the page reloads with a new $_GET variable "choice" defined as a, b, or c. I want the option that the user last selected to now be the default selected option listed. For example, after the user clicks on "b" from the dropdown menu, "b" shows up as selected on the drop down menu list. What's the best way to acplish this?

<form method="get" action="index.php">
<select name="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">C</option>
</select></form>

The code above displays a dropdown menu of options, such as a, b, or c, the user can click on. After the user clicks on a, b, or c from the drop down menu, that option is submitted in the form, and the page reloads with a new $_GET variable "choice" defined as a, b, or c. I want the option that the user last selected to now be the default selected option listed. For example, after the user clicks on "b" from the dropdown menu, "b" shows up as selected on the drop down menu list. What's the best way to acplish this?

Share Improve this question asked Sep 30, 2012 at 15:20 Colin HuColin Hu 1892 silver badges15 bronze badges 2
  • Are you using smarty or some other template engine? – Develoger Commented Sep 30, 2012 at 15:22
  • i am using raw html, php, javascript, and jquery code in my index.php document – Colin Hu Commented Sep 30, 2012 at 15:24
Add a ment  | 

4 Answers 4

Reset to default 1

Here you go:

<?php

    if (isset($_REQUEST['choice'])) {

        $selected_choice = $_REQUEST['choice'];

    }
    else {

        $selected_choice = "none";

    }

?>

<html>
<head>
</head>
<body>

<form method="get" action="">
    <select name="choice" size="1" onchange="this.form.submit()">
        <option value="a" <?php if($selected_choice == "a"){ print "selected='selected'"; } ?> >a</option>
        <option value="b" <?php if($selected_choice == "b"){ print "selected='selected'"; } ?> >b</option>
        <option value="c" <?php if($selected_choice == "c"){ print "selected='selected'"; } ?> >C</option>
    </select>
</form>

</body>
</html>

When outputting the form do a check like:

<option value="a"<?php if ($_GET['choice'] == "a") {?> selected <?php } ?>>a</option>

You'd then want to extract that into a function that either does the conditional code for you, or outputs the entire option element with the provided value, label, and sanitized data with the check done internally.

Another approach to avoid spaghetti code (or at least reduce it) is to set it through Javascript, like:

<html>
<body>
<form method="get" action="test.htm">

<select name="choice" id="choice" size="1" onchange="this.form.submit()">
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
</select>

</form>

<script>

    document.getElementById('choice').value = 'b';//replace with something like: <?php echo $_GET['choice'] ?>

</script>

</body>
</html>

IMHO this approach is better than put a test in each option tag.

Try saving the posted select option in session like below

<?php $_SESSION['current_select'] = $_REQUEST['posted_select_option']?>

And in page load check as below

<script>
var currentSelect = <?php echo $_SESSION['current_select']; ?>
$(document).ready(function(){
 if(currentSelect!="")
 $("#choice").val(currentSelect);
});
</script>

Considering your select tag has id like below

<select name="choice" id="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">C</option>
</select>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论