同一php文件中,如何将Js代码中的变量,传递到php代码中。
<script language="javascript">
function test(){
var num = document.getElementById("category").value;
}
</script>
<?php
echo num;
?>
急求:将上面js代码中的num,能够在下面php代码中输出。最好将实现代码贴出来。
你没理解 js/php 运行时的顺序激答弊和逻辑.
js仅在 浏览器中运行.
php 仅在服务器端运行.
2者交互, 通常通过 http get/post 协议进行交互.
因此, 要将 js 变量传输到 php, 需通过 get/post 将参数传入明族.
譬如:
<script>
function test(){
var x="abc";
$.ajax("test.php?x="+x);
}
</script>
而 test.php 中, 通过 $_REQUEST["x"] 即可拿到js 请求举搭过来的变量.
1. 在JS中设置COOKIE:document.cookie="誉培cookiename="+value;
2. 在PHP中用$_COOKIE["cookiename"]的方式读取。
[PHP]
<script>
var name="num";
var value=document.getElementById("category").value;
document.cookie="num"+"="+value;
</script>庆卖唯
<?php
echo "$_COOKIE[‘num’]"配卖;
?>
在js中用post或者get