2011-07-26 44 views
1

我的代碼有什麼問題? ,這裏是形成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()); 

?> 

,因爲它是插入一個空白行中我的表我提交表單

+0

爲什麼我看不到任何提交事件? – MatTheCat

回答

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

}) 

你的問題是,你在數據庫中插入空行甚至在當頁開始加載

2

您正在$(document).ready()上發出您的ajax請求。這意味着您在文檔加載時正在做它。這就是爲什麼它會在你的數據庫中留出一個空行。