12

i have referred to this two questions call php page under Javascript function and Go to URL after OK button in alert is pressed. i want to redirect to my index.php after an alert box is called. my alert box is in my else statement. below is my code:

processor.php

    if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($var_title) && !empty($var_story) && !empty($var_task) && !empty($var_power) && !empty($var_solve) && !empty($var_result)) {

    (some imagecreatefromjpeg code here)

    else{
        echo '<script type="text/javascript">'; 
        echo 'alert("review your answer")'; 
        echo 'window.location= "index.php"';
        echo '</script>';   
    }

it's not displ ying anything(no alert box and not redirecting). when i delet this part echo 'window.location= "index.php"'; it's showing the alert. but still not redirecting to index.php. hope you can help me with this. please dont mark as duplicate as i have made tose posts as reference. thank you so much for your help.

Community
  • 1
  • 1
Jben Kaye
  • 171
  • 1
  • 5
  • 16

6 Answers6

31

You're missing semi-colons after your javascript lines. Also, window.location should have .href or .replace etc to redirect - See this post for more information.

echo '<script type="text/javascript">'; 
echo 'alert("review your answer");'; 
echo 'window.location.href = "index.php";';
echo '</script>';

For clarity, try leaving PHP tags for this:

?>
<script type="text/javascript">
alert("review your answer");
window.location.href = "index.php";
</script>
<?php

NOTE: semi colons on seperate lines are optional, but encouraged - however as in the comments below, PHP won't break lines in the first example here but will in the second, so semi-colons are required in the first example.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • semicolons are optional. It's a good idea to use them, but it won't break without them ? – adeneo Nov 06 '13 at 23:50
  • True, probably just the `location.*` thing then – scrowler Nov 06 '13 at 23:51
  • @adeneo They are optional if you do a line-break. If you put it all on the same line, then they are necessary. Echo does not automatically line-break, so the code would all be on one line. – Tim Withers Nov 06 '13 at 23:51
  • @TimWithers - Yup, and that's the correct answer, just adding EOL or \n would also work ! – adeneo Nov 06 '13 at 23:52
  • @scrowler thanks for yor answer. i will try them now and let you know what happen. thank you – Jben Kaye Nov 06 '13 at 23:52
  • @adeneo thanks but how can i place this inside echo? without messing up with quotation marks thanks – Jben Kaye Nov 06 '13 at 23:55
  • TimWithers and @adeneo thank you guys for you effort and ideas. really appreciate it:) – Jben Kaye Nov 06 '13 at 23:59
  • Can someone explain why this wouldn't work in a .done() function after an ajax call? it skips over the alert and goes straight to the redirect. thank you – egar Feb 24 '21 at 21:07
  • @egar it would work within an ajax callback. If you have a question you should ask it separately, because it sounds very different to this question. – scrowler Feb 25 '21 at 22:06
4
if (window.confirm('Really go to another page?'))
{
    alert('message');
    window.location = '/some/url';
}
else
{
    die();
}
  • thanks for answering my question. uhm something that worked for me is like scrowler's answer. but your answer is really appreciated too! thanks again – Jben Kaye Nov 07 '13 at 00:38
3

window.location = mypage.href is a direct command for the browser to dump it's contents and start loading up some more. So for better clarification, here's what's happening in your PHP script:

echo '<script type="text/javascript">'; 
echo 'alert("review your answer");'; 
echo 'window.location = "index.php";';
echo '</script>';

1) prepare to accept a modification or addition to the current Javascript cache. 2) show the alert 3) dump everything in browser memory and get ready for some more (albeit an older method of loading a new URL (AND NOTICE that there are no "\n" (new line) indicators between the lines and is therefore causing some havoc in the JS decoder.

Let me suggest that you do this another way..

echo '<script type="text/javascript">\n'; 
echo 'alert("review your answer");\n'; 
echo 'document.location.href = "index.php";\n';
echo '</script>\n';

1) prepare to accept a modification or addition to the current Javascript cache. 2) show the alert 3) dump everything in browser memory and get ready for some more (in a better fashion than before) And WOW - it all works because the JS decoder can see that each command is anow a new line.

Best of luck!

DevlshOne
  • 8,357
  • 1
  • 29
  • 37
  • thank you so much for your answer. you and scrowler gave me the same answer. thank you guys for a bunch of help! – Jben Kaye Nov 07 '13 at 00:16
2

Like that, both of the sentences will be executed even before the page has finished loading.

Here is your error, you are missing a ';' Change:

       echo 'alert("review your answer")'; 
       echo 'window.location= "index.php"';

To:

       echo 'alert("review your answer");';
       echo 'window.location= "index.php";';

Then a suggestion: You really should trigger that logic after some event. So, for instance:

           document.getElementById("myBtn").onclick=function(){
                 alert("review your answer");
                 window.location= "index.php";
           };

Another suggestion, use jQuery

fos.alex
  • 5,317
  • 4
  • 16
  • 18
-1

Working example in php.
First Alert then Redirect works....
Enjoy...

echo "<script>";
echo " alert('Import has successfully Done.');      
        window.location.href='".site_url('home')."';
      </script>";
Arslan Tabassum
  • 900
  • 1
  • 10
  • 25
-1
<head>
<script>
    function myFunction() {
        var x;
        var r = confirm("Do you want to clear data?");
        if (r == true) {
            x = "Your Data is Cleared";
            window.location.href = "firstpage.php";
        }
        else {
            x = "You pressed Cancel!";
        }
        document.getElementById("demo").innerHTML = x;
    }
</script>
</head>
<body>
<button onclick="myFunction()">Retest</button>

<p id="demo"></p>
</body>
</html>

This will redirect to new php page.

tanmayee
  • 15
  • 1