Php reload page with post data

Hi I have a form that adds attachments to a post, however when the form posts it obviously doesn't show the post with the new attachment echoing "your file has been uploaded". When the user refreshes the page (to try and show their new attachment) the form posts again!

Is it possible to either (1) stop the form posting again on refresh, (2) automatically refresh the page to display the post with its new attachment?? (2 is way better)

ID;
if ( isset( $_POST['html-upload'] ) && !empty( $_FILES ) ) {
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$id = media_handle_upload('async-upload', $post_id); //post id of Client Files page
unset($_FILES);
if ( is_wp_error($id) ) {
    $errors['upload_error'] = $id;
    $id = false;
}

if ($errors) {
    echo "

There was an error uploading your file.

"; } else { echo "

Your file has been uploaded.

"; } } ?>

Thanks! :)

asked Apr 20, 2013 at 15:27

Instead of …

} else {
    echo "

Your file has been uploaded.

"; }

… redirect to another address on success:

} else {

    $new_url = add_query_arg( 'success', 1, get_permalink() );
    wp_redirect( $new_url, 303 );
    exit;
}

Status code 303 triggers a GET request:

This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.

In your form handler check first if $_GET['success'] is set and its value is 1, then print a success message. The visitor can reload this page again and again – and nothing will be sent.

answered Apr 20, 2013 at 15:34

Php reload page with post data

fuxiafuxia

105k34 gold badges244 silver badges440 bronze badges

5

Basically, the general idea is to redirect the user to some other pages after the form submission which would stop the form resubmission on page refresh but if you need to hold the user in the same page after the form is submitted, you can do it in multiple ways.

Unset Form Data

One way to stop page resubmission on page refresh is to unset the form data after it is submitted so that the variable storing form data becomes empty and wrap up your form processing block of codes to check if the form is empty.

if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
   $data = // processing codes here
   unset $data;
}

This will make $data empty after processing and the first clause would stop form resubmission on page refresh.

Javascript

This method is quite easy and blocks the pop up asking for form resubmission on refresh once the form is submitted. Just place this line of javascript code at the footer of your file and see the magic.


More on such queries you can visit this link.

answered May 11, 2018 at 8:54

JiwanJiwan

312 bronze badges

I had the same problem and it was "complicated" by the fact that I redirect all URLs to a single .php file. It turned out all I had to do to solve it was to insert the following piece of code before any html is written to the page.


This will redirect to the same page after the postdata is processed as you want it. After the redirect the original postdata is vanished and will not trigger the resubmit. The function 'header()' will only work if you insert it before anything is written to the page. exit() is necessary.

You have to process the post data before you place the header().

answered Sep 11, 2013 at 23:41

I solved this my using the template_redirect action (either in a a plugin or on your themes functions.php file).

add_action('template_redirect','myfunction') ;

function myfunction() { 
  global $post;
  $post_id = $post->ID;
  if ( isset( $_POST['html-upload'] ) && !empty( $_FILES ) ) {
     require_once(ABSPATH . 'wp-admin/includes/admin.php');
     $id = media_handle_upload('async-upload', $post_id); //post id of Client Files page
     unset($_FILES);
     if ( is_wp_error($id) ) {
        $errors['upload_error'] = $id;
        $id = false;
    }

  }
  $error = ($errors) ? 1 : 0;
  wp_redirect( "/?p={$post_id}?errors={$error}",301);
}

Then on your page you can check for errors

if ($_GET['errors']) {
    echo "

There was an error uploading your file.

"; } else { echo "

Your file has been uploaded.

"; }

(I've not tested this code.. it should give you a good starting point).

answered Sep 12, 2013 at 1:33

SunilSunil

1014 bronze badges

1

You can also test for $_SERVER['REQUEST_METHOD'] === 'POST' to make sure that the page has been refreshed via POST before processing the form.

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
   // Do Stuff
}

answered Sep 12, 2013 at 22:40

JakeJake

1,78618 silver badges16 bronze badges

an easier way is to remove the action from your form and have a hidden action so for example:


have your controller checking for validity and for submissions so to check if the form was submitted:


this method is very useful for scripts where you are adding or removing records on an admin page as it would reload the page without resubmitting and the record removed or added would be either removed or added to the page :)

answered Jan 9, 2016 at 22:06

The best method I found is using javascript and css. Common php redirection method header('Location: http://www.yourdomain.com/url); will work but It cause warning " Warning: Cannot modify header information - headers already sent" in different frameworks and cms like wordpress, drupal etc. So my suggestion is to follow the below code

echo ''; 
echo ''; 
exit;

The style tag is important otherwise the user may feel like page loaded twice. If we use style tag with body display none and then refresh the page , then the user experience will be like same as php header('Location: ....);

I hope this will help :)

answered Jun 23, 2017 at 12:33

Php reload page with post data

AjithAjith

1011 bronze badge

Solution 1 : If you are inserting post in post type then just generate the random code using rand() php function and save the code while inserting post. Also set the same code in $_POST['uniqueid'] variable.

Then check on insert if the the current uniqueid is there in posts or not, you can do the same using wp_query().

Solution 2 : Redirect to another page using javascript. But kindly not that user can go back to page and on again it will save post on refresh. For this issue the first solution is best.

Solution 3 : Use ajax to insert post there will be no issues like this. But if you want page hits. It's not a good idea, may be for Google Adsense.

I just tried to help by my experience. If anyone need code to fix the problem, simply ask, I will share.

Tony Djukic

1,8863 gold badges13 silver badges25 bronze badges

answered Apr 30, 2020 at 22:05

the simple and easy way to do this in php. example here:

dont forget the html tags(this form wont allow it here so i use [this])

$firstname = $post[firstname]

$lastname = $post[lastname]

[form action="here.php" method="post"]

first name[input type='firstname' name='firstname']

last name[input type='text' name='lastname']

[input type=submit value=submit]
[/form]

if($firstname==""||$lastname=="")
{

echo"the firstname and lastname are empty";

}
else
{

mysql_query("YOUR DATABASE INSERT HERE");

header("Location:here.php");

}

this is how to keep the data enter in the database from posting the same data twice on page refresh

Pieter Goosen

53.8k22 gold badges109 silver badges200 bronze badges

answered Dec 7, 2014 at 15:14

0

How to reload page in PHP?

Below example illustrates the use of header() to refresh current page in PHP: Example: This example uses header() function to refresh a web page in every 3 seconds. header( "refresh: 3" ); echo date ( 'H:i:s Y-m-d' );

How reload the current page without losing any form data in PHP?

The easiest way to reload the current page without losing form data, use WebStorage where you have -persistent storage (localStorage) or session-based (sessionStorage) which remains in memory until your web browser is closed. window. onload = function() { var name = localStorage.

How stop resending data refresh in PHP?

Linked.
-2. How to stop re-sending Email when the page refresh..
what means by top of the page in php file..
Don't allow to resend POST information after reload..
Avoid "resend form confirmation after login..
Loading a new view doesn't change the URL and and user can resend data under CodeIgniter..
Reset the form data on unload..

How do I keep my data after form refresh?

How to Keep Form Data After Submit and Refresh Using PHP.
Keep Form Data on Refresh Using the PHP Ternary Operator. The ternary operator is a shorter and more practical version of the standard if/else statement. ... .
Keep the Form Data on Refresh with the PHP Null Coalescing Operator..