douzhun4124 2014-10-06 21:37
浏览 45
已采纳

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

I am trying to send data to PHP using JQuery ajax. However, I am only able to the send the first key=value to PHP, the second key=value echo as blank.

For example:

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

How can I pass multiple key value pair to PHP from 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

$env = filter_input(INPUT_GET, 'functionName');
echo $env
$username = filter_input(INPUT_GET, 'username');
echo $username;
  • 写回答

2条回答 默认 最新

  • douju1280 2014-10-06 21:41
    关注

    You're building your data wrong. it's basically equivalent to a URL query string, which means you need to use & as the separator, not a ,:

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?