php给js传参数(js传输数据到php)
更新:<time datetime="2022-11-10 08:59">2022-11-10 08:59</time>
本文目录一览:
1、php如何传值给js
2、php向js传值的问题
3、PHP中怎么传参数给js函数?
php如何传值给js
很简单。。。举例
$message = "这是一个来自 php 的值。";
echo "<script language=\"JavaScript\" type=\"text/JavaScript\">\r\n<!--
alert('".$message."');\r\n//-->\r\n</script>";
你得把所有的JS代码转成PHP格式就可以了。
php向js传值的问题
原生php可以这样: test.js.php:
function getValue() {
return <?php echo $a; ?>;
}
使用某些mvc框架, 例如thinkphp, 可以让你写成类似这样:
function getValue() {
return {$a};
}
不过比较好的方式还是php生成一个json由js接收:
jQuery.ajax({
url: "xxx/getValue.php",
dataType: "json",
method: "GET",
success: function (data) {
a = data.a;
}
});
PHP中怎么传参数给js函数?
<input type="button" value="+" onclick='add("<?php echo $item['id']; ?>", "<?php echo $item['name']; ?>")' />
<script>
function add(id, name) {
alert(name);
}
</script>