Reputation: 1127
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>
Upvotes: 0
Views: 561
Reputation: 39704
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'] ;
}
?>
Upvotes: 4
Reputation: 162
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.
Upvotes: -1