表单提交后保留选中的值

时间:2010-02-11 17:21:31

标签: php javascript html wordpress forms

<form method="get" action="">
   <select name="name">
      <option value="a">a</option>
      <option value="b">b</option>
   </select>
   <select name="location">
      <option value="x">x</option>
      <option value="y">y</option>
   </select>
   <input type="submit" value="Submit" class="submit" />
</form>

在提交表单时,如何确保在下拉列表中保留所选值?这个表单在wordpress(PHP)中。

11 个答案:

答案 0 :(得分:41)

为了避免许多if-else结构,让javascript自动完成这个技巧:

 <select name="name" id="name">
  <option value="a">a</option>
  <option value="b">b</option>
 </select>

<script type="text/javascript">
  document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>

 <select name="location" id="location">
  <option value="x">x</option>
  <option value="y">y</option>
 </select>

<script type="text/javascript">
  document.getElementById('location').value = "<?php echo $_GET['location'];?>";
</script>

答案 1 :(得分:22)

<select name="name">
   <option <?php if ($_GET['name'] == 'a') { ?>selected="true" <?php }; ?>value="a">a</option>
   <option <?php if ($_GET['name'] == 'b') { ?>selected="true" <?php }; ?>value="b">b</option>
</select>

答案 2 :(得分:5)

尝试了这个&#34;解决&#34;什么都行不通之前对w3school进行了一些研究,并记得有关于保持对无线电的价值的解释。但它也适用于选择选项。看这里的例子。试试吧,玩吧。

<?php
$example = $_POST["example"];
?>
<form method="post">        
<select name="example">
    <option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
    <option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
    <option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>

答案 3 :(得分:3)

由于wordpress已经使用了jquery,你可以尝试这样的事情:

var POST=<?php echo json_encode($_POST); ?>;
for(k in POST){
  $("#"+k).val(POST[k]);
}

答案 4 :(得分:3)

如果您使用的是Wordpress(与OP的情况一样),您可以使用selected功能。

<form method="get" action="">
  <select name="name">
   <option value="a" <?php selected( isset($_POST['name']) ? $_POST['name'] : '', 'a' ); ?>>a</option>
   <option value="b" <?php selected( isset($_POST['name']) ? $_POST['name'] : '', 'b' ); ?>>b</option>
 </select>
 <select name="location">
   <option value="x" <?php selected( isset($_POST['location']) ? $_POST['location'] : '', 'x' ); ?>>x</option>
   <option value="y" <?php selected( isset($_POST['location']) ? $_POST['location'] : '', 'y' ); ?>>y</option>
 </select>
 <input type="submit" value="Submit" class="submit" />
</form>

答案 5 :(得分:2)

Js唯一的解决方案:

var tmpParams = decodeURIComponent(window.location.search.substr(1)).split("&");
for (var i = 0; i < tmpParams.length; i++) {
    var tmparr = tmpParams[i].split("=");
    var tmp = document.getElementsByName(tmparr[0])[0];
    if (!!tmp){
        document.getElementsByName(tmparr[0])[0].value = tmparr[1];
    }
}

或者如果您使用的是jquery,则可以替换

 var tmp = document.getElementsByName(tmparr[0])[0];
 if (!!tmp){
     document.getElementsByName(tmparr[0])[0].value = tmparr[1];
 }

使用:

 $('*[name="'+tmparr[0]+'"]').val(tmparr[1]);

答案 6 :(得分:1)

尝试使用此解决方案在下拉列表中保留所选值:

<form action="<?php echo get_page_link(); ?>" method="post">
    <select name="<?php echo $field_key['key']; ?>" onchange="javascript: 
       submit()">
    <option value="">All Category</option>
    <?php 
    foreach( $field['choices'] as $key => $value ){ 
       if($post_key==$key){ ?>
          <option value="<?php echo $key; ?>" selected><?php echo $value; ?></option>
<?php
   }else{?>
  <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
 <?php }
  }?>
 </select>
</form>

答案 7 :(得分:0)

这对我有用!

<label for="reason">Reason:</label>
<select name="reason" size="1" id="name" >
    <option value="NG" selected="SELECTED"><?php if (!(strcmp("NG", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>Selection a reason below</option>
    <option value="General"<?php if (!(strcmp("General", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>General Question</option>
    <option value="Account"<?php if (!(strcmp("Account", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>Account Question</option>
    <option value="Other"<?php if (!(strcmp("Other", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>Other</option>

</select>

答案 8 :(得分:0)

我在WP中工作不多,但是对于Wordpress以外的表格,这很好。

PHP

location = "";                                   // declare variable

if($_POST) {
    if(!$_POST["location"]) {
        $error .= "Location is required.<br />"; // If not selected, add string to error message
     }else{
        $location = $_POST["location"];          // If selected, assign to variable
     }

HTML

<select name="location">
    <option value="0">Choose...</option>
    <option value="1" <?php if (isset($location) && $location == "1") echo "selected" ?>>location 1</option>
    <option value="2" <?php if (isset($location) && $location == "2") echo "selected" ?>>location 2</option>
</select>

答案 9 :(得分:0)

只需更改此行:

 <input type="submit" value="Submit" class="submit" />

有这行:

 <input type="submit" value="Submit/Reload" class="submit" onclick="history.go(-1);">

答案 10 :(得分:-1)

<form method="get" action="">
 <select name="name" value="<?php echo $_GET['name'];?>">
   <option value="a">a</option>
   <option value="b">b</option>
 </select>
 <select name="location" value="<?php echo $_GET['location'];?>">
   <option value="x">x</option>
   <option value="y">y</option>
 </select>
   <input type="submit" value="Submit" class="submit" />
</form>