简体   繁体   中英

save PHP form data without submit on page refresh

I have two forms in the page one is filter, second is the item list, when I apply the filter to item list form, selected values reset to default. For filter I can save selected values, because I submit this form via POST method, but other remains not submited is possible to save selected that form values after page refresh? Here is some code of second form:

<form name="forma" method="post" onMouseOver="kaina();" onSubmit="return tikrinimas()" action="pagrindinis.php?page=generuoti.php">
  <table width="540" border="1" align="center">
  <tr>
    <td>Client:</td>
    <td>MB:</td>
    <td>Price:</td>
  </tr>
  <tr>
    <td>
    <?php
        $query="SELECT name,surname,pers_code FROM Clients";
        mysql_query("SET NAMES 'UTF8'");
        $result = mysql_query ($query);
        echo "<select name=Clients id='clients'>";
        echo "<OPTION value=''>- Choose -</OPTION>\n";
        while($nt=mysql_fetch_array($result)){
            echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
        }
        echo "</select>"; 
    ?></td>
  </tr>
</form>

Unfortunately there is no easy way to do this, and it's something that PHP and web developers in general have maligned for years, because repopulating form fields is never easy AND clean.

Values in the form you aren't submitting wont be sent (via GET or POST), so you're left with writing a custom workaround.

While it's a bit Q&D, I would recommend sending an Ajax call on form submit to store the values for your second form in the $_SESSION variable.

I'm sorry to say there's no easy alternative - due to the stateless nature of HTTP requests, this is something that every programmer has to struggle with.

You need to set the selected attribute of your select element based on what is in $_POST . Something like:

$selected = $_POST['Client'];
while($nt=mysql_fetch_array($result)){
  if ($selected == $nt[pers_code]) {
    echo "<option value=$nt[pers_code] selected="selected">$nt[name] $nt[surname]</option>";
  }
  else {
    echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
  }
}

Also note that you should probably sanitize any values you get from $_POST .

<script type="text/javascript" src="js/jquery1.6.1.js"></script>
<script type="text/javascript">
   $(document).ready(function () {
       $("#forma").submit(function(event) {
    var 1stformfield = $form.find( 'input[name="misc"]' ).val();
    /* stop form from submitting normally */
    //event.preventDefault(); 
    $.post("1stformsubmit.php", $("#forma").serialize());
    //after it posts you can have it do whatever
    //just post code here
    $('#iframe1').attr('src', 'iframepage.php?client=' + 1stformfield);
    window.frames["iframe1"].location.reload();
    return false;
     });
   });
    </script>
    <form name="1stform" method="post" action="/">
        <input type="text" name="misc" id="misc" />
        <input type="submit" name="submit" id="submit" value="submit 1st form"/>
    </form>
    <iframe id="iframe1" src="" scrolling="no" ></iframe>

iframe page

     <form name="forma" method="post" onmouseover="kaina();" action="/">
       <table width="540" border="1" align="center">
         <tr>
              <td>Client:</td>
               <td>MB:</td>
      <td>Price:</td>
  </tr>
  <tr>
     <td>
   <?php
    $selected = $_GET['Client'];
    $query="SELECT name,surname,pers_code FROM Clients";
    mysql_query("SET NAMES 'UTF8'");
    $result = mysql_query ($query);
    echo "<select name=Clients id='clients'>";
    echo "<OPTION value=''>- Choose -</OPTION>\n";
    while($nt=mysql_fetch_array($result)){
        echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
       }
    echo "</select>"; ?></td>
  </tr>
</form>

This will post all inputs inside your form to the file you specify then you can have it do whatever you like, for example show a thank you message..Hope it helps.

just to break the question down to a more simplified form, let's assume we don't have the hassle of working with dropdowns. The real issue you're having is to take a value from form 1 and have it work even after form 2 has been submitted.

If you use a hidden field on form 2, of the same name as form 1, and populate it based on both form 1 and form 2's POST data, it will be "remembered"

<? // mostly html with a bit of php. ?>
<form id="f1" method="POST">
    <input type ="text" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
    <input type="submit">
</form>


<form id="f2" method="POST">
    <input type="hidden" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
    <input type ="text" name="f2val" value="<?= htmlspecialchars( array_key_exists( 'f2val', $_POST )?$_POST['f2val']:'' ); ?>">
    <input type="submit">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM