0

There is some way to return value from PHP in to ajax? Or some other way?

simple jQuery call:

$(document).ready(function(){
    var quizCount= 1;
    $("button").click(function(){
        quizCount = quizCount+1;
        $("#quiz").load("load-quiz.php",{
            quizNewCount : quizCount
        });
    });
});

PHP form:

$quizNewCount = $_POST['quizNewCount'];

$sql= "SELECT* FROM quiz WHERE quizid =$quizNewCount";
$result = $conn->query($sql);
mysqli_set_charset($conn,"utf8");
if ($result->num_rows > $quizNewCount)
{
   return $quizNewCount=1;
}

I try that, but without effect. Anyone know how to do that? I don't wont number, but changing value because database will be increased every month.

Yash Parekh
  • 1,513
  • 2
  • 20
  • 31
Piotr Mirosz
  • 846
  • 9
  • 20
  • Use `ajax` instead of `load` – Pedram Apr 23 '18 at 05:55
  • Dnt use load it will unnecessary loads it, instead use ajax it will use only when needed – Bits Please Apr 23 '18 at 05:56
  • Your code is vulnerable to [SQL injection](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [mysqli](https://secure.php.net/manual/en/mysqli.prepare.php) or [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [this post](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). –  Apr 23 '18 at 08:34

2 Answers2

0

Use ajax instead of load. Here is an example code. Use GET instead of POST. Explanation here.

 $.ajax({
    type:  'GET',
    url:   'load-quiz.php',
    data:  {quizNewCount : quizCount},
    success: function (result) {
        console.log(result);
    }
});
Bluetree
  • 1,324
  • 2
  • 8
  • 25
0
    // Ajax calling
     post_data = {quizNewCount : quizCount}
     $.ajax({
                method: 'post',
                dataType: 'json',
                data: post_data,
                url: 'load-quiz.php',
                success: function (data) {
                    result = data;
                    alert(result)
                }
    })

    // action part or processing part
    $quizNewCount = $_POST['quizNewCount'];

            $sql= "SELECT* FROM quiz WHERE quizid =$quizNewCount";
            $result = $conn->query($sql);
            mysqli_set_charset($conn,"utf8");

            if ($result->num_rows > $quizNewCount) {
            $quizNewCount=1;
//as dataType is json so we need to enocde the data in the json format before sending it
            echo json_encode($quizNewCount);
        }
Satish51
  • 119
  • 6