Sandesh
Sandesh

Reputation: 63

Retaining values of dynamically populated select option after form submit in php

I am trying to retain form values after submitting the form.

document.getElementById('start_date').value = "<?php echo $_POST['start_date'];?>";
document.getElementById('end_date').value = "<?php echo $_POST['end_date'];?>";

It is working for date picker but the same is not working for select option.

HTML code:

<form id=Inputs action="" method="POST" onsubmit="return ray.ajax()">
<center><table>
<td align="left" width=200px> <b>Select The Product:</b> 
  <select name="product" id="product" onChange="changecat(this.value);changeenv(this.value);">
    <option value="" disabled selected>Select</option>
    <option value="MOBILE">MOBILE</option>
    <option value="RAM">RAM</option>
    <option value="CAT">CAT</option>
</select></td>
<td width=200px> <b>Select Data Center:</b> 
  <select name="dc" id="dc">
    <option value="" disabled selected>Select</option>
</select></td>
<td width=200px> <b>Select Environment:</b> 
  <select name="env" id="env"><option value='NO' selected disabled>Select</option>
</select></td>
<td width=200px> <b>Select Start Date:</b>
<input type="date" name="start_date" id="start_date">
</td>

<td width=200px><b>Select End Date:</b>
<input type="date" name="end_date" id="end_date"></td></tr></table></center><br>
<div align="center"><input type="submit" class="button" value="Submit" name="submit" onclick="clearBox('tb')">&nbsp;&nbsp;&nbsp;<input type="reset" class="button" value="Reset" name="reset" onclick="clearBox('tb')"></div>
<br>
</form>

JS code for making select dropdown getting populated dynamically:

<script language="javascript" type="text/javascript">
    var mealsByCategory = {
    MOBILE: ["Select","ALL","RED","GREEN"],
    RAM:["Select","ALL","ROM","CPU"],
    CAT:["Select","ALL","DOG","FOOD"],

}

    function changecat(value) {
        if (value.length == 0) document.getElementById("dc").innerHTML = "<option></option>" ;
        else {
            var catOptions = "";
            for (categoryId in mealsByCategory[value]) {
                catOptions += "<option>" + mealsByCategory[value][categoryId] + "</option>";
            }
            document.getElementById("dc").innerHTML = catOptions;
        }
    }


    var environment = {
    MOBILE: ["Select","Pr","Prv"],
    RAM:["Select","Pr","Sl"],
    CAT:["Select","Pr","BS"],
}

    function changeenv(value) {
        if (value.length == 0) document.getElementById("env").innerHTML = "<option></option>" ;
        else {
            var envOptions = "";
            for (categoryId in environment[value]) {
                envOptions += "<option>" + environment[value][categoryId] + "</option>";
            }
            document.getElementById("env").innerHTML = envOptions;
        }
    }
    </script>

Upvotes: 0

Views: 454

Answers (1)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

You have error in your code as below:

var environment = {
    MOBILE: ["Select","Pr","Prv"],
    RAM:["Select","Pr","Sl""],  // Remove extra " 
    CAT:["Select","Pr","BS"],
}

check out put here - https://jsfiddle.net/bhedabhupat/nragst1z/

Upvotes: 1

Related Questions