Javascript POST无法正常工作-将Javascript数组发送到PHP [英] Javascript POST not working - Sending Javascript array to PHP

查看:78
本文介绍了Javascript POST无法正常工作-将Javascript数组发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道SO和网络上的条目很少,但是我却无法工作-任何帮助将不胜感激.

I know there a fair few entries on SO and the web on this however I just can't get to work - any help would be appreciated.

所以我有一个用Javascript编写的数组,我正尝试将其传递给PHP.

So i have an array in Javascript which I'm trying to pass on to PHP.

我要先发布一个JS函数,所以:

I've got a little JS function to first POST it, so:

function sendToPHP() {
$.post("index.php", { "variable": toSearchArray });
}

然后在页面上,我有了PHP:

Then down the page, I have the PHP:

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

*这里有照片供我检查.

*The prints just there for me to check.

任何想法-仅供参考,我正在使用MAMP,因此它是localhost:8888/index.php.可能会导致网址不正确的问题吗?

Any ideas - fyi I'm using MAMP so its localhost:8888/index.php. Could this be causing issues in that the URL is not correct?

谢谢.

推荐答案

您对ajax的工作方式有误解.尽管jquery使它变得容易,但它仍然不是自动的.您应该只找到有关带有jquery的ajax的教程,但是如果您只想向php发送一个数组并在屏幕上看到输出,则可以执行以下操作:

You have a misunderstanding about how ajax works. Although jquery makes it easy, it is still not automatic. You should just find a tutorial about ajax with jquery, but if you want to just send an array to php and see the output on screen, something like this would work:

index.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>

catcher.php:

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);
}

最好使用2个文件,其中一个供用户加载以启动ajax调用,另一个用于处理ajax调用.发送数组的工作原理相同,只是将获取文本框的值替换为发送数组.

It is better to use 2 files, one for the user to load that initiates the ajax call and one page to handle the ajax call. Sending an array works the same, just replace getting the textbox value with sending an array.

这篇关于Javascript POST无法正常工作-将Javascript数组发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆