javascript - 如何将 JS 值传递给 PHP 参数
问题描述
在此处添加的代码中,我将值从 PHP 参数传递给 JS 参数:两个参数都称为“stv”。
我想知道我该如何做相反的事情?
谢谢!
<script>
var stv="<?php echo $stv; ?>";
</script>
解决方案
如果文档中已经使用 jquery,则使用 jquery 发送带有 ajax 的请求。如果您没有添加 Jquery,您可以使用脚本标签将其添加到 html 正文的末尾并从 google 获取下载地址:
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
function ajax(value) {
var x = {
'info': value //data to send can be json or something else
};
$.ajax({
url: 'index.php', //page where to send
type: 'POST', //type of request
data: x, // data to send
success: function(data) { //what happends when request is success
try {
getAjPromise(JSON.parse(data));
} catch (e) {
console.log("error");
}
}
});
}
在 PHP 中检查是否有 $_POST['info'],获取值并执行某些操作
推荐阅读
- windows - Pickled Matplotlib 3D 缺乏交互功能
- jenkins - SonarQube 扫描仪执行错误 [Windows - Linux 主从]
- javascript - 如何从包含特定单元格值的 Web 表中获取整行
- kubernetes - 与 Pod 自动缩放的 Pubsub 数据关联
- java - 如何将char转换为int并分配给java中的其他对象
- c# - 根据具体对象的 Color 属性设置 WPF DataGridRow 背景颜色
- r - 从多个 csv 文件导入数据集的问题
- python - 如何使用 itemgetter 对列表进行排序
- spring-boot - Spring批处理作业执行错误:无法将NULL插入(BATCH_JOB_EXECUTION.START_TIME)
- javascript - 如何通过 CloudFront 实现分段上传?