• 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)
  • Please user a header location inside the code to clear the $_POST.

       $current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    	   header('Location: '.$current_url);

    Here the complete 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
    	  }
           $current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    	   header('Location: '.$current_url);
    	  
    	}
    	
    
    	?>
    
    	<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>
    Thread Starter Luiz Araújo

    (@infolu)

    Hello @sumithsuku11 thanks for your time, unfortunately for me I returned the error below, an extra info, I am using my code inside a shortcode, I do not know if this has to do with the error below.

    ERROR
    Warning: Cannot modify header information – headers already sent by (output started at

    tranks

    HI,

    It happened because anything echo or print before the header location call.
    Currently i have modified the code please check it.

    <?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
    	  }
    					   $current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    						  if(!headers_sent())
    						     {
    								   header('Location: '.$current_url);
    								   exit;
    						    }
    						else
    						    {
    						?>
    							<script language="javascript">
    							window.location.href = "<?PHP echo $current_url;?>";
    							</script>
    						<?php
    						}		
    	  
    	}
    	
    
    	?>

    Regards
    Sumith

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.