将无线电表格插入mysql

时间:2012-11-17 17:21:40

标签: php javascript jquery html mysql

有人可以通过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的示例。 提前谢谢!

1 个答案:

答案 0 :(得分: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]')");