Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

jquery - Ajax Success and Error function failure

I am having trouble getting my jQuery ajax to work properly. It directs to the PHP page to update the database, but never returns back to the script for the success or error options.

My code is below:

$(document).ready(function(){  
        $("form#updatejob").submit(function() {  
            function textreplace(x) {return text.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&");}
            // we want to store the values from the form input box, then send via ajax below
            var job     = $("#job").attr("value");
            var description     = $("#description").val();
            description.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&");
            var startDate   = $("#startDate").attr("value");
            var releaseDate = $("#releaseDate").attr("value");  
            var status  = $("#status").attr("value"); 
            $.ajax({
                beforeSend:textreplace(description),
                type: "POST",  
                url: "updatedjob.php",
                data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
                success: function(){  
                    $("form#updatejob").hide(function(){$("div.success").fadeIn();});  
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    alert("Status: " + textStatus); alert("Error: " + errorThrown); 
                }       
            });
            return false;  
        });  
});

And the PHP:

<?php 
    include("connect.php"); 
    $job = trim($_POST['job']); 
    $startDate = trim($_POST['startDate']); 
    $releaseDate = trim($_POST['releaseDate']); 
    $mysqlstartdate = date('Y-m-d', strtotime($startDate)); 
    $mysqlreleasedate = date('Y-m-d', strtotime($releaseDate)); 
    $description = trim($_POST['description']); 
    $status = trim($_POST['status']); 
    $update = "UPDATE jobs SET startDate = '$mysqlstartdate', releaseDate = '$mysqlreleasedate', description = '$description', status = '$status' WHERE jobID = '$job' "; 
    $rsUpdate = mysql_query($update);
// or die(mysql_error()); mysql_close(); 
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

$.ajax({
    beforeSend: function() { textreplace(description); },
    type: "POST",  
    url: "updatedjob.php",
    data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
    success: function(){  
        $("form#updatejob").hide(function(){$("div.success").fadeIn();});  
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
        alert("Status: " + textStatus); alert("Error: " + errorThrown); 
    }       
});

The beforeSend property is set to function() { textreplace(description); } instead of textreplace(description). The beforeSend property needs a function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...