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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配