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 ""

}
查看全部
weixin_33738982
?Briella
2013/12/23 03:12
  • php
  • ajax
  • send
  • 点赞
  • 收藏
  • 回答
    私信

1个回复