我一直在阅读使用jquery提交html表单的几十个主题,我的表单仍然希望传统提交。当我点击按钮提交表单时,php文件成功执行,但我得到一个页面刷新,告诉我我的脚本没有在表单提交时触发。有什么帮助吗?
编辑:更多信息;我的表单在页面上动态加载,并在加载时设置为display:none。
我的表格:
<div id='member-form' style="text-align: center; display: none;">
<form id="addmemberform" method="post" action="/scripts/addmemberscript.php" style='margin: 0 auto;'>
<th><h2 class="form-header">Add Team Member</h2></th>
<tr><span class="error">* required field</span></tr>
<fieldset id="team-info" class="team-info">
<div class='label'>*First Name:</div><div class='label'>*Last Name:</div><div class='label'>Specialty:</div><div class='label'>Email:</div><div class='label'>Phone:</div>
<div>
<input type="hidden" name="tmid" value="<?php echo $team; ?>"></input>
<input type="text" name="fname" autocomplete="off" autofocus tabindex="1">
<input type="text" name="lname" autocomplete="off" tabindex="2">
<input type="text" name="specialty" autocomplete="off" tabindex="3">
<input type="text" name="email" autocomplete="off" tabindex="4">
<input type="text" name="phone" autocomplete="off" tabindex="5">
<div>
</fieldset>
<fieldset class="team-action">
<input class="btn" type="submit" name="submit" value="Submit" style='width: 70; heigt: 15; font-size: 14px;' tabindex="6">
</fieldset>
</form>
</br></br>
</div>
我的JQuery:
<script type="text/javascript">
$(document).ready(function(){
$(document).on('submit','#addmemberform',(function(e){
var postData = $(this).serializeArray();
var formURL = $(this).attr('action');
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
alert(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
}
});
e.preventDefault(); //STOP default action
e.unbind(); //unbind. to stop multiple form submit.
});
});
</script>
我的php文件:
<?php
$team = $_POST["tmid"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$con = mysql_connect('************', '******', '**********');
mysql_select_db(dbbmdmi);
if ($con) {
$sql = "SELECT First, Last, City, State FROM MemberAdmin WHERE First='" .$fname. "' AND Last='" .$lname. "'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) > 0){
while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
echo'People found with that name. Please choose the correct team member: <div>' .$row['First']. ' ' .$row['Last']. ' from ' .$row['City']. ', ' .$row['State'];
}
} else {
$date = date(dmi);
$name = substr($fname, 0, 1) . substr($lname, 0, 1);
$inid = $name . $date;
$memid = substr($team, 0, 3) . $name . date(d);
$con = mysql_connect('dbbmdminet.ipagemysql.com', 'local', 'dbbmdmi');
mysql_select_db(dbbmdmi);
If ($con) {
$sqlins1 = "INSERT INTO MemberAdmin". "(IndID, First, Last)". "VALUES ('$inid','$fname','$lname')";
$sqlins2 = "INSERT INTO Members". "(MemberID, TeamID, IndID)". "VALUES ('$memid','$team','$inid')";
mysql_query($sqlins1);
mysql_query($sqlins2);
echo 'Team member ' .$fname. ' ' .$lname. ' was successfully added.';
}
}}
?>