1

我正在尝试制作星级评分系统,但无法将结果插入 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: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>

现在,我的starsubmit.php:

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "cosmote";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
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>";
}

?>

最后,我的桌子:

CREATE TABLE IF NOT EXISTS `answers` (
  `q1` int(3) NOT NULL,
  `q2` int(3) NOT NULL,
  `q3` int(3) NOT NULL,
  `q4` int(3) NOT NULL,
  `q5` int(3) NOT NULL,
  `q6` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
4

2 回答 2

1

我必须说这应该是:

$('form1').submit(function() { // <------you are not submiting the specific form 

像这样:

$('#form1').submit(function() { // <------ every id should have a '#' in jquery

如果您可以更改 .done 功能,这可能是问题

.done(function() {  
   alert( "Data Saved");  // try removing the passed param "msg"
});
于 2012-11-17T11:22:13.370 回答
0

在 jQuery 中,ID 以“#”开头

$('form1').submit(function() {

于 2012-11-17T11:23:41.567 回答