Redirect header php to success page [duplicate] - php

This question already has answers here:
PHP Redirect to new page after form submission
(5 answers)
Closed 6 years ago.
I have a contact contact form that after submitting a message, if it is sucess it should go to the success.php page, but im having a issue, i want this page only be available when the user is redirect from the sendForm.php. How should i make this permission in my script?

Many ways simplest way is to send parameter in URL ex. success.php?ok=1
<form action="success.php?ok=1" ...>
And at top of success.php page write:
<?php if( isset($_GET['ok']) ){
?>
html page code here
<?php }else{
//code if not redirect from sendForm.php
?>
or send POST request to look more professional without annoying url parameters and use isset($_POST['ok']).

Related

Codeigniter loads views from functions on top of another [duplicate]

This question already has answers here:
Stop execution after call $this->load->view()
(6 answers)
PHP/codeigniter - use of exit()
(8 answers)
Forcing CodeIgniter to send view and stop working
(3 answers)
Closed 2 years ago.
I have a page at admin/show_queries with a form in codeigniter like so
<form action="" method = "post">
<input....
<input....
<input type = "submit" name = "view_query" />
</form>
Then in my controller Admin.php, I try to catch the event when someone presses view_query
public function show_queries(){
...
...
if($this->input=>post('view_query'){
$this->view_query_function();
}
$this->load->view('elex/show_queries');
}
In the same file Admin.php my view_query_function:
public function view_query_function(){
//DO Something
...
...
$this->load->view('elex/view_queries');
}
All of this is working and I'm able to go to the View_queries page without trouble.
Here's the problem, that I'm facing<
When I click the button view_queries and the view_queries page loads, it loads on top of show_queries page. Means, when the view_queries page is loaded, I can scroll down and I see the show_queries page, which should not be there. I check the url, and the url hasn't changed either.
What am I doing wrong here?
Add a return after calling the function:
if($this->input->post('view_query'){
$this->view_query_function();
return;
}

HTML/PHP Form Submission (what to do after) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
First question here, so please be patient with me.
I am developing a website for my company. I have an HTML form that uses a php file to email me the data entered by the user. But the user is routed to an ugly .php page with a simple "Thank you" message.
My question has several parts.
Is it standard to have a "Successful Submission" page which looks nice and fancy, or is it fine to just stay on the page the form is on?
If it's fine to stay, how do I execute the php code to send the data without leaving the page?
Further, if I stay on the page, how do I display a message over it saying something like "Thanks, your form was submitted"?
Considering question 1
Is it standard to have a "Successful Submission" page which looks nice and fancy, or is it fine to just stay on the page the form is on?
It is not mandatory to have a separate page to display response to the end user. Actually it depends on the scenario for which you are developing the page. The main intention is to provide the best UI experience for the end users / visitors of your site. If there is some crucial information with large content, then it is better to show it in another page after form submission. If it is just a success message then it is fine to stay on the same page after form submit and show the response message there.
Now coming to question 2 and 3
If it's fine to stay, how do I execute the php code to send the data without leaving the page?
Further, if I stay on the page, how do I display a message over it saying something like "Thanks, your form was submitted"?
You can accomplish it using ajax. With the help of jquery you can make it much simpler using
$.ajax function
Ex :
$("#formbutton").click(function(){
$.ajax({
type: "POST", // method
url: "savecontact.php", // call to external php file
data: {name:n,contact:c}, // passing variables to php file
success: function(result) {
// get the response after ajax call is successful
},
complete: function(result){
}
});
});
Now in savecontact.php you write the necessary code (usual php code to insert into table) to save the data and return response.
Ex:
result = mysqli_query("INSERT INTO table (NAME ) VALUES ('val')"));
if($result)
{
echo "Success";
exit;
}
else
{
echo "Error";
exit;
}
After ajax call gets executed, inside success function of ajax call you will get the response either Success or Error and based on that you can display the message into an empty div which normally placed above the form.
success: function (response) {
if(response == "Success"){
$("#div").html("Thanks, your form was submitted");
}
else{
$("#div").html("Issue with form submission.Please try again");
}
}
The appropriate message will gets display after form submit as ajax response.
Is it standard to have a "Successful Submission" page which looks nice and fancy, or is it fine to just stay on the page the form is on?
Answer: It will be good if you show Thank you!!! message on same page if the page is having other content also.
If it's fine to stay, how do I execute the php code to send the data without leaving the page?
Answer: You can use AJAX to send async request to server with data filled in form.
3.Further, if I stay on the page, how do I display a message over it saying something like "Thanks, your form was submitted"?
Answer: Once you get success response from AJAX you can change the HTML code with Thank you message, and if you get some error while submitting form, you can show that error on same page without refreshing page.
It usually is based on your preferences.
For me, I usually include my validation script on the action page (Redirects to a blank page temporarily). I save my post variables into my session so I can reload the form later. If there is an error, I save the error message in a session variable then redirect to my form page. In my form, I include in each input element this fragment
value=<?php if isset($_SESSION['var_name']){echo $_SESSION['var_name'];}?>
If the data is valid however, I will execute certain stuff (database inserts, mailing, etc...) before redirecting to my landing page (usually the homepage).
As to properly answer your 3 questions:
Like I said, it is preferential. It can range from a simple pop-up to redirect,to a full fledged page. However, it IS standard to inform your user about the submission's status. He won't be doing guess work on your page.
If you want to stay on your form page, try to send your form through AJAX to your validation script. Nice and simple
Refer to #2. You execute your code in the AJAX snippet
Yes you can use same page which your going to submit form.
You can set action to another .php page as example sendEmail.php
action="sendEmail.php" method="post"
and you can get form submitting value in there. when the email sent you can put condition like this
if(emailSent == true){
header('Location: http://www.example.com/index.php?success=true');
}
when the sendEmail.php redirect to the index page you can get url value using php. And display the success message
$paraVal = $_GET["success"];
if($paraVal == true){
// display message
}

Display a message when a form is submitted to run a PHP script [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 5 years ago.
I am a bit puzzled with this one. I am trying to display a "Please wait..." message when a form is submitted (see code below) that is running PHP on the same page. It appears to run the PHP and then reloads the page.
I have added the sleep(10) just for testing purposes.
Can anyone help?
Thanks,
John
EDIT: Ok, I now understand the difference between server and client side. So - my question is: How do I display a message when the submit is clicked and before the PHP is run?
<?php
if (isset($_POST['submit'])) {
echo "Please wait..."; //display a loading message
}
?>
<form action="test.php" method="post">
<input type="test"></input>
<button name="submit" type="submit">SEND</button>
</form>
<?php
if (isset($_POST['submit'])) {
sleep(10); //run a script that takes a while
}
?>
Your PHP script won't be able to send the message and then redirect. You need to use JavaScript on the wait page and redirect. If you are not trying to redirect, not sure why the please wait message would be needed.
setInterval(function(){window.location.href="your_url" }, 10000);
For the download script, it needs to set the content type to PDF which means you can't send anything before that (header already sent error otherwise).
Two possible solutions:
1. You should display your 'please wait' on the page where the user clicks and open the download script in another page.
2. The download page can display the html and then use an iframe for downloading the file. Using jQuery and iFrame to Download a File

Page refresh causes form submission [duplicate]

This question already has answers here:
Avoid resending forms on php pages
(6 answers)
Closed 9 years ago.
I have a webpage that contains a form that uses the POST method and references the same page it is on for submission. I am using a PHP include file that contains an if statement that runs when the submit value is set. For some reason though, after one submission, every time you refresh the page it submits the form with the previously submitted data (The browser warns of this before refreshing the page). What causes this, and what could I be doing wrong?
This is expected. You should have the form submit to a handler that has a unique URL, whether it be a query string or a different URI. One solution (of many) would be to change your form action:
<form action="?action=submit" method="post">
<input type="hidden" name="action" value="submit" />
...
and then in the PHP script handle the form, then change the context back to a URL without the hidden query string
if (!empty($_POST['action']) && $_POST['action'] == 'submit') {
// do stuff
header('Location: '.$_SERVER['SCRIPT_NAME']);
die();
}
Note the query string is not actually present in $_POST but we keep it there so browsers don't consider it to be a redirect loop.
i had the same issue with one of my pages.
the reason is that when the browser warns you that it will submit the form again, that means it is going yo be the same exact thing when you click on a submit button.
I did 2 things to avoid it but i am sure there many other ways.
1. Do not let the page echo the form again after succesfull submission of the form.
mine was like this
<?php
if(!isset($_POST['submit'])) {
include(form.php);// you can modify this according to your needs.
} else {
//display your message about what happened with the form.
}
?>
with that approach, your page will not the contaion a form to submit HOWEVER this will not prevent it from submitting on refresh.
2. if the form is submitted create a contoller input that carries a value indication that the form is already submitted. for example , place this into your form:
<?=(isset($_POST['submit']))?"" :"<input type-"hidden" name="submit_stat" value="true" />" ; ?>
and when you process your form when it is submitted check it with your php and make the script act on that variable like this:
<?php
if($_POST['submit_stat']==true) {
//do not process the form here.
//stop your script
}
?>
Another thing you can do is redirect your page to another page other than the page that handles the form. i believe this is the safest one.
Another Way to prevent this is to move the Post Data to Session, redirect, collect Post back from Session and delete Session Post Data.
if(!empty($_POST) && empty($_FILES)){
// move post to session
// redirect to same url (don't forget possible get query)
}else{
// collect post from session
// unset post from session
}
Build this as default and you should never have problems with post data.
Only exceptions are File uploads. In this case redirect *after* post processing manualy.

Change the url after clicking the submit button [duplicate]

This question already has answers here:
Redirecting after form submission with user input in the URL
(3 answers)
Closed 8 years ago.
I have a form that filters the products by some characters such as size/price etc..
I would like to change the url variables values according to the filter result for example:
Before submit:
www.exmaple.com/product.php
after submit
www.example.com/product.php?size=1&price=300
How can I do that?
Try something like this
if(isset($submit))
{
//After clicking the button , do some operations here....
//Your code...
//More code...
header("location:product.php?size=1&price=300");
}
EDIT :
This is most common error check this thread. Also, if that doesn't work. Replace your header with
echo "<script>document.location.href=product.php?size=1&price=300</script>";

Categories