N
N
Nikolay2015-10-04 01:47:59
PHP
Nikolay, 2015-10-04 01:47:59

Why data is not returned from ajax request or where is the error?

I can't find any error. I've been sitting here scratching my head for three hours. I apologize for the not very beautiful code, I'm waiting for criticism and advice on what can be done better or more beautiful (possibly more correctly).

Request file:

<?php

Error_Reporting(E_ALL & ~E_NOTICE);

$db = mysql_connect("localhost","root", "")or die("errorBaseConnect!");
mysql_select_db("test_program") or die("baseConnectDown!");

?>

<html>

  <head>
    <meta charset="windows1521">
    <title>Admin panal</title>

    <style>

      * {
        padding: 0;
        margin: 0;
      }

      body {
        display: flex;
        margin: auto;
      }

      body > div {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        width: 60%;
        margin: auto;
      }

      body button {
        padding: 5px 20px 5px 20px;
        margin: 10px 5px;
      }

      body span {
        padding: 30px;
        font-size: 20px;
      }

    </style>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

    <script type="text/javascript">

    $(document).ready( function() { 

      var test_id = "";

      function funcSuccess(data) {
        $(test_id).css("display", "block"));
                                $(test_id).text (data);
                        }

      $(".watch_test").bind("click", function() {

        var question_id = (Number(this.id) + 1).toString();
        this.id = question_id;

        var test_name = this.parentNode.getElementsByTagName('h1')[0].firstChild.nodeValue;

        test_id = this.parentNode.getElementsByTagName('section')[0].id;

        var str = "question_id=" + question_id + "&test_name=" + test_name;

        $.ajax ({

                        url: "read-test.php",
                        type: "POST",
                        data: str,
                        success: funcSuccess

                });
      });
    });

    </script>

  </head>

  <body>
    <span><a href="admin.php"><< Back</a></span>
    <div>
      <?php
        $result = mysql_query("SELECT * FROM `tests`");
        $array = mysql_fetch_array($result);
        do {
          echo "<div><h1>".$array['name']."</h1><p>Time: ".$array['time']."</p><p>Mistakes: ".$array['mistakes']."</p><button id='0' class='watch_test'>Watch and change</button><section id='#info".$array['id']."' style='display: none;'></section></div>";
        } while ($array = mysql_fetch_array($result));
      ?>
    </div>
  </body>

</html>


PHP file (requested):

<?php
  Error_Reporting(E_ALL & ~E_NOTICE);

  $db = mysql_connect("localhost","root", "")or die("errorBaseConnect!");
  mysql_select_db("test_program") or die("baseConnectDown!");

  $result = mysql_query("SELECT * FROM ".$_POST['test_name']." WHERE id = \'".$_POST['question_id']."\'");
  $array = mysql_fetch_array($result);

$string1 = "<form class='questions-test' style='display: none;'>";
$string2 = "<textarea type='text' name='question' placeholder='Input question' rows='1'>".$array['question']."</textarea>";
$string3 = "<input type='text' name='answer1' placeholder='Answer 1' value='".$array['answer1']."'>";
$string4 = "<input type='text' name='answer2' placeholder='Answer 2' value='".$array['answer2']."'>";
$string5 = "<input type='text' name='answer3' placeholder='Answer 3' value='".$array['answer3']."'>";
$string6 = "<input type='text' name='answer4' placeholder='Answer 4' value='".$array['answer4']."'>";
$string7 = "<input type='text' name='right_answer' placeholder='Write number of right answer' value='".$array['right_answer']."'>";
$string8 = "<button name='send-question' onclick=' return false; '>Next</button>";
$string9 = "</form>";

echo $string1.$string2.$string3.$string4.$string5.$string6.$string7.$string8.$string9;

?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question