Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The object of my script is to save form output to local storage in the browser if the signal does not allow connection to server, using an iPad in remote locations. Then subsequently getting the data back when the signal is restored.
I have been a couple of weeks trying to get the local storage to feed back onto my page in php format. I get it back OK as a javascript but I cannot work out how to send to the server and then get it back to the page. I have copied a few different bits of script from websites, including ajax, but I really do not understand javascript enough to make it work. None of the javascript is my own creation. The script can be downloaded and run to show the script working.
The code I originally gave has been changed by someone. It will not work at all now because the script tags have been removed.

UPDATE: I have now changed the code sample back to original with the changes suggested for POST instead of GET.

What I have tried:

<?php
$php_result=$_POST['result'];
print "This is the php result from server <br>result=".$php_result."<br><br>";
print "This is the javascript output from storage...";
$customer=$_POST['customer'];

?>
<!DOCTYPE html>
<html>
<body>
<div id="result"></div>
<form method = "POST" action = "" name="cust_form">
<p>Type anything in the box</p>
<input type="text" name="customer" size="10" value="<?php print $customer;?>">

<p><input type="submit" value="Continue"></p>
</form>
<script>
var customer = <?php echo json_encode($customer); ?>;
if (typeof(Storage) !== "undefined") {
localStorage.setItem("cust", customer);

document.getElementById("result").innerHTML = localStorage.getItem("cust");

xhttp.onreadystatechange = function() {
getElementById("result").innerHTML = localStorage.getItem("cust");

  if (this.readyState == 4 && this.status == 200) {

  }
};


xhttpRequest.open('POST', 'http://www.jhw1.com/local_store.php', true);
xhttpRequest.send();
};


</script>
</body>
</html>
Posted
Updated 29-Apr-18 0:07am
v6
Comments
Member 13802778 29-Apr-18 5:55am    
Yes, php and javascript changed to read 'POST' instead of 'GET'.
Member 13802778 29-Apr-18 6:07am    
Do I need some kind of utton to send to server?
Member 13802778 29-Apr-18 6:08am    
Should read 'Button'

1 solution

HTTP's GET does not send hidden fields to the server automatically, so you have two options...
1. add it a query param
2. use POST

Your second problem is here:
HTML
<div id="result"></div>
<form method = "POST" action = "" name="cust_form">
  <p>Type anything in the box</p>
  <input type="text" name="customer" size="10" value="<?php print $customer;?>">
  <p><input type="submit" value="Continue"></p>
</form>

The 'result' you are using not a hidden field (means an input with type hidden), but a div, and it is also outside of the form...
You should use markup lie this:
HTML
<div id="result"></div>
<form method = "POST" action = "" name="cust_form">
  <p>Type anything in the box</p>
  <input type="text" name="customer" size="10" value="<?php print $customer;?>">
  <p><input type="submit" value="Continue"></p>
  <input type="hidden" id="result">
</form>


-- EDIT --
A proposed solution:
sample.php
PHP
<?php
  $php_result = $_POST['result'] ? $_POST['result'] : 'server';
  $customer = $_POST['customer'];
  echo "From server =" . $php_result;
  echo "From client = " . $customer;
?>

<!DOCTYPE html>
<html>
  <body onload="initStorage()">
    <form method="POST" action="sample.php" onsubmit="saveStorage()">
      <p>Type anything in the box</p>
      <input type="text" name="customer" size="10" value="<?php echo $customer;?>">
      <input type="hidden" name="result" value="<?php echo $php_result;?>">
      <input type="submit" value="Continue">
    </form>
  </body>

  <javascript>
    function initStorage()
    {
        // store 'result' on local storage
    }

    function saveStorage()
    {
        // save local storage to 'result'

        return(true); // to continue...
    }
  </javascript>
</html>
 
Share this answer
 
v4
Comments
Member 13802778 29-Apr-18 5:49am    
Thanks for your reply. I have now changed GET to POST and tried it, but still no output. The URL where I am testing the script is http://www.jhw1.com/local_store.php
Kornfeld Eliyahu Peter 29-Apr-18 5:51am    
The question is: have you changed the PHP too?
Member 13802778 29-Apr-18 6:18am    
Sorry, I'm new to this website. I answered in the wrong place. Please see the revised code above.
Kornfeld Eliyahu Peter 29-Apr-18 6:38am    
In the standard HTTP POST hidden field should be inside the form to be posted to the server...
Member 13802778 29-Apr-18 6:48am    
I understand that, but I am hopeless with javascript so I do not know how to do that. I just tried moving the end form tag after the end scrpt tag but that di not make any difference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900