-3

I want to display the javascript alert and the redirect using php. The logical error is that it won't display the javascript alert.

here's my code:

if($sql_update==true){
    echo "<script type='text/javascript'>alert('Updated');</script>";   
    header("location:?tag=student_entry&opr=upd&rs_id=".$_POST['stud_id_txt']."");
}   

3 Answers3

0

if your doing embedding js inside php for alert then also use JS redirect inside it

$redirectURL="Your URL";
    print("<script>");
    print("alert('Updated');");
    print("var t =setTimeout(\"window.location='".$redirectURL."';\", 3000);");
   print("</script>");
CY5
  • 1,551
  • 17
  • 23
0

You can make an javascript function to help you alert the user and redirect like this:

function alertAndRedirect(studId) {
     alert('Updated');
     window.location = "?tag=student_entry&opr=upd&rs_id="+studId;
}

and in php you call it like this:

if($sql_update==true){
    echo "<script type='text/javascript'>alertAndRedirect(".$_POST['stud_id_txt'].");</script>";
}  
Alex7
  • 560
  • 3
  • 19
  • What if the id starts with a letter because I'm going reuse that code for the subject_code which starts with a letter. I tried to use in the subject_code but it doesn't work when the id starts with a letter. – Jude Howell Sarabia Sep 23 '15 at 03:23
  • should become window.location = "?tag=student_entry&opr=upd&rs_id=' "+studId+" ' "; pay attention to the commas – Alex7 Sep 23 '15 at 07:19
0

You can not provide output before header ,even if you set output buffer on , you will not be able to alert . So do redirection through js.

if($sql_update==true){
    echo "<script type='text/javascript'>alert('Updated');location.href=?tag=student_entry&opr=upd&rs_id=".$_POST['stud_id_txt']."script>";   

}  
Rohit Kumar
  • 1,948
  • 1
  • 11
  • 16