Header location to remain the same - php

I am currently creating a podcast website where I have a form for my users to enter a comment on the current episode they are listening to.
All my content is stored in mysql and data on my episode page is displayed like so
http://www.mysite.com/episode.php?id=1
With separate content for the episode.
On this post adding to database no repeat on refresh
I mentioned about the form resubmitting on refresh which is why I added
header('Location: index.php');
However. I would like this to visit the same page with a thank you message echoed.
I have tried
header('Location: episode.php?id=<?php echo $data['cast_id']; ?>');
echo "thank you";
But this brings back the error Parse error: syntax error, unexpected T_STRING
Please can someone advise me on the best way to do this? Thank you.

header('Location: episode.php?id=<?php echo $data['cast_id']; ?>');
you are using php tags in already started php tags.
you can simply do this ..
session_start(); // make sure session is up and running
// just before redirecting
$_SESSION['thankyou_msg'] = 1;
header('Location: episode.php?id='.$data['cast_id']);
now on index.php.
session_start();
if(isset($_SESSION['thankyou_msg'])){
echo "your thankyou message here";
unset($_SESSION['thankyou_msg']);
}

header() function is php function, so why you start php in it?
Also add exit() and store message in SESSION variable.
$_SESSION['msg'] = "thank you";
header("Location: episode.php?id=$data['cast_id']");
exit();

Related

php message passing with query string between pages

In PHP after logout send a query string from logout.php in header like header("location:login.php?call=logout"); and on login page i got that call query string variable to show message on login page that you are logged out or etc.
But if i will directly go to login.php?call=logout link then same you are logout messgage will appear. but no logout process followed at this time.
How could i get rid from this problem.
if direct url login.php?call=logout passed then no log out message should displayed.
logout.php
<?php
header("location:login.php?call=logout");
?>
Login.php
<?php
if($_GET['call']=='logout'){ echo "you are logged out.";}
?>
Put your messsage to session. Example:
$_SESSION['message'] = 'Some message';
header("location: login.php");
exit;
// login.php
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
Is that actually a problem worth solving? It's the easiest mechanism to display a message and unless you link to login.php?call=logout from anywhere else, nobody should ever get into this situation to begin with.
To "verify" that the message should actually be displayed you need to keep state server-side. Typically you'd use a session (even without logged in user) to store the information that a message should be displayed on that page.

HTML PHP html php redirect same page with additional message

I am creating a submit contact form. However when the submit button is pressed, i want it to go back to my contact.php page with an additional message at the top of the body page such as.
"We have received your email, our agent will contact you shortly"
i have two files, contact.php for form and send_form_email.php for email process
i have tried this
header("location: contact.php");
however this would only send me back to contact page without any confirmation
Can you please kindly help me ?
Regards,
Lex
The easiest way to do it is a query string.
header("location: contact.php?success=1");
Or something like that.
Then in your contact page, you check for that using $_GET['success'] and if it is set (you can use isset() to just see if its in the URL or check the actual value if you want to do more) display your message.
For something slightly more "complex", see this: PHP passing messages between pages
I suggest using POST in your form. Using GET in this is a bad idea.
http://www.w3schools.com/php/php_forms.asp
Then add
if($_POST["done"] == 1)
echo "We have received your email, our agent will contact you shortly";
You have a couple options.
The simplest would be to just have the form processing logic on the same page and then set your form's action attribute to that page.
You could do something like checking if you have a value posted to tell you if you need to process the page.
if (isset($_POST['my_value_from_form'])) {
// process
}
// body of page itself
That lets you put one pretty easily.
Another way would be to add a GET value by appending it to the URL:
header("location: contact.php?message=1")
And use that with $_GET['message'] to determine what to show. However, that ?message=1 will be in the URL of the page, so it may not be ideal.
The other way would be to set a session value before you direct, then check if that value is there (and also clear it after you display it.
// on send_form_email.php
session_start();
$_SESSION['message'] = 1;
// on contact.php
session_start();
if ($_SESSION['message'] == 1) {
// do something
}
unset($_SESSION['message']); // so it only shows once.
All ways have minor trade-offs, mostly just owing to how you organize your code. If I was going to implement it, I would use the session method.
In file Contact.php add
$done = $_GET['done'];
if ($done == 1){
echo "<div>We have received your email, our agent will contact you shortly</div>";
}
In file send_form_email.php add
header("Location: contact.php?done=1");
change your header to
header("location: contact.php?success");
and then on your contact.php add this at the top of your page
if(isset($_GET['success'])){
echo "We have received your email, our agent will contact you shortly";
}
you can make a session of message like this
$_SESSION['message'] = "your message";
and after that you do redirect insert this session inside message div and then unset it
<?php
session_start();
$_SESSION['message'] = "your message";
header("location: contact.php");
dont forget to start the session with session_start(); on top of your file contact.php
You need to use sessions for that. Example
session_start(); // start session
$_SESSION['message'] = 'We have received your email, our agent will contact you shortly';
header("location: contact.php");
In your contact.php
echo $_SESSION['message'];
unset($_SESSION['message']); // delete message so it doesnt display again

displaying a message after redirecting the user to another web page [duplicate]

This question already has answers here:
Redirect to another page with a message
(6 answers)
Closed 8 months ago.
I am doing HTML with PHP and MySql.
After some database operation that was performed by the user, my system redirects the user to the original database page in order to show him the updated table. (I am done with this part).
At the same time, I wish to display a message to the user on the original page (the one where the system moved) to notify him with the success of the operation. How can I possibly display this message?
Here's my php code that moves to the other page.
Header( 'Location: Database.php');
Header( 'Location: Database.php?success=1' );
And in the Database.php page :
if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
// treat the succes case ex:
echo "Success";
}
Store it in the session as sort of a "flash"-message:
$_SESSION['message'] = 'success';
and show it in Database.php after the redirect. Also delete its content after displaying it:
print $_SESSION['message'];
$_SESSION['message'] = null;
The advantage of this is, that the message won't be shown again every time the user refreshes the page.
you can do this:
$_SESSION['msg']="Updation successfully completed";
header("location:database.php");
on database.php
echo $_SESSION['msg'];
unset($_SESSION['msg']);
One solution is to put the message in a SESSION, in your php file. So in the original page, you get that SESSION variable, and display that.
ex:
in your php file:
session_start();
$_SESSION["message"]="MESSAGE OF SUCCESS"
In you original file:
session_start();
if(isset($_SESSION["message"]))
{
echo"SUCCESS OR THE MESSAGE SET IN THE VAR SESSION";
unset($_SESSION["message"]);
}
The best way to solve this problem is set a session message after the success of your operation in the process page.
Then in the redirected page check whether the session message is set or not.If it is set then simply echo that message.
The below code may help you.
$_SESSION['MSG']="Your data is saved";
Header( 'Location: Database.php');
exit;
//now in the database.php page write at the top
<?php
if(isset($_SESSION['MSG'])){
echo $_SESSION['MSG'];
}
?>//its very simple,you can also format the message by using different html attributes
Before redirecting to new page, you can set a cookie with the message you want to show, upon loading the original page you will see if this special cookie is set and if it is you can display the success message stored in the cookie.

required field error not appearing

When using this code to try an show an error, the page redirects to myaccount, but should only redirect there if the required field has been completed, for example;
error message appears if birth_country is not chosen
if(empty($birth_country))
{
$err[] = "ERROR - Enter Birth Country";
header("Location: personal.php?msg=$err[0]");
}
but if it is chosen, should redirect to...
header("location: myaccount.php?id=" . $_SESSION['user_id']. "");
exit();
However it always goes to myaccount no matter what
Thanks
After your first header put exit as well. Your code just continues on and it likely emits the second location header as well, negating the first.
Like Frits says, you will have to print out the error to the screen if you want it to be seen; however, you can not print out any HTML before a header or the header won't work. If you want to print the error on the redirecting page then I would suggest putting the error message in a $_SESSION cookie, then call and echo that cookie/variable on the redirecting page. But by doing this, you will need to use php session_start() at the top of the question page and the top of your redirect page in order to use the $_SESSION variables
Also, your second header
header("location: myaccount.php?id=" . $_SESSION['user_id']. "");
exit();
Should look like this:
header("Location: myaccount.php?id=$_SESSION['user_id']")
By using the double quotes, you don't need to add the $_SESSION[] tag, then add blank "" at the end.

PHP Pass Data with Redirect

PHP Redirect with Post Data
Hi,
I am a newbie PHP programmer and trying to code a small blog.
I will explain what I am trying to do.
page1.php: Has a table of all posts in the blog
page2.php: This page has a form where you can add a Post
Page 2 posts to itself and then processes the data, if it successful then uses header() to redirect back to page1 which shows the table.
Now what I want to do is to be able to have a small message on page 1 above the table saying your blog post has been successfully submitted but I’m not sure how I can pass data back to page 1 after the form processing.
Set it as a $_SESSION value.
in page2:
$_SESSION['message'] = "Post successfully posted.";
in page1:
if(isset($_SESSION['message'])){
echo $_SESSION['message']; // display the message
unset($_SESSION['message']); // clear the value so that it doesn't display again
}
Make sure you have session_start() at the top of both scripts.
EDIT: Missed ) in if(isset($_SESSION['message']){
You could also just append a variable to the header location and then call it from the page.
header('Location: http://example.com?message=Success');
Then wherever you want the message to appear, just do:
if (isset($_GET['message'])) {
echo $_GET['message'];
}
A classic way to solve this problem is with cookies or sessions; PHP has a built-in session library that assists with the creation and management of sessions:
http://www.php.net/manual/en/book.session.php
Here is a concise example:
Page 1
session_start();
if (isset($_SESSION['message'])) {
echo '<div>' . $_SESSION['message'] . '</div>';
unset($_SESSION['message']);
}
Page 2
session_start();
// Process POST data
$_SESSION['message'] = 'Hello World';
// Redirect to Page 1

Categories