weixin_33711647 2013-08-15 12:58 采纳率: 0%
浏览 30

AJAX变量处理

I need to create an ajax processing file, where this javascript is to send the variable myGroupId to, then send it back to my edit.php file where I can then run a query off of the returned file.

I am not experienced with ajax, so I need to know how the processing file should look regarding the javascript variable being converted to a php variable. I know I need to create another folder to send the javascript variable.

I just need to know how to do the ajax processing file (or whatever you want to call it).

Here is my javascript:

 <script type="application/javascript">
    $(document).on("click", ".open-EditRow", function () {
    var myGroupId = $(this).data('id');
    $(".modal-body #groupId").val( myGroupId );

    // ajax call
    var url = "?groupId=" + encodeURIComponent(myGroupId);
    $.get(url, function(data){
    // do something here
    });
      });
  </script>

I'll call the separate file ajaxProcessing.php. Now if someone could show me how that file should look to convert myGroupId to a php variable, I would be grateful.

When it is sent back to my edit.php file, I should be able to reference it as $_GET['groupId'].

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • weixin_33704234 2013-08-15 13:14
    关注

    Adapted from the jQuery documentation (http://api.jquery.com/jQuery.get/):

    $.get(url, {GroupID: myGroupId})
    .done(function(data) {
      alert("Data Loaded: " + data);
    });
    

    However, if you're passing data to be processed by this script, you should really be using POST:

    $.post(url, {GroupID: myGroupId})
        .done(function(data) {
          alert("Data Loaded: " + data);
        });
    

    At any rate, you could get the resulting variable on your ajax processing file by using $variable = $_GET['GroupID']; (or $_POST['GroupID'])

    NOTE: To prevent SQL Injection, you must sanitize your $variable, either through mysqli_real_escape string or uses prepared mysqli statements More specifically, don't create a query like "Select * FROM table_name WHERE GroupID= $variable" (I know my syntax is incorrect here, but you get the idea).

    Also, make sure you're using mysqli or PDO - mysql is deprecated.

    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。