0

Could somebody help me with inserting a radio form results into mysql through ajax? I've tried something but it's not working at all.

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

This is how I tried:

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

And the 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>";
}

?>

The results are visible in browser's address bar and they are not inserted in mysql when submit so I think the problem is with ajax..I don't know exactly.

Please if you have, give me an example of how to insert a form with radio buttons into mysql. Thanks in advance!

user1820705
  • 591
  • 2
  • 7
  • 17

1 Answers1

0

You need remember: radiolist return only one value. Also radio as same checkbox return value when checked. And you have to set before check $_POST, $_GET, $_REQUEST set this vars to 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]')");
Sergey
  • 5,208
  • 25
  • 36
  • Thanks a lot! Now it's inserting but only zeros, i think there is also a problem with ajax or with my form. Could you please look a little – user1820705 Nov 17 '12 at 18:05
  • @user1820705 it will post as `0` because your `input` name has a space `name="Question 1"` and the `$_POST['Question'+$i]` does not so they will not match. Since `` names should not have spaces, as they are not recognized in php post/get/request arrays, either remove the space `name="Question1"` or add an underscore to both `name="Question_1"` `$_POST['Question_'+$i]`. See [http://stackoverflow.com/questions/11221879/can-input-elements-have-capital-letters-and-spaces-in-php](http://stackoverflow.com/questions/11221879/can-input-elements-have-capital-letters-and-spaces-in-php) – Sean Nov 17 '12 at 19:22
  • @user1820705, I don't see html part, but now Sean is right. I'd updated my answer - add space into 'Question '+$i – Sergey Nov 17 '12 at 20:48