Problems with form front end when pressing refresh page (F5)
-
I am having a problem, I have the form below for visitors to post, it is saving perfectly, my problem is that soon after the visitor click on subimit, and press f5 on the same page the post is registered again, I have a Hack in javascript that logo After posting it redirects to the same page, but I believe this is not ideal. Mainly visitors with javascript disabled.
Someone can help me to solve this problem, I did not find anything that works for me, below my basic code.
<?php $postTitleError = ''; $postcontentError = ''; if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) { if (trim($_POST['postTitle']) === '' || $_POST['postContent'] === '' ) { if(trim($_POST['postTitle']) === '') { $postTitleError = 'Please enter a title.'; } if(trim($_POST['postContent']) === '') { $postcontentError = 'Please enter a title.'; } } else { $post_information = array( 'post_title' => esc_attr(strip_tags($_POST['postTitle'])), 'post_content' => esc_attr(strip_tags($_POST['postContent'])), 'post_status' => 'publish', // Choose: publish, preview, future, pending, draft, etc. 'post_type' => 'post' ); $post_id = wp_insert_post($post_information); ?> <?php } } ?> <div class="col-xs-12"> <form action="" id="primaryPostForm" method="POST" enctype="multipart/form-data"> <div class="form-group"> <!--<label for="postTitle"><?php _e('Nome:', 'framework') ?></label>!--> <input type="hidden" class="form-control" autocomplete="off" name="postTitle" id="postTitle" value="" placeholder="Nome:" required /> </div> <?php if($postTitleError != '') { ?> <span class="error"><?php echo $postTitleError; ?></span> <div class="clearfix"></div> <?php } ?> <div class="form-group"> <!--<label for="postContent"><?php _e('Mensagem:', 'framework') ?></label>!--> <textarea name="postContent" class="form-control" autocomplete="off" rows="4" placeholder="Mensagem..." ></textarea> </div> <?php if($postcontentError != '') { ?> <span class="error"><?php echo $postcontentError; ?></span> <div class="clearfix"></div> <?php } ?> <div class="form-group"> <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?> <input type="hidden" name="submitted" id="submitted" value="true" /> <button type="submit" class="btn btn-primary btn-lg btn-block"><?php _e('Enviar', 'framework') ?></button> </div> </form> </div>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Problems with form front end when pressing refresh page (F5)’ is closed to new replies.