表单ajax在提交之前插入数据

时间:2011-07-26 07:27:56

标签: php jquery ajax

我的代码出了什么问题? ,这是

//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['ResumeTitle']; ?>"/>
                   </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());

?>

因为即使在我提交表单

之前它在我的表中插入一个空行

2 个答案:

答案 0 :(得分:2)

您正在$(document).ready()上发出ajax请求。这意味着您在加载文档时正在执行此操作。这就是为什么它在你的数据库中添加一个空行。

答案 1 :(得分:1)

$('#wsrecruitcvhead').submit(function(e){
 e.preventDefault();
$.ajax({
     //etc
   });

})

您的问题是在页面开始加载时在数据库中插入一个空行