I want use a single php file to handle all of my voting requests.
Currently the script will, if siteType isn't set, forward the user and display a message. If the user has JS on then it will return a json object and ajax the page.
if(!isset($_COOKIE['siteType'])){
displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
header('Location:/');
exit;
}
I need it as that if this php code above is executed the page will reload, i assume with javascript and reading the http headers?
[Edit]
I didn't make myself clear enough, but this is a ajax request. I don't output any html. So this really is just about getting js to handle the header?
You can't Refreshing a page with javascript using php header('location')
Because, header('Location: xxx'); must be the only output of your PHP script, it is a header, you can not put it after javascript syntax
PHP
echo '<meta http-equiv="refresh" content="0">';
Javascript
window.location.reload();
Maybe this would be some solution for you,
if(!isset($_COOKIE['siteType'])){
displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
echo '<script>window.location.reload()</script>';
exit;
}
Related
For my login page, currently if the password is incorrect I have to redirect to another page. Is there any way I just insert or show the error message that shows up on the error page on the regular login page using PHP, thus eliminating the need for an error page? i could easily do it with jQuery ($("#error").css("visibility","visible");, but I don't know how to interact with the DOM using PHP, because it's executed on the server.
You can make all the Javascript(including Jquery) and HTML work using PHP by simply echoing the data. For example:
<?php
echo "<script>$('#error').css('visibility','visible');</script>
?>
Remember using Single quotes in the Javascript so as not to clash with double-quotes of echo function of PHP.
You really don't have to hide/show elements if I understand your issue correctly. You just echo out an error message when there are an error. Something like:
<?php
$showErr = false;
if ($err == true) {
$showErr = true;
}
//Put this part in code where you want content of the #error to be displayed
if ($showErr == true) {
echo '<div id="error">ERROR MESSAGE HERE</div>';
}
?>
If the error message needs to be generated in PHP, you may want to consider using AJAX. It is a JavaScript method for getting data from the server and doing something with it in client-side code, without changing the page of the web browser. However, it gets a bit more complicated to do it right, and doing it wrong will just make everything slower and buggier.
The idea would be to use an AJAX request (such as jQuery's ajax function) to submit the form data to a specially crafted link which returns a fragment parseable in JavaScript instead of an entire web page. That fragment would tell the client whether the log in was successful or not. If successful, the fragment should contain login credentials which the JavaScript callback should use to set any session cookies and redirect to whatever page you should end up on. If not successful, the fragment should contain the error message which the callback should display to the client. The JavaScript handling the AJAX request must also account for any transmission errors and it should provide feedback to the user that the form was in fact submitted; these things are provided by the browser with normal form submission but not with AJAX requests.
I am trying to write a page in php which shows a loading message while it does some processing and then auto redirects to another page
<?php
//show a loading message - this is the bit I need help with
// do some processing - don't need help with this bit
header("Location: http://www.mysite.com/mynextpage.php");
exit;
?>
I can't use echo or javascript otherwise I get a "Cannot modify header information - headers already sent" error when the page executes.
Any clues?
First of all, you mustn't use any header change after outputing some data, that is why you get the error above.
Another way, use header redirections by refreshing page on next page:
<?php
header('Refresh: 5; url=http://www.mysite.com/mynextpage.php' );
echo 'Wait 5 sec then redirected';
Note:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Best way is to use ajax requests. Via javascript you should show the loading element, perform the request, on success redirect on target page
Using java script to redirect after the processing has finished seems to be the way to go.
I'm using
<script type="text/javascript">
window.location="http://www.mysite.com/mynextpage.php";
</script>
at the end of the page and it's working
i am doing a simple job. i am calling a ajax function like call_ajax(file, container_id) from the file just as parent.php.
which process the page file and get reponse to the desired container_id.
here file is file1.php. now on file1.php is running successfully and get back response.
but the problem is that when i going through some checking just as login comfirm i have to redirect to the login page such as login.php from file1.php and stop transfer response to the parent.php.
i am using header function of php to transfer but yes it transfer and get response as login page in the container id. I know that its correct as by ajax behaviour what ever target file get content return as response. but what is the solution of my problem. i would not like found my login page in container_id and want to load login page from file1.php.
Well i dont really know how you did your pages but for me i would use a meta tag. It best used if the person is not logged in and access the page . this is the code i used. So place this in your php coding so when ajax calls the files it will run through the php files and when it hits this line it will "refresh" another page and exit the main one
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=Login.php">';
exit;
The person will be redirected immediately. And if you can Please Post your codes!
You Can use Some thing like this to Change your location with in an ajax call.
echo "script top.location='login.php'; script ";
exit;
We are looking to redirect users to different pages after code has been executed, eg. form validation, session timeouts etc..
Placing the following in the code:
header("Location: http://localhost/example/"); exit();
Appears to do nothing more than exit() the code being executed at the correct point. It does not redirect the page to that URL.
How do we physically redirect the page to another URL.
You likely have whitespace in the output before the Header() function is called. The header cannot be called after ANY output is sent back to the browser.
If you can view the output of that file, you might see that there is some whitespace created by an include or something else in your script prior to the Header() being called.
If you cannot avoid output prior to that call, you might have to resort to a javascript to do the redirection. A JavaScript Window.Location should do the trick.
How about using the HTML Redirect after executing the necessary code?
echo '<meta http-equiv="Refresh" content="0;url=http://localhost/example/" />'
After the php script is over, how do I go to another html page?
Why would you want to do that? When the page is over (which I understand as the script ended execution), it is usually sent to the client and then it can be viewed by the user. So, basically, what you are trying to do is to redirect the user after the script stopped executing.
If so, you have two solutions available:
Do not output anything, and after your script stopped executing, use the header() PHP function:
header('Location: http://example.com/some/url');
where you should replace the example URL with your own.
If you are outputting the HTML page and sending it gradually to the user, you can just put JavaScript redirection script at the end of the page (so after everything has been sent):
<script>
window.location = 'http://example.com/some/url';
</script>
Does any of these solutions work for you?
header('Location: /page.html');
Make sure you don't output anything else, then simply
header('Location: http://www.example.com/');