Asked  7 Months ago    Answers:  5   Viewed   52 times

I have a form with input field and this input contain a drop down menu read information from database. If the user enters value and when he arrives to the drop menu he doesn't find what he wants he go to another page to add this info to the drop down menu and then go to the first page to continue enter the information. How can I keep this information if he goes to another page to add info to drop menu and how can after adding the info to drop menu find 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">
   ';?>
     <?php 
    $sql=mysqli_query($conn,"select DISTINCT db_label from tbl_label")or die(mysqli_error($conn));
 echo'<option  value="">-- Select --</option>';
    while($row=mysqli_fetch_array($sql)){
        $label=$row['db_label'];
        echo "<option value='$label'>$label</option>"; 
    }echo'</select>';?><?php echo'
  </div>
</form>

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>
   <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">
  </div>
   <input type="submit" name="addlabel" value="Add" class="btn btn-default">';

 Answers

89

EDIT: Keep value of more inputs

HTML:

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

Javascript:

<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 = e.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)) {
                return "";// You can change this to your defualt value. 
            }
            return localStorage.getItem(v);
        }
</script>
Wednesday, March 31, 2021
 
laurent
answered 7 Months ago
80

dataType - delete this one.

Add console.log and open console in Your browser

success: function (data) {
   console.log( data );

show Your console, and then You will see why. Maybe an unwanted char or php error

Second thing - there should be if stament like this (I supposed)

if (data == "1") // it is returning string, not integer.

You can also try to use switch case in success.

Wednesday, March 31, 2021
 
CodeCaster
answered 7 Months ago
90

I prefer using SESSION variables before cookies. Here is an example code:

On the form recieving page:

session_start();
$_SESSION['from'] = $_POST['from'];
$_SESSION['area_html'] = $_POST['area_html'];

Then on your other page:

<?php session_start(); ?>
<!-- here is your html header etc -->
From: <?php echo $_SESSION['from']; ?><br>
Area HTML: <?php echo $_SESSION['area_html']; ?>

Note that the session_start(); part must be stated BEFORE any other HTML output.

HTML syntax for form should read:

<input type="text" name="from" style="width:350px" value="<?php echo $_SESSION['from']; ?>" />
<textarea valign="top" name="area_html"><?php echo $_SESSION['area_html']; ?></textarea>

Note the type of the INPUT tag and the change how values should be inserted into a TEXTAREA.

Saturday, May 29, 2021
 
Bono
answered 5 Months ago
38

Try this out.

function loadlink(){
    $('#links').load('test.php',function () {
         $(this).unwrap();
    });
}

loadlink(); // This will run on page load
setInterval(function(){
    loadlink() // this will run after every 5 seconds
}, 5000);

Hope this helps.

Thursday, June 3, 2021
 
Asher
answered 5 Months ago
58

Cookies

The best solution is to use cookies. A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is usually a small piece of data sent from a website and stored in a user's web browser while a user is browsing a website (from Wikipedia).

Using a cookie you can save the state and later read it and use it.

With jQuery it is very easy to use cookies.

To set:

$.cookie("var", "10");

To get:

$.cookie("var")

To delete:

$.cookie("var", null);

Local Storage

When you want to save localy great amount of data, you have another opportunity — to use local storage (since HTML5). You can do it directly using JavaScript or using one of available jQuery plugins.

for example, with totalStorage:

var scores = new Array();
scores.push({'name':'A', points:10});
scores.push({'name':'B', points:20});
scores.push({'name':'C', points:0});
$.totalStorage('scores', scores);
Thursday, July 29, 2021
 
freeMagee
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :