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.
Related
I've got a registration page and a landing page, when the register button is pressed it activates a JS function in an external JS file (reg.js) that then runs reg.php, like so:
reg.html->reg.js->reg.php
If the registration succeeds it is supposed to redirect to a landing page (landing.php) but instead it is just appending the landing page to the bottom of the current page(reg.html), like so:
Register
[register button]
Landing
[landing page text]
The redirection code is:
header('Location: landing.php');
From the sounds of it, you're posting via AJAX to a php script to process. PHP would then need to send some sort of success response back to the AJAX—something like:
if (registered) {
header(201); // http response code for "created"
}
The AJAX callback would see that success, and then you would redirect the user client-side:
$.post().success(function() { window.location = '/landing.php'; });
Are you using javascript ajax ? If yes then you need to use
window.location = 'http://webiste.com';
If your javascript submits the form then you need to use header in PHP code like this:
header( 'Location: http://website.com/landing.php' ) ;
Don't forget to add http:// in the redirect url as this may be causing an issue.
<?php
if(registered)
{
header( 'Location: /landing.php' ) ;
}
?>
or try to input the whole url.
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>
so I made a simple user log in website, and I want to set a cookie so that when they come back to the website, it will take them to the members area and not the main page, kind of like a "remember me" function that redirects users to a members area if a cookie is set.
Problem I am facing: The php code right before the html code does not redirect to the member.php page even though the cookie is set!
Note: I'm just using parts of the code, and not the entire code/ other files to simplify the question.
here's my code:
main.php (this is the main page, and also where the log-in form is, but log in form is not shown)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php
//Checks if there is a login cookie
if(isset($_COOKIE["blablabla"])) //if cookie is set
{
header("Location: www.website.com/member.php"); //redirect to member.php
}
else
{
//otherwise, redirect to nocookiefound.php
header("Location: www.website.com/nocookiefound.php");
}
?>
<html>
<body>
<?php
echo "Welcome " . $_COOKIE["blablabla"] . "!<br />";
//I ran a echo test to see if cookie is still there, and it is.
?>
</body>
</html>
So my question is, can my redirect work the way it is?
I must be doing something wrong because it's not redirecting to member.php even though the cookie echos the correct value.
So, if i was originally in the members.php page after I logged in, then go back to main.php, it SHOULD redirect me to members.php, but it doesn't, it just stays at main.php. Anyone know what's going on? I would appreciate all the help I can get. Thanks
The PHP code should be the first thing on the page, as you're sending a redirect "Header". Move it to before the Doctype declaration.
As well as what #Dogbert says, your redirection is invalid. Try:
header("Location: http://www.website.com/member.php");
In addition to the other answers, put an exit(); after the header lines. It's possible to write a script that ignores headers, hence running the other code inadvertently.
People often use headers to protect admin areas without realising that.
Thanks to Col. Shrapnel I am using a VERY basic PHP script to send emails. The form is located here.
This is the script:
<?php
mail('JBIRD1111#gmail.com','Live Date Submission',implode("\n\n",$_POST));
header("Location: thankyou.html");
?>
When a user submits the form, they are redirected to a thankyou.html page. I want to edit the code to display a javascript alert, instead of a redirect. I don't have much PHP knowledge, so how would I edit this code to return a alert instead of a redirect?
Actually, if you want to send a Javascript alert instead I would recommend using some basic jQuery work. I would also take a look at the AJAX section of the documentation.
Otherwise you can inset some javascript in the original form page.
session_start();
$_SESSION['message'] = "<script>alert('Thank you so very much! You rock!');</script>";
header("Location: originalformpage.html");
and on the original form page
session_start();
if(isset($_SESSION['message']))
{
echo($_SESSION['message']);
unset($_SESSION['message']);
}
Replace the header line with:
print("<script>alert('Thank you so very much! You rock!');</script>");
Not tested but should work.
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