我是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'/>";
}
?>
你可以檢查瀏覽器控制檯(鉻-F12),看看是否有錯誤? – Abhi 2014-08-28 22:09:11
你爲什麼將JS代碼發佈爲圖片? – developerwjk 2014-08-28 22:11:33
懶懶地輸入它大聲笑 – EasyBB 2014-08-28 22:40:29