dongwei3172 2015-04-30 16:32
浏览 31
已采纳

使用PHP变量将多个参数传递给AJAX JQuery [关闭]

I am trying to pass 2 values to a AJAX JQuery function.

My function is triggered onChange of a drop down list that which gets data from a DB and runs through a while loop and outputs it with PHP, it looks like this:

$dropDown = "<label for='drop_down_list'>Drop Down: </label>
                <select name='drop_down_list' id='drop_down_list' onChange='getInfo(this.value, ". $val2 .")'>
                    <option selected='selected' disabled='disabled' value=''>Options</option><br/>
                    " . $dropDownOptions . "
                </select></br>";

echo $dropDown;

Here I am trying to send the selected options value, as well as a value I have in a PHP variable (which does have a value because I echo it out elsewhere).

My getinfo function looks like this:

function getInfo(val1, val2) {
    $.ajax({
    type: "POST",
    url:  "get_info.php",
    data: 'value1='+val1+'&value2'+val2,

    success: function(data){
            $("#info").html(data);
    }
   });
};

I try to output the values I return from get_info.php like this:

$value1 = $_POST['value1'];
$value2 = $_POST['value2'];

echo "First Value: " .$value1;
echo "<br>";
echo "Second Value: " .$value2;

My output is:

First Value: 1
Second Value:

I have error reporting on and I am getting an undefined error on $value2 so I think somewhere that PHP variable is not passing properly, I'm not quite sure if that is how I am supposed to properly pass the PHP value I need, am I doing something wrong here?

How can I pass my PHP variable into an AJAX JQuery function and and get the output the values properly?

example:

First Value: 1
Second Value: 2
  • 写回答

3条回答 默认 最新

  • douhuang5331 2015-04-30 16:37
    关注

    You missed an equal = in your js function:

    function getInfo(val1, val2) {
      $.ajax({
        type: "POST",
        url:  "get_info.php",
        data: 'value1='+val1+'&value2='+val2,
    
        success: function(data){
            $("#info").html(data);
        }
      });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条