douhan9748 2009-12-06 23:35
浏览 62
已采纳

jQuery AJAX Post无法正常工作

I cant not figure out why this script doesnt work. It has to do with something within the AJAX POST script/function. Right now, when I hit submit on my form it runs the php code on the same page. What it should do it is send the values of the form to project_ajax.php, then at page will return a var of success that will be true or false.

$(document).ready(function () {
    $('div#didIt').hide();
    $('form[name=adminForm]').submit(function () {
        $.post('/project_ajax.php', {
            action: $('[name=action]').val(),
            pId: $('[name=pId]').val(),
            name: $('[name=name]').val(),
            url: $('[url=url]').val(),
            summary: ('[summary=summary]').val()
        }, function (data) {
            if (data.success) {
                $('div#didIt').slideDown('slow');
            } else {
                alert('Failed SA!');
            }
        }, 'json');
        return false;
    });
});

Below is what the code for project_ajax.php...

if($_POST['action'] == "update") {
    //Prep the field for Query Entry!-----------------------------------
    $pId     = $_POST['pId'];
    $name    = trim(mysql_prep($_POST['name']));
    $status  = 1;
    $url     = trim(mysql_prep($_POST['url']));
    $tumb    = false; //False because I still need to make a script for it.
    $summary = trim(mysql_prep($_POST['summary']));
    $creater = $_SESSION['userId'];
    $created = date("Ymd");

    $q = "UPDATE " . DB_NAME . ".`projects` SET 
                    name    = '{$name}',
                    status  = '{$status}',
                    url     = '{$url}',
                    summary = '{$summary}',
                    creater = '{$creater}',
                    created = '{$created}'
              WHERE `projects`.`id` = {$pId}";
    $r = mysql_query($q, $connection);
    if ($r) {
        //Successful
        $data['success'] = true;
        $date['error']   = false;
        $date['message'] = "You are the Greatest!";

    } else {
        //Fail
        $data['success'] = false;
        $data['error']   = true;
        $date['message'] = "You can't do it fool!";

    }

} else {
    $data['success'] = false;
    $date['error']   = true;
    $data['message'] = "You Failed Stupid!";
}

echo json_encode($data);

Note: When load this page. The browser never really finsh loading. The the blue-ring on the title tab within IE8 spins as if the page never finsh loading.

  • 写回答

1条回答 默认 最新

  • douzi5214 2009-12-06 23:38
    关注

    On this line:

      summary: ('[summary=summary]').val()},
    

    You have a missing $ which represents the jQuery function. Corrected:

    summary: $('[summary=summary]').val()},
    

    Because you have a Javascript error, the execution is terminated. Thus the loading takes forever.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么