简体   繁体   English

无法从jquery ajax向php发送数据?

[英]Unable to send data to php from jquery ajax?

I am trying to send data to PHP using JQuery ajax. 我正在尝试使用JQuery ajax将数据发送到PHP。 However, I am only able to the send the first key=value to PHP, the second key=value echo as blank. 但是,我只能将第一个key=value发送到PHP,第二个key=value echo为空白。

For example: 例如:

functionName="+env works but not username="+username;

How can I pass multiple key value pair to PHP from JQuery AJAX? 如何从JQuery AJAX将多个键值对传递给PHP?

Jquery AJAX jQuery AJAX

var envdata="functionName="+env+",username="+username;
alert(envdata);


if(action == 'R'){
  $.ajax({
type: "GET",
url: 'getdata.php',
data: envdata,
success: function(response) {
                $("#textarea_message").val(response);

}

getdata.php 访问getdata.php

$env = filter_input(INPUT_GET, 'functionName');
echo $env
$username = filter_input(INPUT_GET, 'username');
echo $username;

You're building your data wrong. 您建立的data错误。 it's basically equivalent to a URL query string, which means you need to use & as the separator, not a , : 它基本上等效于URL查询字符串,这意味着您需要使用&作为分隔符,而不是,

var envdata="functionName="+env+"&username="+username;
                                 ^---note the &

commas mean nothing to PHP as far as url query arguments go, so your ,username=foo would become part of the functionname value. 就URL查询参数而言,逗号对PHP毫无意义,因此,您的,username=foo将成为functionname值的一部分。

A simple var_dump($_GET) would show you exactly what PHP is seeing come in. Doing that should've been your first stop when you didn't get your expected data in the script. 一个简单的var_dump($_GET)可以准确地告诉您PHP所见的内容。当您没有在脚本中获得期望的数据时,这样做应该是您的第一站。

Read about JSON 了解有关JSON的信息

$(document).ready(function() {
$('#some_button').on('click', function() {
    var checks = new Array();
    $("input[name='ls_id']:checked").each(function() {checks.push($(this).val());});
    $.ajax({
                type: 'POST',
                url: "some url",
                dataType: 'json',
                cache: false,
                data: {checks: checks, func: 9},
                success: function(data) {
                }
            });
});

}); });

getdata: 的GetData:

$json = json_decode($_POST['checks'], true);
        print_r ($json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM