I have a one page site that has PHP code in it. Once the user presses 'Send', this sends the information to my email, then displays a messagebox saying that the action was a success to the user - great.
After the messagebox is closed, the website stays at website.com/report.php. Is there a way to redirect it back to the original page.
Also, any way to change the icon in the messagebox that pops up? Here is the code that I have:
<script language="JavaScript">alert("Your request has been sent. I will contact you soon!");</script>
Thanks.
Look into window.open and window.location
Place it after your alert()
http://www.tizag.com/javascriptT/javascriptredirect.php
Also, to answer your messagebox icon question: No, it is browser-dependent and not modifiable.
If you want to do that, your are going to need to fake it with html/css and javascript.
<script language="JavaScript">
alert("Your request has been sent. I will contact you soon!");
window.location.assign("http://website.com");
</script>
If you want to change the icon in alert box or make it look a little fancy, you could try YUI dialog
Use this code to display the alert:
function displayAlert(message, redirect) {
alert(message);
window.location.href = redirect;
}
Then, you can use code like:
displayAlert("This is the message", "http://redirect.the/user/here");
Use the php header command
<?php
header("Location: http://www.example.com/");
exit;
?>
To do a redirect in PHP, use header("Location: page.php"); for this. Before and after this your code shouldn't be sending any other output to the response. Eventually use exit(); to terminate the script afterwards.
If you need the page which was requested right before this page, then best what you can do is to include its URL as request parameter of the link to report.php and use it as redirect destination. E.g.
report
and in the report.php pass it as hidden input field:
<input type="hidden" name="referrer" value="<?php echo getParam("referrer"); ?>">
And after submitting the report do:
header("Location: " . getParam("referrer") . ")"; // getParam() returns sanitized GET parameter.
An alternative is to use the $_SERVER['HTTP_REFERER'] header (yes, including the typo) for this, but this is just not that reliable as it may be disabled or spoofed by the client.
I believe a more elegant solution is to simply present the user with a confirmation page (instead of an alert box), and place a link to the previous page there.
That would at lease work for all users.
For those users with javascript a little Ajax (jQuery) could submit the form for you, and display the confirmation nessage. All without leaving the page the user is on (negating the need for any fancy redirects).
Towards the end of your php, use this:
header('location: home.php');
This will cause the browser to load the original page.
I don't believe that the standard alert box can be altered, aside from the message. You can't change the title or the buttons, either.
Frank
Related
I'm setting up a simple landing page on DreamHost. It won't let me put php code in the file index.html. So, when user submits an email address, I use $_POST to submit the email address to another page mail_auto.php.
After seeing a quick "email sent" message, I'd like the user to be directed back to the index.html page from mail_auto.php.
header() looks a bit complex and seems to interfere with the execution of the balance of mail_auto.php.
What's the best way to redirect the user?
To redirect user back to index.html, use the following:
header('Location: index.html');
exit;
Alternatively, if you want to display something like " Redirecting... " on screen, you can use the meta-refresh method , or JavaScript window.location method with setTimeout
The meta refresh method:
Add this to HTML <head>:
<meta http-equiv="refresh" content="2;url=index.html">
where 2 is number of seconds before the refresh is executed.
Using the header is typically what I'd do.
Have you thought about using JavaScript? It's not the best way, although it would work.
<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//-->
</script>
Just echo this javascript code end of the process.
<script>
window.location.href = 'http://www.yourwebsite.com';
</script>
I wanted to popup an alert box. After that, the site would redirect to main page. But, it seems that it directly redirect to the mainpage without alerting anything.
if($registerquery)
{
?>
<script>alert('received!')</script>
<?php
}
header("Location: mainpage.php");
exit();
?>
I wanted to do this to ensure users that the process of submission ended successfully. How can i alert something before the page redirect to mainpage and more importantly what causes this? I think the page should not have redirected before the alert box.(Before these codes, site registers what users submitted but not relevant i guess.)Thanks
You just can't do this. PHP is server-side, JS is client-side. Using a location header is server-side, so the browser never gets the JS.
Instead, try something more like:
if( $registerquery)
echo "<script>alert('received!'); location.href='mainpage.php';</script>";
and remove the header bit altogether.
I would like people to be able to click submit on a form, load a page where it checks some data then redirect to a whole new page. I know that you can use header() to redirect but you cant load anything with the header and I would like a loading animation between pages. What is the best way to accomplish this.
I would make the form submit its data to another page (something like action="process.php").
That page processes whatever data your form sent, shows a nice "loading" text and animation and redirects the user to another page after a while.
This could be done using HTML's meta refreshes, which goes in your <head> and looks like this:
<meta http-equiv="refresh" content="3;url=anotherpage.php">
The content attribute is the interesting bit in there. The 3 means 3 seconds, anotherpage.php is the site redirected to after 3 seconds have passed.
As alternative I'd suggest a minimal Javascript effect on the submitting page. onsubmit you disable the submit button and display a spinner or similar animation near it, maybe also adding a "Processing..." message. The page can then go through its normal POST/redirect/GET cycle, it's faster than going through an intermediate page and the user still has the feeling that something's happening.
If headers have already been sent then this is the PHP/JavaScript code I use to send the browser to $newLocation:
echo '<script language="javascript" type="text/javascript" >', PHP_EOL;
echo '<!--', PHP_EOL;
echo ' window.top.location.href = \'', $newLocation, '\'', PHP_EOL;
echo '// -->', PHP_EOL;
echo '</script>', PHP_EOL;
In my case I'm using it to more controlably recover from error situations after a header has already been sent.
To redirect in php, you use header("Location: $url");
i use of php and jquery in my application
i want when users submit success a form in page1 , forward form page1 to page2 and show a pop-up message(like this site) "success" and when they do not submit success , dont forward and just show pop-up message "error"
how i can implement this process?
thanks?
In your form, you can add some javascript into your form tag like so...
<form action="page2.php" onsubmit="if (CONDITION) {alert('Success'); return true;} else { alert('Error'); return false;}">
<input type="submit" value="Submit">
</form>
You can just a call a function ("return checkCondition();") in the onsubmit part and write the function in a separate Javascript file.
If the Javascript in the onsubmit part returns true, then it will go to the page specified in the action. If it returns false, then the form validation fails and it will stay where it is.
You would use something like this:
<?
if($form_success) { //
header("location: formpage2.php?success=1");
}
else {
header("location: formpage1.php?error=1");
}
?>
If you wanted to pass form data from page1 to page2 on success, use either or URL query string or store whatever's in $_POST in $_SESSION.
For the popup message, I would check for a success value in the query string of formpage2 and from there use javascripts alert to alert the user of their success.
I would not rely on Javascript itself in the first "return CheckCondition() in onsubmit" (by Muddybruin), but I would use it! I would not rely on it because the visitor CAN turn of Javascript and easily bypass the functionality.
I would also use the "header-redirection-answer" (by Levi Hackwith), but I would modify it to:
<?php
//checkform.php
if($form_success) {
//Include template or code here when form is successful
}
else {
//Include template or code here when form is unsuccessful
}
?>
If you absoutely must go to a specific file when form is successful, then I would include it instead of redirecting it. This is because redirections can cause unnessary issues regarding to links indexing in searchengines and it would be a lot slower than to just include directly into the checkform.php. Also keep in mind that header redirects must be sent BEFORE any other output is sent from the script.
I was wondering if there was any way through php or javascript I could tell the browser to go back to the page it came from, or even better not load the page at all (the later being probably impossible).
The reason for this is that I have written a small php script that will take parameters from the url and post a tweet for me discreetly while I am at work.
ex.
tweet.php?user=myname&pass=mypass&message=My message goes here
Though it works, I get stuck with a white page. It would be nice if I could have the browser go back to the page it was just on, so the pause between work would be minimal.
Thank you for the help!
javascript: history.go(-1);
The JavaScript function for this is window.back(). Have your PHP script produce something like the following to have browsers automatically "bounced back" to the submitting page:
<html>
<head>
<title>Success</title>
</head>
<body onload="window.back()">
<h1>Success</h1>
</body>
</html>
Non-JS browsers will see a "success" message, JS browsers will get bounced back.
You could do the following in PHP to redirect back to the previous page:
<?php
$ref = $_SERVER['HTTP_REFERER'];
header('refresh: 10; url='.$ref);
?>
Depending on the browser, either an HTTP response code of 204 or 205 might cause it to not leave the current page.
If use use a PHP HEADER, you can redirect to another point on the site. Minimal Pause work (as long as the process isn't very long).
In tweet.php, use the header function to redirect back to the referer
Sending people back to the page without any success message would be very confusing. I would make the call with AJAX and provide some feedback to the user that the action was performed successfully, very much like the voting system here on SO works.