Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将数据从Javascript传递到PHP,反之亦然?_Php_Javascript - Fatal编程技术网

如何将数据从Javascript传递到PHP,反之亦然?

如何将数据从Javascript传递到PHP,反之亦然?,php,javascript,Php,Javascript,如何将Javascript脚本请求传递给PHP页面并向其传递数据?然后如何让PHP脚本将数据传递回Javascript脚本 client.js: data = {tohex: 4919, sum: [1, 3, 5]}; // how would this script pass data to server.php and access the response? server.php: $tohex = ... ; // How would this be set to data.tohe

如何将Javascript脚本请求传递给PHP页面并向其传递数据?然后如何让PHP脚本将数据传递回Javascript脚本

client.js:

data = {tohex: 4919, sum: [1, 3, 5]};
// how would this script pass data to server.php and access the response?
server.php:

$tohex = ... ; // How would this be set to data.tohex?
$sum = ...; // How would this be set to data.sum?
// How would this be sent to client.js?
array(base_convert($tohex, 16), array_sum($sum))

您可以将数据从PHP传递到javascript,但从javascript获取数据到PHP的唯一方法是通过AJAX


原因是,您可以通过PHP构建有效的javascript,但要将数据传输到PHP,您需要再次运行PHP,并且由于PHP只运行以处理输出,因此您需要重新加载页面或异步查询。

有几种方法,最突出的是获取表单数据或获取查询字符串。这里有一个使用JavaScript的方法。当您单击链接时,它将调用_vals('mytarget','theval'),它将提交表单数据。当页面发回时,您可以检查此表单数据是否已设置,然后从表单值中检索它

<script language="javascript" type="text/javascript">
 function _vals(target, value){
   form1.all("target").value=target;
   form1.all("value").value=value;
   form1.submit();
 }
</script>

因此,当您单击使用JavaScript的链接时,它将发布表单数据,当页面从该单击返回时,您可以从PHP中检索它。

从PHP传递数据很容易,您可以使用它生成JavaScript。另一种方法有点困难——您必须通过Javascript请求调用PHP脚本

示例(为了简单起见,使用传统的事件注册模型):


函数callPHP(params){
var httpc=new XMLHttpRequest();//为清晰起见,进行了简化
var url=“get_data.php”;
open(“POST”,url,true);//作为POST发送
httpc.onreadystatechange=function(){//在状态更改时调用函数。
如果(httpc.readyState==4&&httpc.status==200){//完成且无错误
alert(httpc.responseText);//此处进行一些处理,或者对响应执行任何操作
}
};
httpc.send(参数);
}
无论
get_data.php
生成什么,都将出现在httpc.responseText中。错误处理、事件注册和跨浏览器XMLHttpRequest兼容性留给读者作为简单的练习;)


另请参见更多示例,我将使用Ajax(真正的XMLHttpRequest)作为格式,将Ajax(真正的XMLHttpRequest)作为客户端->服务器机制。

另一种从php到javascript交换数据的方法是使用cookie,您可以用php保存cookie,并通过javascript读取,为此,您不必使用表单或ajax,使用Cookie是一种简单的方法。您可以使用jquery和插件,或者创建自己的插件。 举例来说,使用Jquery+Jquery.cookie

   <script>
   var php_value = '<?php echo $php_variable; ?>';

   var infobar_active = $.cookie('php_value');
   var infobar_alert  = any_process(infobar_active);

    //set a cookie to readit via php
   $.cookie('infobar_alerta', infobar_alerta );

   </script>

   <?php 

    var js_value = code to read a cookie

   ?>

var php_值=“”;
var infobar_active=$.cookie('php_值');
var infobar_alert=任何_进程(infobar_活动);
//设置cookie以通过php读取它
$.cookie('infobar\u alerta',infobar\u alerta);
我发现这种有用的服务器端和混合框架:


很长一段时间以来,我一直在使用Ashley的RSJS脚本毫无问题地更新HTML中的值,直到遇到JQuery(ajax、load等)

前几天我遇到了类似的问题。比方说,我想将数据从客户端传递到服务器,并将数据写入日志文件。以下是我的解决方案:

我的简单客户端代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
   <title>Test Page</title>
   <script>
    function passVal(){
        var data = {
            fn: "filename",
            str: "this_is_a_dummy_test_string"
        };

        $.post("test.php", data);
    }
    passVal();
   </script>

</head>
<body>
</body>
</html>

测试页
函数passVal(){
风险值数据={
fn:“文件名”,
str:“这是一个虚拟测试字符串”
};
$.post(“test.php”,数据);
}
passVal();
和服务器端的php代码:

<?php 
   $fn  = $_POST['fn'];
   $str = $_POST['str'];
   $file = fopen("/opt/lampp/htdocs/passVal/".$fn.".record","w");
   echo fwrite($file,$str);
   fclose($file);
?>


希望这对你和未来的读者有用

但事实并非如此,正如我刚才所展示的,我几乎每天都在使用表单提交方法从JavaScript向服务器端获取数据。通过页面重新加载或异步查询?值得注意的是,它只适用于相对URL,因为相同的源策略:不完全正确,它适用于相同的域,因此绝对URL也适用。如果目标php页面已启用,它将跨域工作。如果CORS不是一个选项,也可以使用。这不起作用。您现在正在设置禁止的标题(内容长度)。现在什么能起作用?@traducerad:很确定现在这种情况是自动发生的,不需要设置它,很好的捕获。我有以下错误。有人知道解决办法吗?(索引):77未捕获引用错误:$未在passVal((索引):77)中定义->$.post(“test.php”,data);at(index):79-for->passVal();找到解决方案:添加,我发现无法在php端接收帖子内容。php和javascript应该在相同或不同的文件中?(我两次都尝试了..记录文件已形成,但其中没有内容)
   <script>
   var php_value = '<?php echo $php_variable; ?>';

   var infobar_active = $.cookie('php_value');
   var infobar_alert  = any_process(infobar_active);

    //set a cookie to readit via php
   $.cookie('infobar_alerta', infobar_alerta );

   </script>

   <?php 

    var js_value = code to read a cookie

   ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
   <title>Test Page</title>
   <script>
    function passVal(){
        var data = {
            fn: "filename",
            str: "this_is_a_dummy_test_string"
        };

        $.post("test.php", data);
    }
    passVal();
   </script>

</head>
<body>
</body>
</html>
<?php 
   $fn  = $_POST['fn'];
   $str = $_POST['str'];
   $file = fopen("/opt/lampp/htdocs/passVal/".$fn.".record","w");
   echo fwrite($file,$str);
   fclose($file);
?>