Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to a page/URL after alert button is pressed

Tags:

javascript

php

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.

like image 657
Jben Kaye Avatar asked Nov 06 '13 23:11

Jben Kaye


People also ask

How do I redirect a page after click on OK button on sweet alert?

For example: swal({ title: "Wow!", text: "Message!", type: "success" }). then(function() { window. location = "redirectURL"; });

Can I put a link in a JavaScript alert?

Displaying hyperlinks in an alert box in JavaScript is not possible. To show links, use a custom alert box. With a dialog alert widget, you can achieve the following: For more on this, refer to the source code for Dialog Alert Widget.

How do I add an alert to a URL?

Let say you have a page, called "myAlert. php". So what you can do is, you can write a script on this page, which will simply show the alert when somebody with this URL will access this. Or you can also, write a simple condition that when a request has been made to this URL, you can show the alert box.


2 Answers

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.

like image 97
scrowler Avatar answered Oct 18 '22 22:10

scrowler


if (window.confirm('Really go to another page?'))
{
    alert('message');
    window.location = '/some/url';
}
else
{
    die();
}
like image 4
user2962745 Avatar answered Oct 18 '22 23:10

user2962745