• Hey everyone,

    I need some help getting a front end post submission form working. Right now, I have a form in the body of my page and the PHP to process the form at the top. The problem is that users can rapidly click submit and that leads to multiple copies of the same post being submitted.

    when you click submit, you get redirected to the post you just created. The problem is that when you post content and then hit back in your browser, you can click submit again, and another post gets submitted.

    can anyone tell me of a way, using PHP only, to make it so that once a page is submitted, you can’t keep clicking submit?

Viewing 2 replies - 1 through 2 (of 2 total)
  • better to provide your link or codes here

    Thread Starter np073189

    (@np073189)

    <?php
    
    	if(isset($_POST['submitted']) && isset($_POST['post_nonce_field'])) {
    
    		if(wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
    
    			$user_submitted_title = sanitize_text_field($_POST['user_submitted_title']);
    			$user_submitted_progress = $_POST['user_submitted_progress'];
    			$user_submitted_goals = $_POST['user_submitted_goals'];
    			$user_submitted_categories = $_POST['user_submitted_categories'];
    			$user_submitted_tags = sanitize_text_field($_POST['user_submitted_tags']);
    			$user_submitted_video = sanitize_text_field($_POST['user_submitted_video']);
    			$user_submitted_audio = sanitize_text_field($_POST['user_submitted_audio']);
    
    			if(($user_submitted_title != '') && ($user_submitted_progress != '') && ($user_submitted_goals != '') && ($user_submitted_categories != '') && ($user_submitted_tags != '')) {
    
    				$user_post = array(
    					'comment_status' => 'open',
    					'post_author'    => $user_ID,
    					'post_category'  => array($user_submitted_categories),
    					'post_content'   => '<h2>Project Progress</h2>' . $user_submitted_progress . '<h2>Project Goals</h2>' . $user_submitted_goals,
    					'post_status'    => 'publish',
    					'post_title'     => $user_submitted_title,
    					'post_type'      => 'post',
    					'tags_input'     => $user_submitted_tags
    				);
    
    				$user_post_id = wp_insert_post($user_post);
    				add_post_meta($user_post_id, 'wpcf-video', $user_submitted_video);
    				add_post_meta($user_post_id, 'wpcf-audio', $user_submitted_audio);
    
    				global $post;
    				if(!empty($_FILES)) {
    					$files = $_FILES['upload_attachment'];
    					foreach ($files['name'] as $key => $value) {
    						if(!empty($files['name'][$key])) {
    							$file = array(
    								'name' => $files['name'][$key],
    								'type' => $files['type'][$key],
    								'tmp_name' => $files['tmp_name'][$key],
    								'error' => $files['error'][$key],
    								'size' => $files['size'][$key]
    							);
    							$_FILES = array("upload_attachment" => $file);
    							foreach ($_FILES as $file => $array) {
    								$newupload = insert_attachment($file,$user_post_id);
    							}
    						}
    					}
    				}
    
    				$user_post_redirect = get_permalink($user_post_id);
    				wp_redirect($user_post_redirect);
    				exit;
    
    			} else {
    
    				$notice = 'Please fill out all required fields';
    
    			}
    
    		} else {
    
    			echo 'Sorry, but your post did not verify.';
    			exit;
    
    		}
    
    	}
    ?>
    <?php get_header(); ?>
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<?php if (is_user_logged_in()) { ?>
    
    					<form id="user_submitted_post" action="" method="POST" enctype="multipart/form-data">
    
    						<?php if (!empty($notice)) {
    							echo '<p>' . $notice . '</p>';
    						} ?>
    
    						<h2>Title</h2>
    						<input type="text" id="user_submitted_title" name="user_submitted_title" value="<?php if(!empty($_POST['user_submitted_title'])) echo $user_submitted_title; ?>">
    
    						<p>Please provide detailed information about the long-term goals of your project, as well as your current project progress. Feel free to format your text using boldness, italics, lists, and block quotes. Simply click on the formatting option you'd like to use and start typing. When you no longer need the format you have selected, hit enter to go to the next line, and click on the active format button to end formatting.</p>
    
    						<h2>Project Progress</h2>
    						<?php wp_editor($user_submitted_progress, 'user_submitted_progress', $settings = array('media_buttons' => false, 'quicktags' => false, 'textarea_rows' => 15, 'editor_css' => '<style type="text/css">.wp_themeSkin .mceListBox .mceText {width: 81px;} .wp_themeSkin table.mceToolbar {margin: 5px;} td.mceToolbar > div {height: inherit;} tr.mceLast {display: none;} .wp_themeSkin .mceButton {margin: 1px 12px;}</style>', 'tinymce' => add_filter("mce_buttons", "base_extended_editor_mce_buttons", 0), add_filter("mce_buttons_2", "base_extended_editor_mce_buttons_2", 0)) ); ?>
    
    						<h2>Project Goals</h2>
    						<?php wp_editor($user_submitted_goals, 'user_submitted_goals', $settings = array('media_buttons' => false, 'quicktags' => false, 'textarea_rows' => 15, 'tinymce' => add_filter("mce_buttons", "base_extended_editor_mce_buttons", 0), add_filter("mce_buttons_2", "base_extended_editor_mce_buttons_2", 0)) ); ?>
    
    						<h2>Category</h2>
    						<?php wp_dropdown_categories($args = array('selected' => $user_submitted_categories, 'orderby' => 'name', 'hide_empty' => 0, 'hierarchical' => 1, 'id' => 'user_submitted_categories', 'name' => 'user_submitted_categories')); ?>
    
    						<h2>Tags</h2>
    						<p>Separate tags with commas.</p>
    						<input type="text" id="user_submitted_tags" name="user_submitted_tags" value="<?php if(!empty($_POST['user_submitted_tags'])) echo $user_submitted_tags; ?>">
    
    						<h2>Video</h2>
    						<p>Copy and paste links from Youtube and Vimeo in the field below</p>
    						<textarea id="user_submitted_video" name="user_submitted_video"><?php if(!empty($_POST['user_submitted_video'])) echo $user_submitted_video; ?></textarea>
    
    						<h2>Audio</h2>
    						<p>Copy and paste links from Soundcloud in the field below</p>
    						<textarea id="user_submitted_audio" name="user_submitted_audio"><?php if(!empty($_POST['user_submitted_audio'])) echo $user_submitted_audio; ?></textarea>
    
    						<h2>Images</h2>
    						<input type="file" name="upload_attachment[]" multiple="multiple">
    
    						<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
    
    						<input type="hidden" name="submitted" id="submitted" value="true">
    
    						<button type="submit" name="submitbutton" id="submitbutton">Submit</button>
    
    					</form>
    
    				<?php } else { ?>
    
    					<?php echo 'Sorry, but you need to be logged in to see that. You can <a>
    					<?php echo wp_login_url( get_permalink() ); ?>
    					<?php echo '" title="Login">login here</a>'; ?>
    
    				<?php } ?>
    
    			<?php endwhile; // end of the loop. ?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>


    [Please use the code buttons for posting code here]

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Post Submission Form’ is closed to new replies.