How to redirect to modal from php file?

I need redirect to my modal from my php file. I'm using header(Location:) but I don't know how redirect please help me ;)

Thanks!

PHP File :

if($cargo == "personal"){  
  $query = "UPDATE empleado SET cargo='bloqueado' WHERE rut='$rut'";

  if ($conex->query($query) == TRUE) {
     header("Location:TO MODAL");
} else {
     console.log('error!');
}

Modal :

echo "<div class='modal fade' id='infodetallada".$fila['rut']."'>";

Upvotes: 0

Views: 3800

Answers (4)

Phil
Phil

Reputation: 595

TL;DR: use header('Location: path/to/file/filename.php')

First things first: Your are mixing up the languages PHP and JS.

if ($conex->query($query) == TRUE) ... is PHP

console.log(..); is JS. You should replace that with something like die('error!'); which will stop the execution of the script and shows an error message.

If you want to redirect to model.php, you could write something like:

if($cargo == "personal"){  
    $query = "UPDATE empleado SET cargo='bloqueado' WHERE rut='$rut'";

    if ($conex->query($query)) {
       header("Location: model.php");
    } else {
       die('error!');
    }
}

Upvotes: 1

As they said, you must to use javascript in order to popup the modal, via some hash, like this:

// on the footer of redirect page if (window.location.hash == "#openm") { $("#myModal").modal("show"); }

Upvotes: 0

Julian Koster
Julian Koster

Reputation: 556

A modal is a Bootstrap JavaScript element which is used on the client side of things. PHP works on the server end of things.

As weird as it may sound, PHP cannot make a modal pop-up (unless you're using some fuzzy, weird-ass code). What you'll need is JavaScript (or jQuery) to help you make that Modal appear as it should.

Since you have jQuery included follow the following tutorial; PHP and AJAX Please let me know if you can follow and make it work?

Upvotes: 0

xpeiro
xpeiro

Reputation: 760

Pass an argument via url, fetch it via js and trigger the modal with js when this arg value is passed by.

Upvotes: 0

Related Questions