nirks
nirks

Reputation: 340

How to Redirect from PHP page by call of ajax request

I want to redirect to welcome.php by sending ajax request to n.php. But It returns me all the code of welcome.php how to fix this problem using php only.

<input type = "text" class="myname">
<input type = "submit" class="ok">

 $(".ok").on("click", function(e){
  var do = $('.myname').val();
    $.ajax({url: "n.php",
    cache: true,
    type: "POST",
    data: {moo:do},
    success: function(d){
      alert(d);
    }
   });
  });

In n.php

<?php
 if(isset($_POST['moo']) ){
     if ($_POST['moo'] == "Nirikshan"){
     header('location: ../welcome.php');
     }else{
     echo"You Are Not Nirikshan";
     }
 }
?>

when I try to redirect to next page then alert box shows all the html codes of page welcome.php how can i redirect to welcome.php by php only

Upvotes: 1

Views: 261

Answers (2)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26288

Try this:

<?php
   if(isset($moo))
   {
       echo true;
   }
   else
   {
       echo false;
   }
?>

Ajax:

success: function(response){
    if(response === true)
    {
        window.location('URL');
    }
}

Upvotes: 1

Vinod VT
Vinod VT

Reputation: 7159

Just put redirect on your jquery success,

like this,

success: function(d){
  if(d === true){
    window.location.replace("welcome.php");
  }
}

Replace your alert(d) with this code.

PHP page will be like,

<?php
if(isset(moo)){
       return true;
   }
   else{
       return false;
   }
?>

Upvotes: 2

Related Questions