How can I make a Modal Box/Notification Box appear when I do header redirect or some other method of redirection to a page? Basically as a Success kind of thing. Like I click on send it opens a page and then brings them back to the same page where the send button is and will show a Success box? Any ideas?
-
I don't think it's possible with redirection. Without knowing your specific needs and what that redirection would achieve my best bet would be that you could perhaps try to load that page through an iframe on the same page where the 'send' button is. Or load an URL through an AJAX call. – Slavenko Miljic May 22 '15 at 20:45
-
You can use ajax calls or set session variable before the redirection, and check for this variable in the script that you are redirecting to – Ron Dadon May 22 '15 at 20:47
-
Its literally to say success once they click it like i just need it to do some PHP once its finished i need it to say success and reload the page can that be done? maybe put some kind of javascript to like initiate some html to show when the php is finished anyone? – May 22 '15 at 20:48
-
Ron how do i do that never even saw any ajax code before im mainly html to php – May 22 '15 at 20:49
-
Maybe setting a cookie and checking in the JS code if exists, and if exists show the notification – MikeVelazco May 22 '15 at 21:37
2 Answers
If you need to trigger some code on a PHP page through an AJAX call you can do it without directly opening that URL. You just pass that PHP URL to a call, the PHP page code is executed and the response is returned back to the page where the request originated. It would look something like this:
<span id="someButton">Button</span>
<div id="someDivOnYourPage"></div>
<script>
$('#someButton').click(function(e) {
$.ajax('page.php', {
success: function(data) {
alert(data); //This alerts 'Hello from PHP!'
$('#someDivOnYourPage').html(data);//this will set 'Hello from PHP!' inside the #someDivOnYourPage div
},
error: function() {
alert('An error occurred');
}
});
});
</script>
page.php
<?php
echo "Hello from PHP!";
?>
A more detailed explanation with downloadable examples can be found here: jQuery Ajax POST example with PHP.

- 1
- 1

- 3,836
- 2
- 23
- 36
-
1So basically if I load print_r(test); ?> and link that snippet u gave to that php i gave right int his comment will it basically initiate that php and print it on the screen in the same page? also will it reload the page? – May 22 '15 at 21:08
-
Well, not exactly. You won't see the word 'test' written on the screen like you would expect when opening that PHP page in a browser. The value of the `returnedData` variable will be 'test' though. Anything that is printed out on the PHP page is returned back to the success: callback function of the AJAX call. – Slavenko Miljic May 22 '15 at 21:13
-
And no, the page won't reload, the main purpose of AJAX is to handle data without refreshing the whole page. – Slavenko Miljic May 22 '15 at 21:15
-
Ok im tryna like get this ajax to load a file with some php i linked it to the file clicked the button but its not doing anythign and says error occured like whats the returned data about do i need to have some kind of js or php in the linked php file for it to say yay success? – May 22 '15 at 21:18
-
Well, I've edited my answer for better readibility, that should give you an idea how it would work. – Slavenko Miljic May 22 '15 at 21:26
-
Yes i understand it now but may i ask. How do I get it to render HTML to the page with the button? Also how with PHP so I can stop ahving to use 2 different pages to execute some PHP and so html so I can make a nice looking popup box saying success – May 22 '15 at 21:30
-
That is not something that can be answered in one comment. I suggest searching StackOverflow/Google for such answers. – Slavenko Miljic May 22 '15 at 21:36
-
I tried i cant find out how to output the data gotten from the returned data how dude?! – May 22 '15 at 21:40
-
I've edited my answer. There are 4 examples how to do it on the page that I've linked to. – Slavenko Miljic May 22 '15 at 21:42
-
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78581/discussion-between-slavenko-miljic-and-justin-butler-unfedzombie). – Slavenko Miljic May 22 '15 at 21:57
If it's just a login or something similar, you can store the message in a session on whatever page you're processing and then once the page has been redirected, put that session variable into a regular variable and unset your session. Then you'll just print out whatever as HTML or Javascript.
On the page you submit to
//Do your processing
$_SESSION['someReturnMessage'] = 'Success!';
header('Location:./originalpage.php');
On originalpage.php
//Top of file
if(isset($_SESSION['someReturnMessage'])){
$message = $_SESSION['someReturnMessage'];
unset($_SESSION['someReturnMessage'];
}
...further down the page where you want the message to show...
if(isset($message){
echo $message;
}
You could also use AJAX, which may be a better fit for doing simple tasks.
Using sessions can be a bad idea though, so make sure you don't use them when there are better solutions: Error messages stored in SESSION

- 1
- 1

- 170
- 12
-
Ive messed with sessions once before which is for doing a last use kind of thing where it counts time and once they go tho a page it resets the time but i honestly am clueless about ajax and with the $message session return message and the unset bit do i put the message bit on the page with the send button and the unset in the page they get redirected to and then redirected back it should pop up with that message? if so thats perfect i could just make a css modal box and op that $message variable on it. Plz confirm this. – May 22 '15 at 20:53
-
I updated the example, but it sounds like Slavenko Miljic's example might be better suited to what you're trying to do using AJAX. – chrisjacob May 22 '15 at 20:59