Javascript 如何在PHP中将js整数值作为整数传递?

Javascript 如何在PHP中将js整数值作为整数传递?,javascript,php,json,Javascript,Php,Json,有人知道如何在PHP中将js整数值作为整数传递吗??我喜欢jQuery,但在这种情况下,请不要给我jQuery建议。你把服务器端变量和客户端变量混淆了 您需要将数据传输到服务器,否则服务器只知道变量名,即字符串 <script type="text/javascript"> var int_val = 111; </script> <?php $js_int_val_in_php ='<script>document.write(int

有人知道如何在PHP中将js整数值作为整数传递吗??我喜欢jQuery,但在这种情况下,请不要给我jQuery建议。

你把服务器端变量和客户端变量混淆了

您需要将数据传输到服务器,否则服务器只知道变量名,即字符串

<script type="text/javascript">
    var int_val = 111;
</script>

<?php
    $js_int_val_in_php ='<script>document.write(int_val);</script>';
    echo $js_int_val_in_php; // 111
    echo '<br>';
    echo gettype($js_int_val_in_php); // string  
    // but I want it as an integer
    // even settype(); does not help!!
?>

我想这里已经有人问过这个问题:

如果需要,还可以在此处查看PHP类型转换:

也许有这样的东西

<script>
var int_val=111;
function doAjax()
{
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","index.php?v="+int_val,true);
xmlhttp.send();
}
</script>
<a href="javascript:doAjax();">click here to send data to server</a>
<?php
$int_val=''
if (isset($_GET['v']))
{$int_val=intval($_GET['v']);}
//now you can access the `int_val` after the JS function ran
?>

我还没有测试过,但我相信类型转换应该可以做到,不要忘记看一下我首先发送的URL

你要么提交表单,要么提出ajax请求,即你不能将java脚本变量值分配给php变量。这是因为PHP是服务器端脚本语言,javascrpt是客户端脚本语言。如果服务器端执行需要php中的JavaScript值,那么可以使用ajaxWelcome来实现。你可以设定值。但是我想你对这应该如何工作的想法是错误的,JavaScript在浏览器中执行,而不是在PHP中执行。总是一个快乐的人:-我会在下一次发布之前进行测试,不是100%确定这只会起作用,你实际上不是在键入int_val,而是在尝试键入字符串文档。writeint_val$toInt实际上等于0。试着做一个var_dump$toInt来看看。patrick,你是对的,它给我的类型是整数,但值是零。与Torean的链接相同的问题
<script type="text/javascript"> var int_val = 111</script>
<?php
    $js_int_val_in_php = '<script>document.write(int_val);</script>';
    echo $js_int_val_in_php; // 111
    echo '<br>';
    $toInt = (int) $js_int_val_in_php;
    echo '<br>';
    echo gettype($toInt); // string  
?>