0

有人可以帮助我通过 ajax 将无线电表单结果插入 mysql 吗?我尝试了一些东西,但它根本不起作用。

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head> 
 <script src='jquery.js' type="text/javascript"></script>
 <script src='documentation/documentation.js' type="text/javascript"></script>
 <link href='documentation/documentation.css' type="text/css" rel="stylesheet"/>
 <script type="text/javaScript" src="documentation/chili/jquery.chili-2.0.js"></script> 
 <script type="text/javascript">try{ChiliBook.recipeFolder="documentation/chili/"}catch(e){}</script>
 <script src='jquery.MetaData.js' type="text/javascript" language="javascript"></script>
 <script src='jquery.rating.js' type="text/javascript" language="javascript"></script>
 <link href='jquery.rating.css' type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="tab-Testing">
<script>
$('#form1').submit(function() {
 var ans=$(this).serialize();
  $.ajax({
  type: "POST",
   url: "starsubmit.php",
  data:{data:ans}
 }).done(function( msg ) {
 alert( "Data Saved: " + msg );
 });
 return false;
});
</script>

<div class="Clear">&nbsp;</div>
<form id="form1" ">

        Question 1:bla bla bla

   <input class="star required" type="radio" name="Question 1" value="1"/>
    <input class="star" type="radio" name="Question 1" value="2"/>
    <input class="star" type="radio" name="Question 1" value="3"/>
    <input class="star" type="radio" name="Question 1" value="4"/>
    <input class="star" type="radio" name="Question 1" value="5"/>
   </div>
   <br/>
   <div class="Clear">

        Question 2:bla bla bla

    <input class="star required" type="radio" name="Question 2" value="1"/>
    <input class="star" type="radio" name="Question 2" value="2"/>
    <input class="star" type="radio" name="Question 2" value="3"/>
    <input class="star" type="radio" name="Question 2" value="4"/>
    <input class="star" type="radio" name="Question 2" value="5"/>
   </div>
   <br/>
   <div class="Clear">

        Question 3:bla bla bla

    <input class="star required" type="radio" name="Question 3" value="1"/>
    <input class="star" type="radio" name="Question 3" value="2"/>
    <input class="star" type="radio" name="Question 3" value="3"/>
    <input class="star" type="radio" name="Question 3" value="4"/>
    <input class="star" type="radio" name="Question 3" value="5"/>
   </div>
 <br>
   <div class="Clear">

        Question 4:bla bla bla

    <input class="star required" type="radio" name="Question 4" value="1" />
    <input class="star" type="radio" name="Question 4" value="2" />
    <input class="star" type="radio" name="Question 4" value="3" />
    <input class="star" type="radio" name="Question 4" value="4" />
    <input class="star" type="radio" name="Question 4" value="5" />
   </div>
   <br/>
   <div class="Clear">

        Question 5:bla bla bla

    <input class="star required" type="radio" name="Question 5" value="1"/>
    <input class="star" type="radio" name="Question 5" value="2"/>
    <input class="star" type="radio" name="Question 5" value="3"/>
    <input class="star" type="radio" name="Question 5" value="4"/>
    <input class="star" type="radio" name="Question 5" value="5"/>
   </div>
   <br/>
   <div class="Clear">

        Question 6:bla bla bla

    <input class="star required" type="radio" name="Question 6" value="1" />
    <input class="star" type="radio" name="Question 6" value="2" />
    <input class="star" type="radio" name="Question 6" value="3" />
    <input class="star" type="radio" name="Question 6" value="4" />
    <input class="star" type="radio" name="Question 6" value="5" />
   </div>

   <input type="submit" value="Submit scores!" />  </td>

</form>
</body>
</html>

这就是我尝试的方式:

$('#form').submit(function() {
 var ans=$(this).serialize();
  $.ajax({
  type: "POST",
   url: "starsubmit.php",
  data:ans
 }).done(function( msg ) {
 alert( "Data Saved: " + msg );
 });
 return false;
});

和PHP:

<?php
include("db.php");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$question1 = mysql_real_escape_string($_POST['Question1']);
$question2 = mysql_real_escape_string($_POST['Question2']);
$question3 = mysql_real_escape_string($_POST['Question3']);
$question4 = mysql_real_escape_string($_POST['Question4']);
$question5 = mysql_real_escape_string($_POST['Question5']);
$question6 = mysql_real_escape_string($_POST['Question6']);

mysql_query("INSERT INTO answers (q1,q2,q3,q4,q5,q6) VALUES ('$question1','$question2','$question3','$question4','$question5','$question6')");
echo "<h1>Thank You !</h1>";
}

?>

结果在浏览器的地址栏中可见,并且在提交时它们没有插入到mysql中,所以我认为问题出在ajax ..我不知道。

如果你有,请给我一个如何将带有单选按钮的表单插入 mysql 的示例。提前致谢!

4

1 回答 1

0

你需要记住:radiolist 只返回一个值。选中时也与复选框返回值相同。并且您必须在检查 $_POST、$_GET、$_REQUEST 之前设置此变量为 false

$q = array();
for ($i = 1; $i <= 6; $i++) {
  $q[$i] = isset($_POST['Question '+$i]) ? mysql_real_escape_string($_POST['Question'+$i]) : 0;
}
mysql_query("INSERT INTO answers (q1,q2,q3,q4,q5,q6) VALUES ('$q[1]','$q[2]','$q[3]','$q[4]','$q[5]','$q[6]')");
于 2012-11-17T17:43:48.683 回答