Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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 POST不工作-将Javascript数组发送到PHP_Php_Jquery_Ajax_Post - Fatal编程技术网

Javascript POST不工作-将Javascript数组发送到PHP

Javascript POST不工作-将Javascript数组发送到PHP,php,jquery,ajax,post,Php,Jquery,Ajax,Post,我知道在SO和网络上有相当多的条目,但我就是不能去工作-任何帮助都将不胜感激 所以我有一个Javascript数组,我正试图将它传递给PHP 我有一个小的JS函数来首先发布它,所以: function sendToPHP() { $.post("index.php", { "variable": toSearchArray }); } 然后往下看,我看到了PHP: <?php $myval = $_POST['variable']; print_r ($myval);

我知道在SO和网络上有相当多的条目,但我就是不能去工作-任何帮助都将不胜感激

所以我有一个Javascript数组,我正试图将它传递给PHP

我有一个小的JS函数来首先发布它,所以:

function sendToPHP() {
$.post("index.php", { "variable": toSearchArray });
}
然后往下看,我看到了PHP:

<?php 
    $myval = $_POST['variable'];
    print_r ($myval);
    ?>

*指纹就在那里让我检查一下

任何想法-仅供参考,我正在使用MAMP,因此其本地主机:8888/index.php。这是否会导致URL不正确的问题


谢谢。

对于数组,我认为这不是正确的方法,你需要在javascript中使用JSON编码,然后在php中使用JSON解码
参考这个问题

您对ajax的工作原理有误解。尽管jquery使它变得简单,但它仍然不是自动的。您应该找到一个关于ajax和jquery的教程,但是如果您只想将一个数组发送到php并在屏幕上看到输出,类似这样的东西可以工作:

index.php

<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    //attach to the button a click event
    $('#btn').click(function(){
            //get the value from the textbox
        var txt=$('#txt').val();
            //if txt is blank, alert an error
        if(txt == ''){
            alert("Enter some text");
        } else {
                    //send txt to the server
                    //notice the function at the end. this gets called after the data has been sent
            $.post('catcher.php', {'text':txt}, function(data){
                            //now data is an object, so put the message in the div
                $('#response').text(data.message);
            }, 'json');
        }
    });
});
</script>
</head>
<body>
<input type="text" id="txt">
<input type="button" id="btn">
<pre id="response" style="overflow:auto;width:800px;height:600px;margin:0 auto;border:1px solid black;">&nbsp;</pre>
</body>
</html>

试验
$(文档).ready(函数(){
//将单击事件附加到按钮
$('#btn')。单击(函数(){
//从文本框中获取值
var txt=$('#txt').val();
//如果txt为空,则发出错误警报
如果(txt=''){
警报(“输入一些文本”);
}否则{
//将txt发送到服务器
//注意末尾的函数。它在数据发送后被调用
$.post('catcher.php',{'text':txt},函数(数据){
//现在数据是一个对象,所以将消息放在div中
$('#response').text(data.message);
}“json”);
}
});
});
catcher.php:

<?php
//if something was posted
if(!empty($_POST)){
    //start an output var
    $output = array();

    //do any processing here.
    $output['message'] = "Success!";

    //send the output back to the client
    echo json_encode($output);
}

这是打开页面时发生的情况(
index.php

  • index.php
    发出
    GET
    请求,并返回内容。
    $\u POST
    数组中没有值,因此
    print\u r()
    行不执行任何操作
  • 执行Javascript,通过AJAX向
    index.php
    发送
    POST
    请求。请注意,这是一个全新的请求,独立于原始的
    GET
    。此请求将填充
    $\u POST
    数组,但会丢弃响应
  • 希望这能说明你能做什么

    ajax.php
    而不是将变量声明为数组。把它看作是一个JavaScript对象。
    
    var-toSearchArray={}。

    打印输出的是什么?你没有对AJAX帖子的结果做任何事情,所以我不知道你为什么会期望看到我刚刚意识到的任何东西,你是否通过AJAX将数据发送到你所在的页面?ajax不是这样工作的。您通常会将ajax数据发送到另一个页面,并将第三个参数添加到$.post,这是一个回调函数,在ajax调用完成时调用该函数,并将其传递到ajax调用的输出中。在该回调函数中,您可以更新屏幕,但输出没有给出任何结果。因此,对于AJAX post,需要做些什么呢?我假设它刚刚移交了呢?试试$.post({url:“index.php”,data:{“variable”:toSearchArray}});在jquery中,您可以传递一个对象,jquery将把它序列化为有效的post负载。我太迷路了。真的不知道为什么我的东西不起作用我需要读什么?我所要做的就是向PHPHi发送一个javascript数组-好的,我看到这是可行的,但是我有2个文件-我如何将它返回到原始的数组值,即index.php-我应该只使用会话吗?它已经是index.php中的一个数组了。但是,如果希望服务器的响应(文件2,catcher.php)是一个数组,那么可以在帖子中指定一个数据类型,最有可能是“json”,作为第四个参数。然后,从第二个文件发送的任何内容都可以在php中通过json_encode运行,回调函数将得到它。我会更新这个例子来说明这一点。嗨-好的,我看到这个工作,但是,因为我有2个文件-我如何让它返回到原始的数组值,即index.php-我应该只使用会话吗?
    <?php echo json_encode($_POST);
    
    <script>
    var toSearchArray = ['some', 'array', 'with', 'values'];
    $.post('ajax.php', {
        variable: toSearchArray
    }, function(data) {
        alert(data); // here you will see the result of the ajax.php script
    });
    </script>