Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a form on my website, that creates a post from front end. So every time when I refresh a page I got this message first:

The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?

If page is refreshed then previous action is done that is post with same entries as is in previous post is created. CODE is::

<form action="" method="post" name="story_form" onsubmit="return vedioform()"  enctype="multipart/form-data">
    <label>Add link to your video</label><br />
    <input type="text" class="input-big" name="cntnt"/>                                       
    <label>Name</label><br />
    <input type="text" class="input-box" name="fname" />                
    <label>Email</label><br />
    <input type="text" class="input-box" name="email"  />
    <label>Your Wix website</label><br />
    <input type="text" class="input-box" name="title"/>
    Excerpt::<input type="text" class="input-box" name="excerpt"/>
    custom:::<input type="text" class="input-box" name="custom"/>
    <label>Your picture (Type: jpeg, jpg, png, gif)</label><br />
    <input type="file" size="60" id="BrowserHidden" onchange="getElementById('FileField').value = getElementById('BrowserHidden').value;" name="thumbnail"/>
    <input type="text" id="FileField" name="filefield" />
    <input type="submit"  class="form-submit" id="submit" value="SUBMIT" name="submit-video"/><br />
</form>

<?php
$title = $_POST['title']; //from your form...
$content=$_POST['cntnt'];
$excerpt=$_POST['excerpt'];
$custom = $_POST['custom'];
//-- Set up post values
$myPost = array(
    'ID' => '',
    'post_title' => $excerpt,
    'post_excerpt' => $excerpt,
    'post_status' => 'publish',
    'post_type' => 'post',
    'post_author' => $authorID,
    'post_content' =>$content,
    'comment_status' => 'closed',
    'ping_status' => 'closed',          
    'post_category' => array('7'),
);

$new_post = wp_insert_post( $myPost);
add_post_meta($new_post, 'rating', $custom, true);
?>
share|improve this question

1 Answer 1

That is more or less how post works. What you need to do is what WordPress does on the backend.

  1. Submit the form
  2. Process it
  3. Redirect back to the original page.

That way, a refresh doesn't pass post data. There is a name for that process but I can't remember what it is right now.

get does not issue that warning, by the way, but will "double insert".

A big part of your problem is that the way your code is written, every time the page loads you are building the $myPost variable and running wp_insert_post( $myPost); You aren't even so much as checking to see if you have $_POST data before doing that. You are trying to insert a post whether the form has been submitted or not. Luckily, wp_insert_post will die if it is missing a couple of key parameters. At the very least you should have...

if (!empty($_POST)) {
    $title = $_POST['title']; //from your form...
    $content=$_POST['cntnt'];
    $excerpt=$_POST['excerpt'];
    $custom = $_POST['custom'];
    //-- Set up post values
    $myPost = array(
        'ID' => '',
        'post_title' => $excerpt,
        'post_excerpt' => $excerpt,
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_author' => $authorID,
        'post_content' =>$content,
        'comment_status' => 'closed',
        'ping_status' => 'closed',          
        'post_category' => array('7'),
    );

    $new_post = wp_insert_post( $myPost);
    add_post_meta($new_post, 'rating', $custom, true);
}

At least that won't run every time the page loads. However, you should really check the elements before you use them...

$title = (isset($_POST['title'])) ? $_POST['title'] : '';

And I'd try to sanitize it as well. Then you should process the form early in the page, not at the bottom, and redirect back, using wp_safe_redirect, if the form submission is a success. Since this is the front end that means you will need to process the form before the get_header function so that your redirect happens before any content is sent to the browser.

Really, and no offense intended, but your problem is bigger than you think it is. You have very badly designed code there, in several different ways.

share|improve this answer
    
I want post is created from front end when a form is submitted then a post will be created . this is working fine with above code .But problem is that after submit form a duplicate post also created if i reload the page even no entry inside it. I have tried get,redirect to thankyou page but problem still remains –  Monika Nov 29 '12 at 4:00
    
"But problem is that after submit form a duplicate post also created if i reload the page even no entry inside it."--- Yes, I know. That is what I told you how to solve. get will still make duplicates but it won't issue a warning. You need to post then redirect. That does work. –  s_ha_dum Nov 29 '12 at 5:45
    
This code is not bad i have shown you clean code .I have tried many options(js,jquery,location,reset,isset etc) after too much google but nothing solved the problem .Is there any solution to remove duplicate post.your solution is also not working. –  Monika Nov 29 '12 at 9:02
    
@Monika, I explained exactly why the code is poorly written. My solution does work. I don't know if it is 'standard' but WordPress uses the technique and so do I, all the time. Your implementation doesn't work and I suspect that it doesn't work because the rest of your code is so ill-conceived, so I edited my answer to explain that. I don't know what is wrong with your attempts to redirect because you haven't posted that code. –  s_ha_dum Nov 29 '12 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.