0

I want to increment $x in this function to get next record in table. But it is incrementing only once . Any Help is appreciated

This is the function I call,

<script type="text/javascript">
    function getnextques(){
        document.getElementById("quescontent").innerHTML= '<?php
        $x++;
        $res = mysql_query("SELECT * FROM question_content WHERE id=".$x) or die(mysql_error());  
        while($row=mysql_fetch_array($res)){
            echo $row['ques'] ;
        }; 
        ?>'
}
</script>

from ,

<a href="javascript:getnextques()" class="startbutton" id="mediumone"> Next </a>
1

3 Answers 3

4

you cannot mix like that Javascript with PHP. PHP is server side based, javascript is browser based.

You should use Ajax for that (jQuery)

eg:

<script type="text/javascript">
var x = 0;
function getnextques(){
    var newX = x++;
    $.ajax({
        type: "POST",
        url: 'script.php',
        data: { x: newX },
        success: function(data) {
            document.getElementById("quescontent").innerHTML = data;
        }
    });
}
</script>

and script.php

<?php
    // mysql connection
    $x = (int) $_POST['x'];
    $res = mysql_query("SELECT * FROM question_content WHERE id=".$x) or die(mysql_error());  
    while($row=mysql_fetch_array($res)){
        echo $row['ques'] ;
    }
 ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Your php code runs on the server and will be statically returned to the browser, where the javascript runs. You need to use Ajax in order to dynamically fetch content.

Comments

-1

If you are not familiar with Ajax, or could not use it for any reason, take the user to next page to show the next question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.