?Briella 2013-12-23 03:12 采纳率: 0%
浏览 14

AJAX未将数据发送到PHP

In this script, data gets sent, but it does not send the updated value of the variable. Hard-coded data gets sent as expected. How can I solve this or go around it?

    username = '';  // supposedly these variables are global
    tree_name = ''; // I also tried plugging in- $('#username').data("username") -directly.. same results.

    $(function() {
        username = $('#username').data("username");    // updating value
        tree_name = $('#tree_name').data("tree_name"); // idem....
    });


    var options = {
        type: "POST",
        url: "/decision/p_tree2/",

        data: {
            username: username,     // this should send the data
            tree_name: tree_nam     // ...
        },

    success: function(response) {
    console.log(response);      
    }
};

$("form").ajaxForm(options);

PHP:

public function p_tree2 (){
    $data = Array();
    $data['username'] = $_POST['username'];
    $data['tree_name'] = $_POST['tree_name'];

    echo print_r($data);    // result: both $data['username'] and $data['tree_name'] equal ""

}
  • 写回答

1条回答 默认 最新

  • 关注

    Reformat your code like following. Also correct the typo you have in tree_name in data. You used tree_nam.

    $(function() {
        var username, tree_name,options;
    
        username = $('#username').data("username");
        tree_name = $('#tree_name').data("tree_name");
    
        console.log(username); //make sure
        console.log(tree_name); // console outputs what you need to pass 
    
        options = {
            type: "POST",
            url: "/decision/p_tree2/",
    
            data: {
                username: username,     // this should send the data
                tree_name: tree_name     // ***correct this typo***
            },
    
            success: function(response) {
                console.log(response);
            }
        };
    
        $("form").ajaxForm(options);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料