jquery/ajax表单提交不起作用


jquery/ajax form submission not working

我从一些教程网站上得到了一个用于表单提交的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();
}

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

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

尝试使用serializeArray()发布数据

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

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

代码中有两个错误

首次更改

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> 

感谢