使用ajax时jquery无法正常工作

时间:2014-08-28 22:01:43

标签: php jquery ajax

我是ajax和jquery的初学者,我遇到了jquery代码的问题。当我从这个表单发布一些数据到php页面时,从php返回结果后,jquery没有做任何事情。

代码如下,请帮忙。

<h2> Add New Offer</h2>
<form method="post" id="postoffer" >
<fieldset>
<legend>Add Offer</legend>
<table>
<tr><td>Offer Name:</td><td><input  type="text" name="offer" ></td></td>
<tr><td>Description:</td><td><input type="text"name="des" > </td></td>
<tr><td>    Offer Link:</td><td> <input  type="text" name="link" ></td></td>
<tr><td>    Pay Per weak:</td><td><input  type="text" name="pay" ></td></td>
</table>
<button class="button2" id="addoffer" align="Center">ADD</button> 
</fieldset>
</form>

查询页面

window.event.returnValue = false;
 $("#addoffer").click(function(e) {

$.post('addoffers.php', $('#postoffer').serialize(), function(data) {
                var code = $(data)[0].nodeName.toLowerCase();
            if(code == 'success') {
                window.location="admin.php#confirm";}
             if(code=='error'){
                var id = parseInt($(data).attr('id'));
                switch(id) {
                    case 0:
                        $('#msg').html('Please fill all the field for add offer.');
                        window.location="admin.php#erorr";
                            break;
                    case 1:
        $('#msg').html('Sorry!This offer name has been already used.Plz change it');
                        window.location="admin.php#erorr";
                        break;
                    case 2:
                    {$('#msg').html('An error occurred, please try again.');
                        window.location="admin.php#erorr";
                            break;}
                    default:
                        $('#msg').html('An error occurred, please try again.');
                        window.location="admin.php#erorr";
                }
            }
        });
        return e.preventDefault();
    });
});

php页面

require_once 'config.php';
if(isset($_POST['offer'], $_POST['des'], $_POST['link'], $_POST['pay'] , $_POST['stime'] , $_POST['smonth']) )
{
    $offername=$_POST['offer']; $description=$_POST['des'];$link=$_POST['link'];$description=str_replace("'",'39',$description);
    $pay=$_POST['pay'];
    $rating=$_POST['rate'];
    $stime=$_POST['stime'];
    $smonth=$_POST['smonth'];
    $data=mysql_query("select fname from offer where fname='$offername'");
    $row=mysql_num_rows($data);
    if($row==0)
    {   //echo $offername."<br>".$description."<br>".$link;
        $sql="insert into offer values ('NULL','$offername','$description','$link','$pay','$stime','$smonth')";
        $result=mysql_query($sql);
        //echo $result;
        if($result)
        {echo "<success />";}
        else
        {
            echo "<error id='1'/>";}
    }
    else
    {
        echo "<error id='2'/>";
    }
}
else
{
echo "<error id='0'/>";
}
?>

1 个答案:

答案 0 :(得分:0)

您应该在这里学到的教训是:

保持简单。

现在,我将向您展示如何正确地完成它,但由于您只是根据4种不同的结果重定向到2个不同的页面 - 您甚至可能不应该使用AJAX。

首先,将您的PHP脚本更改为echo 'success';,或者如果出现错误,请回显该数字。您的错误日志

然后,作为一个初学者,你将自己做一个很好的学习速记版本,首先:

    $.ajax({ 
        url: 'addOffers.php', 
        data: { offer : $('input[name="offer"]').val(), 
                des : $('input[name="offer"]').val(), 
                link : $('input[name="link"]').val(), 
                pay: $('input[name=""]').val() 
           }, // It would be a good thing if you gave those inputs IDs and selected by the IDs, instead.
    method: "POST", 
    success: function(response) {
        if(response == 'success') { 
             window.location "admin.php#confirm"; // redirecting honestly defeats the purpose of using AJAX --- 
         } else {
              switch(response){
                     case 0: 
                        //etc .... your code fits in, here.
                       break;
你知道吗?它更容易---你不需要在你的ajax中使用xml ......