douzhun4124
2014-10-06 21:37
浏览 40

无法从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;

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用JQuery ajax将数据发送到PHP。 但是,我只能将第一个 key = value 发送到PHP,第二个 key = value echo作为空白。

例如:

  functionName =“+ env工作但不是用户名=”+用户名; 
   
 
 

如何 我可以从JQuery AJAX向PHP传递多个键值对吗?

Jquery AJAX

  var envdata =  “functionName =”+ env +“,username =”+ username; 
alert(envdata); 
 
 
if(action =='R'){
 $ .ajax({
type:“GET”,\  nurl:'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.

    点赞 评论
  • ds9567 2014-10-06 21:41

    Read about 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:

    $json = json_decode($_POST['checks'], true);
            print_r ($json);
    
    点赞 评论

相关推荐 更多相似问题