23

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">';
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mohamad
  • 602
  • 2
  • 5
  • 18
  • You could use `session_start();` before headers are sent, then use the `$_SESSION` super global. Of course, I suggest you use AJAX, unless you prefer being stuck in the Stone Age. – StackSlave Aug 13 '16 at 07:16
  • if i want to use ajax how can i do it – Mohamad Aug 13 '16 at 07:23
  • and how can i use session without submit – Mohamad Aug 13 '16 at 07:33
  • There is `localStorage` and `sessionStorage` in JavaScript now. JavaScript's `document.cookie` should work on all Browsers, but is more complex for beginners. How well do you know JavaScript? – StackSlave Aug 13 '16 at 07:37
  • i'm a beginners on javascript can you help me to do this – Mohamad Aug 13 '16 at 07:43
  • http://stackoverflow.com/questions/38844743/js-input-type-date-field-keep-values-selected-after-form-submission/38845803?noredirect=1#comment65096145_38845803 this is similar question try it.. –  Aug 13 '16 at 07:58
  • There are a number of ways to get an Element in JavaScript. Perhaps the most common is `document.getElementById('HTMLidHere')`. There are other ways, of course, so I will use `Element` to represent that. `Element.value` gets and sets HTML input and textarea values. For other Elements it's `Element.innerHTML`. You should be able to find these type of examples, along with the the ways you can store this information, that I've already commented on, online. You have to have, at least, a basic understanding of JavaScript for us to help. By the way, that example is a bad practice. – StackSlave Aug 13 '16 at 07:59
  • If you ask this question with a Stack Overflow `javascript` tag, you'll get more help, possibly. Of course, basic understanding is a must. – StackSlave Aug 13 '16 at 08:03
  • @AhmadIgbaryya my problem is without submiting the form is that possible ? – Mohamad Aug 13 '16 at 08:06
  • @user301800 do you mean to start with default vaules before the submit ? –  Aug 13 '16 at 08:15
  • @AhmadIgbaryya no when user enter a value this value should be saved without submiting if the user go to another page or refresh the page – Mohamad Aug 13 '16 at 08:21
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Nov 09 '19 at 19:52

4 Answers4

54

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>
  • @user3018000 then you need to edit the functions to make it receives also the name of what to save, the value. Also to getSavedValue (v) make it return the value of "v" . –  Aug 13 '16 at 10:22
  • lgbaryya how can i modify this function if i have 2 or mor input – Mohamad Aug 15 '16 at 07:29
  • 1
    @Igbaryya You solved my search that lasted for many days. that too after years.. thanks mate ! <3 – SANGEETH SUBRAMONIAM Apr 02 '21 at 07:51
  • is it possible to use same script for select input ? if you have select for all, active and inactive option ? – Aljaz May 13 '22 at 12:17
  • [Dokumentation for localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) – A-Tech Jul 29 '22 at 11:01
6

if the above code did not work try this:

<input type="text" id="txt_1" onchange='saveValue(this);'/> 
<input type="text" id="txt_2" onchange='saveValue(this);'/>
zaf y
  • 153
  • 3
  • 13
0

You can also use useContext() from react context() if you're using hooks.

Blessing
  • 2,450
  • 15
  • 22
-3

In MVC/Razor, first you should add a variable in your model class for the textBox like this:

namespace MVCStepByStep.Models { public class CustomerClass { public string CustomerName { get; set; }
} }

Then in Views --> Index.cshtml file make sure the Textbox is created like this: @Html.TextBoxFor(m => m.CustomerName)

For a complete example, please check out this site:

How to update a C# MVC TextBox By Clicking a Button using JQuery – C# MVC Step By STep[^]

  • 1
    Since the question was asked with the php tag, I don't think a solution for C# will help to solve this particular problem – overflowed May 23 '21 at 01:12