Javascript POST 不起作用 - 将 Javascript 数组发送到 PHP [英] Javascript POST not working - Sending Javascript array to PHP

查看:28
本文介绍了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.这是否会导致 URL 不正确的问题?

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

<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天全站免登陆