首页 > 解决方案 > 从浏览器中检索保存的 javascript 并转换为 php

问题描述

在浏览器中将表单数据保存到 localStorage。我在检索时遇到问题,将其转换为 php。

示例代码无法进行转换。这不是询问服务器端和客户端之间差异的重复条目,尽管这显然是我遇到问题的原因。我需要的是关于如何克服问题的建议,并提供一些示例脚本来向我展示如何获得所需的结果。我对 ajax 的了解是不存在的,并且查看了各种教程后,我看不出它如何适合脚本来代替最后的 php 行(这显然是错误的)。我广泛使用 php,很少使用 javascript,除非在这种情况下。

<?php
  $cust_no=$_POST['cust_no'];
  $cust_id=$_POST['cust_id'];
  $cust_name=$_POST['cust_name'];
  $cust_addr=$_POST['cust_addr'];
  $cust_all=$cust_no."|".$cust_id."|".$cust_name."|".$cust_addr;
?>
<!DOCTYPE html>
<html>
  <body>
    <div id="result"></div>
    <form method = "POST" action = "" name="maint_form">
      <input type="text" name="cust_no" size="10" value="<?php print $cust_no;?>">
      <input type="text" name="cust_id" size="10" value="<?php print $cust_id;?>">
      <input type="text" name="cust_name" size="10" value="<?php echo  $cust_name;?>">
      <input type="text" name="cust_addr" size="10" value="<?php print $cust_addr;?>">
      <p><input type="submit" value="Continue"></p>
    </form>
    <script>
      var customer = <?php echo json_encode($cust_all); ?>;
      if (typeof(Storage) !== "undefined") {
        localStorage.setItem("custall", customer);
        document.getElementById("result").innerHTML = localStorage.getItem("custall");
      } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
      }
    </script>
    <?php
      $all_delim = '<script>document.getElementById("result").innerHTML =  localStorage.getItem("custall");</script>';
      echo "all_delim=".$all_delim;
    ?>
  </body>
</html>

标签: javascriptphphtml

解决方案


推荐阅读