problem to add result in database with jquery

problem to add result in database with jquery

hello, i have a problem to add my result in my database, look at my code :

index2.php
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="iso-8859-1">
  5.     <title>Calcul mental</title>
  6.     <link rel="stylesheet" href="css/normalize.css">
  7.     <link rel="stylesheet" href="css/foundation.min.css">
  8.     <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  9. </head>
  10. <body>

  11. <div class="row">
  12. <h1>Calcul Mental</h1>
  13. <div class="columns large-12">
  14. <h2>Quel est le résultat de ? </h2>
  15. <div class="columns large-6">
  16. <form class="mental_form2" method="post" id="mental_form2" action="calcul.php">
  17. <div class="columns large-4">
  18. <input class="champ" type="text" name="n1" id="n1">
  19. </div>
  20. <div class="columns large-4">
  21. <input class="champ" type="text" name="si1" id="si1">
  22. </div>
  23. <div class="columns large-4">
  24. <input class="champ" type="text" name="n2" id="n2">
  25. </div>
  26. <div class="columns large-4">
  27. <label for="reponse">Votre réponse</label>
  28. </div>
  29. <div class="columns large-4">
  30. <input type="text" name="result" id="result">
  31. </div>
  32. <div class="columns large-4">
  33. <input type="submit"  id="validation" value="Valider" class="button small right">
  34. </div>
  35. </form>
  36. </div>
  37. <div class="columns large-6 res">
  38. </div>
  39. </div>
  40. </div>
  41. <span id="chronotime">0:00:00:00</span>
  42. <script src="js/api.js"></script>
  43. </body>
  44. </html>


api.js


  1. startTime = 0
  2. var start = 0
  3. var end = 0
  4. var diff = 0
  5. var timerID = 0
  6. function chrono(){
  7. end = new Date()
  8. diff = end - start
  9. diff = new Date(diff)
  10. var msec = diff.getMilliseconds()
  11. var sec = diff.getSeconds()
  12. var min = diff.getMinutes()
  13. var hr = diff.getHours()-1
  14. if (min < 10){
  15. min = "0" + min
  16. }
  17. if (sec < 10){
  18. sec = "0" + sec
  19. }
  20. if(msec < 10){
  21. msec = "00" +msec
  22. }
  23. else if(msec < 100){
  24. msec = "0" +msec
  25. }
  26. document.getElementById("chronotime").innerHTML = hr + ":" + min + ":" + sec + ":" + msec
  27. timerID = setTimeout("chrono()", 10)
  28. }

  29. function chronoStop(){
  30. clearTimeout(timerID)
  31. }
  32. function chronoContinue(){
  33. start = new Date()-diff
  34. start = new Date(start)
  35. chrono()
  36. }

  37. function chronoStart(){
  38. start = new Date()
  39. chrono()
  40. }

  41. function init(){
  42. var n1 = 1 + Math.floor(Math.random() * 10);
  43. var si1 = '+';
  44. var n2 = 1 + Math.floor(Math.random() * 10);
  45. $('#n1').val(n1);
  46. $('#si1').val(si1);
  47. $('#n2').val(n2);
  48. }
  49. function success(){
  50. $('.res').html('<div data-alert class="success alert-box">Bien joué ! <a href="#" class="close">&times;</a></div>');
  51. }
  52. function error(){
  53. $('.res').html('<div data-alert class="alert alert-box">Dommage ! même joueur joue encore ! <a href="#" class="close">&times;</a></div>');
  54. }


  55. function send(){
  56. $('#mental_form2').submit(function() {
  57. alert('jusque la ok');
  58. $.post('calcul.php', { 
  59. n1: $('#mental_form2 input[name=n1]').val(),
  60. si1: $('#mental_form2 input[name=si1]').val(),
  61. n2: $('#mental_form2 input[name=n2]').val(),
  62. result: $('#mental_form2 input[name=result]').val()
  63. }
  64. );
  65. return false; 
  66. });
  67. }
  68. function check(){
  69. var n1 = $('#n1').val();
  70. var si1 = $('#si1').val();
  71. var n2 = $('#n2').val();
  72. var r1 = parseInt(n1) + parseInt(n2);
  73. var r2 = $('#result').val();
  74. if( r1 == r2){
  75. send();
  76. success();
  77. }else{
  78. error();
  79. }
  80. $('#result').val('');
  81. }
  82. $(document).ready(function(){
  83. init();
  84. chronoStart();
  85. })
  86. $('#validation').click(function(){
  87. check();
  88. init();
  89. })


calcul.php

  1. <?php
  2. session_start();
  3. // set error reporting level
  4. if (version_compare(phpversion(), '5.3.0', '>=') == 1)
  5.   error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
  6. else
  7.   error_reporting(E_ALL & ~E_NOTICE);
  8.   
  9. require_once('classes/CMySQL.php');
  10. require_once('classes/Ccalcul.php');


  11. if ($_SESSION['member_id'] && $_SESSION['member_status'] == 'active' && $_SESSION['member_role'])

  12. {
  13. $sChi1 = $GLOBALS['MySQL']->escape($_POST['n1']);
  14. $sSi1 = $GLOBALS['MySQL']->escape($_POST['si1']);
  15. $sChi2 = $GLOBALS['MySQL']->escape($_POST['n2']);
  16. $sVotresultat_1 = $GLOBALS['MySQL']->escape($_POST['result']);
  17. if ($sChi1 && $sSi1 && $sChi2 && $sVotresultat_1) 
  18. {
  19. $Accept2 = $GLOBALS['Ccalcul']->acceptcalcul_1();
  20. }
  21. }
  22. echo (file_get_contents('templates/index2.php'));

  23. ?>

Calcul.php

  1. <?php
  2.   
  3. class Ccalcul {
  4.   
  5.     function Ccalcul() {}
  6.       
  7.     // Accept votresultat
  8.       
  9.     function acceptcalcul_1() 
  10.         {
  11.               
  12.             $sChi1 = $GLOBALS['MySQL']->escape($_POST['n1']);
  13.             $sSi1 = $GLOBALS['MySQL']->escape($_POST['si1']);
  14.             $sChi2 = $GLOBALS['MySQL']->escape($_POST['n2']);
  15.             $sVotresultat_1 = $GLOBALS['MySQL']->escape($_POST['result']);
  16.   
  17.         if ($sChi1 && $sSi1 && $sChi2 && $sVotresultat_1) 
  18.       
  19.             {
  20.                 $bRf = $GLOBALS['MySQL']->res("INSERT INTO `addition` SET 
  21.                     `chiffre1` = '{$sChi1}',
  22.                     `signe1` = '{$sSi1}',
  23.                     `chiffre2` = '{$sChi2}',
  24.                     `votresultat` = '{$sVotresultat_1}'
  25. ");
  26.                       
  27.             }
  28.         }
  29.           
  30. }
  31. $GLOBALS['Ccalcul'] = new Ccalcul();

CMysql.php

  1. <?php

  2. class CMySQL {

  3.     // variables
  4.     var $sDbName;
  5.     var $sDbUser;
  6.     var $sDbPass;

  7.     var $vLink;

  8.     // constructor
  9.     function CMySQL() {
  10.         $this->sDbName = 'site3';
  11.         $this->sDbUser = 'root';
  12.         $this->sDbPass = '';

  13.         // create db link
  14.         $this->vLink = mysql_connect("localhost", $this->sDbUser, $this->sDbPass);

  15.         //select the database
  16.         mysql_select_db($this->sDbName, $this->vLink);

  17.         mysql_query("SET names ISO-8859-1");
  18.     }

  19.     // return one value result
  20.     function getOne($query, $index = 0) {
  21.         if (! $query)
  22.             return false;
  23.         $res = mysql_query($query);
  24.         $arr_res = array();
  25.         if ($res && mysql_num_rows($res))
  26.             $arr_res = mysql_fetch_array($res);
  27.         if (count($arr_res))
  28.             return $arr_res[$index];
  29.         else
  30.             return false;
  31.     }

  32.     // executing sql
  33.     function res($query, $error_checking = true) {
  34.         if(!$query)
  35.             return false;
  36.         $res = mysql_query($query, $this->vLink);
  37.         if (!$res)
  38.             $this->error('Database query error', false, $query);
  39.         return $res;
  40.     }

  41.     // return table of records as result in pairs
  42.     function getPairs($query, $sFieldKey, $sFieldValue, $arr_type = MYSQL_ASSOC) {
  43.         if (! $query)
  44.             return array();

  45.         $res = $this->res($query);
  46.         $arr_res = array();
  47.         if ($res) {
  48.             while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
  49.                 $arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
  50.             }
  51.             mysql_free_result($res);
  52.         }
  53.         return $arr_res;
  54.     }

  55.     // return table of records as result
  56.     function getAll($query, $arr_type = MYSQL_ASSOC) {
  57.         if (! $query)
  58.             return array();

  59.         if ($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  60.             $arr_type = MYSQL_ASSOC;

  61.         $res = $this->res($query);
  62.         $arr_res = array();
  63.         if ($res) {
  64.             while ($row = mysql_fetch_array($res, $arr_type))
  65.                 $arr_res[] = $row;
  66.             mysql_free_result($res);
  67.         }
  68.         return $arr_res;
  69.     }

  70.     // return one row result
  71.     function getRow($query, $arr_type = MYSQL_ASSOC) {
  72.         if(!$query)
  73.             return array();
  74.         if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  75.             $arr_type = MYSQL_ASSOC;
  76.         $res = $this->res ($query);
  77.         $arr_res = array();
  78.         if($res && mysql_num_rows($res)) {
  79.             $arr_res = mysql_fetch_array($res, $arr_type);
  80.             mysql_free_result($res);
  81.         }
  82.         return $arr_res;
  83.     }

  84.     // escape
  85.     function escape($s) {
  86.         return mysql_real_escape_string(strip_tags($s));
  87.     }

  88.     // get last id
  89.     function lastId() {
  90.         return mysql_insert_id($this->vLink);
  91.     }

  92.     // display errors
  93.     function error($text, $isForceErrorChecking = false, $sSqlQuery = '') {
  94.         echo $text; exit;
  95.     }
  96. }

  97. $GLOBALS['MySQL'] = new CMySQL();
Thanks to answer