javascript - 从浏览器中检索保存的 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>
解决方案
推荐阅读
- ios - 在“PSPDFViewController *”类型的对象上找不到属性“pagingScrollView”
- ionic-framework - Error while building android apk in ionic 3
- php - PHPMailer emails NOT sending sometimes because of multiple tests?
- c# - 首先使用代码上传 mvc5 应用程序文件而不在实时站点中发布文件
- kubernetes - 如何在 kubernetes 中使用 consul 作为默认 dns 或服务发现
- android - 运行时 IllegalStateException 预期开始数组,但为字符串
- javascript - 生成 10 个唯一的随机整数
- html - 3 列固定标题,可扩展高度以适应内容
- angular - 导航后角度数据不更新
- reactjs - 传入 React.Component 类型的 prop 时出现 TS 错误