2013-11-21 143 views
0

我从一些教程网站获得了表单提交的jQuery代码,但是ajax没有工作,我猜。当我提交表单时,它不会发生任何事情。我尝试了一切,但没有结果。jquery/ajax表单提交不起作用

这里是我的jQuery代码。

$("#summary").submit(function (event) { 
    event.preventDefault(); 
    var name = $("#name").val(); 
    var address = $("#address").val(); 
    var landline_number = $("#landline_number").val(); 
    var mobile_number = $("#mobile_number").val(); 
    var profile_email = $("#profile_email").val(); 
    var profile_statement = $("#profile_statement").val(); 
    var dataString = 'name='+name+'&address='+address+'&landline_number=' + landline_number + '&profile_email=' + profile_email + '&prof ile_statement=' + profile_statement + '&mobile_number=' + mobile_number; 
    $.ajax({ 
     type: 'POST', 
     data: dataString, 
     url: 'test2.php', 
     success: function (data) { 
      $('#hello').html(data); 
     } 
    }); 
}); 

HTML代码

<form name="summary" id="summary" action="" method="POST"> 
<div class="profile-header"> 

    <table width="740"> 
    <tr> 
    <td> 
    <h2>Personal Detail:</h2> 
    </td> 
    <td> 
    <input type="text" id="name" name="name" /> 
    </td> 
    </tr> 
    </table> 
    </div> 
    <div class="profile-table-hide"> 

    <table width="740"> 
    <tr> 
    <td width="297"><p>Address Location</p></td> 
    <td width="10" class="divider"></td> 
    <td width="428"><input id="address" type="text" name="address" /></td> 
    </tr> 
    <tr> 
    <td><p>Landline Number</p></td> 
    <td class="divider">&nbsp;</td> 
    <td><input type="text" id="landline_number" name="landline_number" /></td> 
    </tr> 
    <tr> 
    <td><p>Mobile Number</p></td> 
    <td class="divider"></td> 
    <td><input type="text" id="mobile_number" name="mobile_number" /></td> 
    </tr> 
    <tr> 
    <td><p>Email Address</p></td> 
    <td class="divider">&nbsp;</td> 
    <td><input type="text" id="profile_email" name="profile_email" /></td> 
    </tr> 
    <tr> 
    <td><p>Personal Statement</p></td> 
    <td class="divider">&nbsp;</td> 
    <td><textarea id="profile_statement" name="profile_statement"></textarea></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td class="divider">&nbsp;</td> 
    <td><input type="button" id="save1" value="Save" name="save1" /></td> 
    </tr> 
</table> 
</div> 
</form> 

PHP文件

$sql = "INSERT INTO `profile_detail` (`ref`,`p_name`,`p_mob`,`landline_number`,`p_email`,`p_add`,`p_statement`) VALUES 
    ('$user_app_id','".$_POST['name']."','".$_POST['mobile_number']."','".$_POST['landline_number']."', 
    '".$_POST['profile_email']."','".$_POST['address']."','".$_POST['profile_statement']."') "; 
if (mysql_query($sql)) { 
echo "<script>window.top.location='index'</script>"; 
    } 
else { 
echo mysql_error(); 
} 
+0

你在期待什么?你在控制台(F12)中遇到任何错误? – adeneo

+0

我期待提交表格时提交!但即使在主机上也没有任何反应 – Abeer

+0

请求中发送的后参数是什么?检查它在铬/萤火虫控制台 –

回答

3

单击type =“button”的输入元素将不会提交表单。您需要将其更改为type =“submit”。

<td><input type="submit" id="save1" value="Save" name="save1" /></td> 
+0

woowwwwwwww,什么是sily错误! 感谢兄弟,你让我的一天! – Abeer

0

尝试使用serializeArray()来发表您的数据

$.ajax({ 
    type: 'POST', 
    data: $('#summary').serializeArray() 
    url: 'test2.php', 
    success: function (data) { 
     $('#hello').html(data); 
    } 
}); 
0

我认为你的问题是你正在从file://协议运行代码。像.php这样的服务器脚本需要http://协议。
为了运行此操作,请查看Apache并尝试运行该服务器。对于Linux,您应该将文件放入/ var/www中,但对于其他操作系统,我不确定。
祝你好运!

+0

我在xamp上运行它。 – Abeer

+0

嗯。对不起......我从那里不知道。 – Atutouato

0

有两个错误在你的代码

首先更改

1.<input type="button" id="save1" value="Save" name="save1" /> 

to 

<input type="submit" id="save1" value="Save" name="save1" /> 

和第二

$('#hello').html(data); 

我不能看到你的窗体中的任何招呼的ID,这样做一个像

<div id="hello"></div> 

谢谢