Note that there are some explanatory texts on larger screens.

plurals
  1. POForm resubmit issue on page refresh - while on preview page in wordpress
    primarykey
    data
    text
    <p>I run wordpress as CMS. I have put together a simple multi-part form which works pretty well and allows to post from front-end. It is a two part form where the first part is where he submits data and second step where he gets a preview of data he just submitted. The submitted data creates a nice draft post in wp as expected. </p> <p>Here is a first chunk of code within the form's page template, which displays the form. In the lower part of the code you will note a <strong>PREVIEW SECTION</strong> (<em>read the comments within code</em>) which displays the data that was just submitted. All of this works really well, I have even been able to <a href="https://stackoverflow.com/questions/19222295/form-data-and-image-preview-after-form-submit">retrieve an image</a>, after the submit. </p> <pre><code> &lt;?php global $wpdb; $this_page = $_SERVER['REQUEST_URI']; $page = $_POST['page']; if ( $page == NULL ) { ?&gt; &lt;form method="POST" id="test_form" name="test_form" enctype="multipart/form-data" action=""&gt; &lt;div&gt;LOCATION : &lt;input type="text" name="location" id="location"/&gt;&lt;/div&gt; &lt;div&gt;DESCRIPTION : &lt;textarea id="details" cols="80" rows="10 maxlength="600" name="details" rows="20"&gt;&lt;/textarea&gt;&lt;/div&gt; &lt;div&gt;UPLOAD IMAGE : &lt;input type="file" name="loc-image" id="loc-image" tabindex="25" /&gt;&lt;/div&gt; &lt;input type="hidden" value="1" name="page" /&gt; &lt;input type="hidden" name="action" value="post_action" /&gt; &lt;input type="submit" name="submit" id="submit" value="PROCEED"/&gt; &lt;/form&gt; &lt;?php } else if ( $page == 1 ) { ?&gt; &lt;?php include_once('validate_first_step.php'); ?&gt; &lt;?php if (isset($_POST['submit']) &amp;&amp; (!empty($error))) { ?&gt; &lt;h3&gt;Submission Failed. Errors highlighted below.&lt;/h3&gt;&lt;br/&gt; &lt;a href="javascript:history.go(-1)"&gt;GO BACK&lt;/a&gt;&lt;br/&gt;&lt;br/&gt; &lt;?php echo $error . '&lt;/br&gt;'; } else { ?&gt; &lt;?php echo 'h2&gt;'.'YOUR SUBMISSION IS ACCEPTED. PREVIEW.'. '&lt;/h2&gt;';?&gt; &lt;?php //PREVIEW SECTION OF THE FORM BEGINS $location=$_POST['location']; $description=$_POST['details']; ?&gt; &lt;?php echo 'Location : ' . $location . '&lt;/br&gt;'; echo 'Details : ' . $description . '&lt;/br&gt;'; ?&gt; &lt;?php echo wp_get_attachment_image( $newupload,'medium' ); ?&gt; &lt;?php //PREVIEW ENDS } } ?&gt; </code></pre> <p>This is how the form is processed. Actual code is pretty complex, I have just put here that was necessary to deliver the idea. Pretty standard stuff for inserting in wordpress.</p> <pre><code>if( 'POST' == $_SERVER['REQUEST_METHOD'] &amp;&amp; !empty( $_POST['action'] ) &amp;&amp; $_POST['action'] == "post_action") { // Do some minor form validation to make sure there is content if (isset($_POST['submit'])) { $error = ""; if ($_POST['details'] != null) { $description = trim(strip_tags($_POST['details'])); } else { $error .= 'Put description.&lt;/br&gt;'; } if ($_POST['location'] != null) { $location = trim(strip_tags($_POST['location'])); } else { $error .= 'Put location.&lt;/br&gt;'; } } if (empty($error)) { $new_post = array( //insert form inputs, set var and define array 'post_title' =&gt; $location, 'post_content' =&gt; $description, 'post_status' =&gt; 'draft', 'post_author' =&gt; '2', 'post_type' =&gt; 'post' // assigning tags and categories are no issue ); $pid = wp_insert_post($new_post); //attachment helper function function insert_attachment($file_handler,$post_id,$setthumb='false') { if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); } require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); $attach_id = media_handle_upload( $file_handler, $post_id ); //set post thumbnail if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id); return $attach_id; } //INSERT OUR MEDIA ATTACHMENTS if ($_FILES) { foreach ($_FILES as $file =&gt; $array) { $newupload = insert_attachment($file,$pid); } } } //end of if empty error } </code></pre> <p><strong>ISSUE</strong>: Here is the issue that I noticed while testing the form. After the form submit when I am in the PREVIEW SECTION, data is displayed correctly as expected BUT when I REFRESH the page the same data is submitted again into wordpress, creating a duplicate draft post.</p> <p><strong>THINGS TRIED</strong> : I am aware of a redirection solution and able to employ <a href="http://codex.wordpress.org/Function_Reference/wp_redirect" rel="nofollow noreferrer">wp_redirect</a> , but redirection is not what I am looking at as I need to show them the preview. I have spent a few hours looking for solution and alternatives and still doing it. </p> <p><strong>REQUEST</strong> : Please let me know how I can stop this from happening. I cannot expect people not to click refresh, they will basically do whatever they want. So instead of a javascript notification I would like to perform this using server-side php. As it may perform even if the javascript is disabled, as will the rest of my code. Basically I need a solution that simply ignores the refresh, after the submit. Please suggest me a code or a hint to right direction that can make this possible. Many thanks.</p> <hr> <p><strong>UPDATE</strong> : As suggested by @user2758004 I tried the comparision method and it worked. I have put the following code at top of the processor.</p> <pre><code>$location=$_POST['location']; $args = array( 'post__not_in'=&gt; array($id), 'post_type' =&gt; 'post', 'post_status' =&gt; array('publish','pending','draft','future','private'), 'meta_query' =&gt; array( 'relation' =&gt; 'AND', array( 'key' =&gt; 'location', 'value' =&gt; $location, 'compare' =&gt; '=' ), ) ); </code></pre> <p>Now within the processor I check if the meta_value of the meta_key 'full_name' already exists. In case it does, the form displays an error and simply stops the process. Hope it helps someone.</p> <pre><code> $existingMeta = get_posts( $args ); if(!empty($existingMeta)){ $error .= 'Bummer, ' .$location. ' is already in database.&lt;/br&gt;'; return; } else { //check for rest of the errors } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload