带有单选按钮的表单提交给mysql

时间:2012-11-17 09:24:48

标签: php javascript jquery mysql ajax

我是php和mysql的初学者,在某种情况下我需要你的帮助。 我有一个带单选按钮的表单,实际上是一个星级系统,我想通过ajax调用mysql数据库提交表单。 我不知道如何使用ajax调用和php插入mysql。请帮忙!

我的代码是:

<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>

1 个答案:

答案 0 :(得分:1)

获取所有带序列化函数的ans并传递给ans_file.php。

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

我有一些问题: 我做了这个PHP,并没有插入任何东西,如果我失踪,请你告诉我 什么? 谢谢!

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "stars";
$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 table (answers) VALUES ('$question1','$question2','$question3','$question4','$question5','$question6')");
echo "<h1>Thank You !</h1>";
?>