当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Keep values selected after form submission

问题描述:

<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>

On submitting the form, how do I make sure that the selected values remain selected in the dropdowns? This form is inside wordpress (PHP).

网友答案:

To avoid many if-else structures, let javascript do the trick automatically:

 <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>
网友答案:
<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>
网友答案:

After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.

<?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>
网友答案:

Js only solution:

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];
    }
}

or if you are using jquery you can replace

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

with:

 $('*[name="'+tmparr[0]+'"]').val(tmparr[1]);
网友答案:

Since wordpress already uses jquery you can try something like this:

var POST=<?php echo json_encode($_POST); ?>;
for(k in POST){
  $("#"+k).val(POST[k]);
}
网友答案:
<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>
网友答案:

You can use wordpress function that is 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>
网友答案:

This works for me!

<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>
网友答案:

To do this in PHP you can use sessions to store the POST or GET results and call on them in the form if they're set. Something like:

<?php
session_start();
if(isset($_POST['submit'])){
$name=$_POST['name'];
$location=$_POST['location'];
$_SESSION['name']=$name;
$_SESSION['location']=$location;
}

$nameSel=array('a','b');
$locSel=array('x','y');

//start form and name select box//
    echo '<form method="post">
          <select name="name">';
foreach($nameSel as $name){
if(isset($_SESSION['name']) && $_SESSION['name']==$name){
    echo '<option selected value="'.$name.'">'.$name.'</option>';
}else{
    echo '<option value="'.$name.'">'.$name.'</option>';
}
}
    echo '</select>';

//location select box with submit and end form//    
    echo '<select name="location">';
foreach($locSel as $loc){
if(isset($_SESSION['location']) && $_SESSION['location']==$loc){
    echo '<option selected value="'.$loc.'">'.$loc.'</option>';
}else{
    echo '<option value="'.$loc.'">'.$loc.'</option>';
}
}
    echo '</select>
          <input type="submit" name="submit" value="Submit" class="submit" />
          </form>';
分享给朋友:
您可能感兴趣的文章:
随机阅读: