2014-08-28 112 views
-2

我是ajax和jquery的初學者,我面臨着我的jQuery代碼的問題。當我從這個表單發佈一些數據到php頁面,當結果從php返回後,jquery不會做任何事情。jquery在使用ajax時無法正常工作

代碼如下,請幫助。

<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'/>"; 
} 
?> 
+0

你可以檢查瀏覽器控制檯(鉻-F12),看看是否有錯誤? – Abhi 2014-08-28 22:09:11

+5

你爲什麼將JS代碼發佈爲圖片? – developerwjk 2014-08-28 22:11:33

+0

懶懶地輸入它大聲笑 – EasyBB 2014-08-28 22:40:29

回答

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

+0

$ .ajax()和$ .post()方法之間的區別 – user3220457 2014-08-28 22:43:08

+0

'$ .ajax'只是一個長期的方法。否則,他們是一樣的。 – 2014-08-29 00:30:42

+0

它總是去其他情況下爲什麼? – user3220457 2014-08-29 10:36:29