简体   繁体   中英

want to get to another page while i m under an ajax call

helllo im making a quiz. im 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 .

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); 

            } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM