user3018000 user3018000 - 1 year ago 120
Ajax Question

keep input value after refresh page

i have a form with input field and this input contain a drop down menu read information from database
if the user enter value and when he arrive to the drop menu he doesn't find what he want he go to another page to add this info to the drop down menu and than go to the first page to continue enter the information
what i need how can i keep this information if he go to another page to add info to drop menu and how can after adding the info to drop menu finding this info without refresh and without submit

this is the first page with the form

<form name='' method='post' action='<?php $_PHP_SELF ?>'>
<input name='txt_name' id='' type='text'>

this drop menu read from database

<select id="groups" name="txt_label" class="form-control">
$sql=mysqli_query($conn,"select DISTINCT db_label from tbl_label")or die(mysqli_error($conn));
echo'<option value="">-- Select --</option>';
echo "<option value='$label'>$label</option>";
}echo'</select>';?><?php echo'

second form in another page

<form class="form-inline" role="form" name="form" method="post" action="';?><?php $_PHP_SELF ?><?php echo'">
<div class="form-group">
<label for="pwd">Label</label>
<input id="txt_label" name="txt_label" type="text" placeholder="Label" class="form-control input-md">
<div class="form-group">
<label for="pwd">Sub Label</label>
<input id="txt_sublabel" name="txt_sublabel" type="text" placeholder="SubLabel" class="form-control input-md">
<input type="submit" name="addlabel" value="Add" class="btn btn-default">';

Answer Source

EDIT: Keep value of more inputs


<input type="text" id="txt_1" onkeyup='saveValue(this);'/> 
<input type="text" id="txt_2" onkeyup='saveValue(this);'/> 


<script type="text/javascript">
        document.getElementById("txt_1").value = getSavedValue("txt_1");    // set the value to this input
        document.getElementById("txt_2").value = getSavedValue("txt_2");   // set the value to this input
        /* Here you can add more inputs to set value. if it's saved */

        //Save the value function - save it to localStorage as (ID, VALUE)
        function saveValue(e){
            var id =;  // get the sender's id to save it . 
            var val = e.value; // get the value. 
            localStorage.setItem(id, val);// Every time user writing something, the localStorage's value will override . 

        //get the saved value function - return the value of "v" from localStorage. 
        function getSavedValue  (v){
            if (localStorage.getItem(v) === null) {
                return "";// You can change this to your defualt value. 
            return localStorage.getItem(v);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download