form ajax inserts data before submit
what's wrong with my code? , here it is
//HTML code
<form id="wsrecruitcvhead" method="post" action="">
<p class="note">All Fields <span class="required">*</span> are required.</p>
<input type="hidden" name="userid" value="<?php if(isset($_SESSION['logged_id'])) echo $_SESSION['logged_id']; ?>" />
<div class="row">
<label>Resume Title </label>
<input type="text" id="resumetitle" name="ResumeTitle" size="60" maxlength="255" value="<?php echo $_POST['ResumeTit开发者_StackOverflowle']; ?>"/>
</div>
<div class="row">
<label>Resume Summary Introduction </label>
<textarea id="resumeintro" name="ResumeSummaryIntroduction" rows="6" cols="50" ><?php echo $_POST['ResumeSummaryIntroduction'];?></textarea>
</div>
<div class="row">
<label>Name </label>
<input id="name" type="text" name="name" size="50" maxlength="50" value="<?php echo $_POST['name']; ?>" />
</div>
<div class="row buttons">
<input id='submitcvbutton' type="submit" name="submit" value="Submit CV" />
</div>
</form>
<div class="success"></div>
//JQUERY AT THE BOTTOM OF THE THE FORM BEFORE THE CLOSING BODY TAG
<script>
$(document).ready(function(){
var resumetitle = $('#resumetitle').attr('value');
var resumeintro = $('#resumeintro').attr('value');
var name = $('#name').attr('value');
$.ajax({
type: "POST",
url: "classes/class.submitcv.php",
data: "resumetitle="+ resumetitle +"&resumeintro="+ resumeintro + "&name=" + name,
success: function(){
$('input#submitcvbutton').click(function(){
$('#wsrecruitcvhead').hide(function(){
$('div.success').fadeIn();
});
});
}
});
return false;
});
//PHP CODE FOR AJAX
<?php
$resumetitle = htmlspecialchars(trim($_POST['resumetitle']));
$resumeintro = htmlspecialchars(trim($_POST['resumeintro']));
$name = htmlspecialchars(trim($_POST['name']));
$link = mysql_connect('localhost','root','test');
if(!$link){
die(mysql_error());
}
$db = mysql_select_db('test',$link);
if(!$db){
die(mysql_error());
}
$add = "INSERT INTO shit(resumetitle,resumeintro,name)VALUES('$resumetitle','$resumeintro','$name')";
mysql_query($add) or die(mysql_error());
?>
because it is inserting a blank row in my table even before I submitted the form
You are making your ajax-request on $(document).ready(). Which means your are doing it when the document has loaded. That is why it puts a blank line in your DB.
$('#wsrecruitcvhead').submit(function(e){
e.preventDefault();
$.ajax({
//etc
});
})
Your problem is that you insert in the database a blank row when the page starts loading
精彩评论