2011-10-02 115 views
0

我有一个表单提交给一个php脚本与jQuery和ajax,但在php脚本中变成空白。表单未被序列化? jQuery和ajax

形式:

<table id="coachapplication"> 

<form id="coachapplicationform" action="" method="post"> 

<tr><td>Briefly explain your teaching methods:<br /> <textarea maxlength="100" name="coachmethods" id="coachmethods"></textarea></td></tr> 

<tr><td><input type="checkbox" value="yes" name="agree" id="agree" /> I am fully prepared to take on the responsibilities of being a coach which include logging into the website daily and hosting sessions. 

<tr><td><button id="submitcoachapplication" type="button">Submit</button></td></tr> 

</form> 

</table> 

jQuery脚本:

$('#submitcoachapplication').click(function() { 

$.ajax({ 

type: "POST", 

url: "includes/sendcoachapplication.php", 

data: $("form#coachapplicationform").serialize(), 

success: function(msg){ 

    $("#coachapplication").html(msg); 

} 

}); 

}); 

php脚本:

<?php 

session_start(); 

$user = $_SESSION['username']; 

include("dbcon.php"); 

include("user.php"); 

$result = mysql_query("SELECT * FROM coachapplications ORDER BY username"); 

while($row = mysql_fetch_array($result)) { 

$username = $row['username']; 

if($username == $user) die("You already have a pending application."); 

} 

$coachmethods = $_POST['coachmethods']; 

$coachmethods = mysql_real_escape_string($coachmethods); 

$agree = $_POST['agree']; 

if($coachmethods == "") die("Please enter some info about how you intend to teach."); 

if($agree != "yes") die(" Please agree to the terms."); 

$sql="INSERT INTO coachapplications (username, methods) VALUES ('$user', '$coachmethods')"; 

if (!mysql_query($sql,$con)) die('Error: ' . mysql_error()); 

echo 'Your application has been submitted, and is awaiting admin approval. If you are found suitable for the program, you will be taught how to use the system.'; 

mysql_close($con); 

?> 

回答

2

一个<form>元素是不允许作为<table>一个孩子。您的浏览器可能会通过将其移动到表格之外进行纠错,这意味着其中不再存在元素。

<table>置于<form>之内,而不是相反。

更好的是,don't use tableslayout

+0

哈哈有趣,我会记得从现在起谢谢。 – vacarsu