dpka7974 2018-04-09 19:15
浏览 60
已采纳

如何使用jQuery Ajax.Request将多个变量类型传递给php

I need to pass an array and a string to my php page. But I cannot figure out how to do this. Here is my code:

Javascript:

function processData(myVar){
  new Ajax.Request('myPage.php', {
    type: 'post',
    data: {myCmd: 'ProcessIt', addData: myVar},
      onSuccess: function(transport) {
        return transport.responseText;
      }
  });
}

PHP:

<?php
if (empty($_POST)){
 // $_POST is always empty.  Even though the type is array.
}

As my comment says, $_POST is always empty. I have tried many ways to get some values out of it, but it is just empty.

Can someone show me what I am doing wrong?

  • 写回答

2条回答 默认 最新

  • dongsonglian7303 2018-04-09 19:53
    关注

    If you want send POST with prototypejs this is the form in ajax

    new Ajax.Request('index/index.php', {
      method: 'POST',
      parameters: {
        'options[]': JSON.stringify({"array1" : "option1", "array2" : "option2"}),
       'option': "test"
      },
      onSuccess: function(transport){
        console.log(transport);
      }
    });
    

    I hope this help you

    index.php

    if(empty($_POST)){
      echo "BAD";
    }else{
      echo json_encode($_POST);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题