Reputation: 291
helllo i m making a quiz. i m fetching questions from database . while im using ajax to display the questions by refreshing a div tag. here is the code of ajax used.
$(document).ready(function(){
$("#button").click(function(){
var q_id=$("#h_val").val();
$("#rv").val(q_id);
$.ajax({ url: 'data.php',
type: "POST",
data: {"q_id":q_id} ,
success: function(result) {
$('#que').html(result);
var newValue = parseInt(q_id) + 1
$('#h_val').val(newValue);
}
});
});
});
now this is the data.php page code.
<?php
$qid=$_POST['q_id'];
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="select * from question where qno=$qid";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
if (mysql_num_rows($rq) == 0)
{
echo "database is empty.";
}
else
{
while ($sub_row=mysql_fetch_array($rq))
{
$id=$sub_row["qno"];
$question=$sub_row["question"];
$option1=$sub_row["option1"];
$option2=$sub_row["option2"];
$option3=$sub_row["option3"];
$option4=$sub_row["option4"];
echo "<h5>Q".$id." : ".$question."</br></h5>";
echo"</br><br>
<h4><input type= radio id='1' name=\"{$id}\" value=\"{$option1}\">$option1</h4>
</br>
<h4><input type= radio id='2' name=\"{$id}\" value=\"{$option2}\">$option2</h4>
</br>
<h4><input type= radio id='3' name=\"{$id}\" value=\"{$option3}\">$option3</h4>
</br>
<h4><input type= radio id='4' name=\"{$id}\" value=\"{$option4}\">$option4</h4>
</br></br>";
}
}
}
?>
i just want to go to a page when the question get over. while using header loaction whole page is coming in div . i just want a code to replace the line "databse is empty" which will redirect me to anther page .
Upvotes: 0
Views: 50
Reputation: 1761
Should be done in js. As per your current code, try this
success: function(result) {
if(result=="database is empty.") {
window.location="newurl.php";
}
$('#que').html(result);
var newValue = parseInt(q_id) + 1
$('#h_val').val(newValue);
}
Upvotes: 1