Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Submitting form only once

Featured Replies

Hi All only me again!

 

I have an input form which gets sent to a db, sometimes if the submit button is pressed twice by mistake the information is submitted twice into db, is there some way to only submit the form once?

 

Thanks!

One option is to use a javascript to disable the button as you click, but it got it's down sides.

 

The best is probably on the server side. I'm not sure what language you use, but here's PHP example:

http://phpsense.com/php/prevent-duplicate-...submission.html

i would personally go with Thomas' solution (server side), as people without Javascript will cause problems.

 

I assume that the server side suffers from lack of cookies though, so the safest option is to use BOTH.

I usually just use javascript but I've never done a system where it's crucial to only click once. You can always put some code to check for javascript first although that could annoy people. It really depends on how crucial it is to not have duplicates and how likely people are to try and get around it.

  • Author

Hi All

 

Thanks for the advice and link! This script is a bit of a beast:

im getting the error:

 

Parse error: syntax error, unexpected $end on line 187

 

I working from this tutorial:

 

http://www.webdesignerforum.co.uk/ipb_seo....submission.html

 

Ive double checked the {} but cant seem to see the problem, the line 187 is the last line of the script <div> tag?????

 

Thanks again!

 

<?php 

session_name ('YourVisitID');
session_start(); // Start the session.

// If no session value is present, redirect the user.
if (!isset($_SESSION['user_id'])) {

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
	$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/db_login.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}


$secret=md5(uniqid(rand(), true));
$_SESSION['FORM_SECRET']=$secret;




ini_set("memory_limit","38M");

/************************** change to match your connection method ********************/ 

require_once ('../mysql_connect.php'); // Connect to the db. 

/*************************** THE FUNCTION BIT **************************************/ 
function resize($name, $newname){ 


$size = 100; // the thumbnail height 

 $filedir = 'uploads/'; // the directory for the original image 
 $thumbdir = 'uploads/'; // the directory for the thumbnail image 
 $prefix = 'thumbnail_'; // the prefix to be added to the original name 

 $maxfile = '8000000'; 
 $mode = '0777'; 

 $userfile_name = $_FILES[$name]['name']; 
 $userfile_tmp = $_FILES[$name]['tmp_name']; 

	 $prod_img = $filedir.$userfile_name; 

	 $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 

   $returnName=$prefix.$userfile_name; // set a name to send back for the db 

	 move_uploaded_file($userfile_tmp, $prod_img); 
	 chmod ($prod_img, octdec($mode)); 

	 $sizes = getimagesize($prod_img); 

	 $aspect_ratio = $sizes[1]/$sizes[0]; 

	 if ($sizes[1] <= $size) 
	 { 
		 $new_width = $sizes[0]; 
		 $new_height = $sizes[1]; 
	 }else{ 
		 $new_height = $size; 
		 $new_width = abs($new_height/$aspect_ratio); 
	 } 

	 $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image:' . $name); 

	$srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image: '. $name); 

	ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing: '. $name); 

	ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving: '. $name); 

	imagedestroy($destimg); 

	unlink($prod_img);//Delete the original image 

	return $returnName; // return the name of the thumb nail 

   } 

/************************************** END OF FUNCTION ****************************/ 

//Retrieve the value of the hidden field
$form_secret=$_POST['form_secret'];

if(isset($_SESSION['FORM_SECRET'])) {
if(strcasecmp($form_secret,$_SESSION['FORM_SECRET'])===0) {

	if(isset($_POST['Submit'])) { ///// CHECK FORM  WHEN SUMBMITTED (if One) 

  // check that the first image has been selected if not return error 
if ($_FILES['imageOne']['name'] <> null) {	
  $nameOne = resize("imageOne","thumbOne");//resize function and return name 
   } else { 
  $whatsediterror[3]='<p class="style1"> You Need to select an image(1).</p>'; 
	$error='false'; 
  } // end if 




/**************************** CHECK FORM INPUT SECTIONS *********************************************************/ 

if(empty($_POST['heading'])){ /// CHECK HEADING FORM HAVE BEEN FILLED IN 
   $whatsediterror[1]='<p class="style1"> Please complete Heading.</p>'; 
   $error='false'; 
}  // end if 

if(empty($_POST['news'])){ /// CHECK NEWS FORM HAVE BEEN FILLED IN 
   $whatsediterror[2]='<p class="style1"> Please complete News.</p>'; 
   $error='false'; 


} else { 


/**************************** ALL PARTS OF FORM CHECKED *****************************************************/ 


  if (empty($error)){ // IF THERE ARE NO ERRORS THE CARRY ON 

	 // place uploaded thumbnail images into variables for db 
	 $imageOne=$nameOne; 



	 // obtain the input form details into variables for the db 
	 $heading=$_POST['heading']; 
	 $news=nl2br( $_POST['news']);


	 $query = "INSERT INTO newspage (heading, news, registration_date, file1) VALUES ('$heading', '$news', NOW(), '$imageOne')";  
	 $result = @mysql_query ($query); // Run the query. 

		 if (mysql_affected_rows() == 0) { // A record was added so what next 
		 echo '<p>There seems to be an error.</p>'; 
		 echo '<p>ERROR: The insertion into the db</p>'; 
			exit; 
		} // end if

	 /****************** ALL WENT OK AND ENTERED INTO DATABASE DO SOMETHING ***************************/ 

   $imagesize='height="100" width="100"';
   $imagesizeuploaded='height="1100" width="100"';
   $alignright='right';


	// display the heading,news and image 
  echo '<div id="maincentredbig">Add News</div><div id="maincentredbig1"><p class="alertHd" align="center">Latest News has been added.</p>';	
  echo '<p class="alertHd" align="left">' .$heading .'</p>'; 
  echo '<p class="alertHd" align="left"><img '. $imagesize .' src="uploads/'.$imageOne .'" align="'.$alignright .'" />'.$news.'</p></div>';



 unset($_SESSION['FORM_SECRET']);



	}else {
		//Secret key missing
		echo 'Form data has already been processed!';

 } // end if else 

} // end if error

} // end if form secret 

/****************** DISPLAY FORM WITH ERROR OR NOT ***************************/ 
?> 
<div id="maincentredbig">Add News</div> 
<br /> 
<div id="maincentredbigleft"><br /> 
<form action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" method="post"> 
<span>File 1 to upload : <input type="file" name="imageOne" /><br /></span><br /> <? echo $whatsediterror[3] ?><br /> 
<span>Heading: <input type="text" name="heading" <?php if (isset($_POST['heading'])) {?> value="<?php echo $_POST['heading'] ?>"<?php }?> size="53" /><span class="smallerrormessage">Required</span><br /><? echo $whatsediterror[1] ?></span><br /> 
<span>News: <textarea name="news" cols="52" rows="16"><?php if (isset($_POST['news'])) {?><?php echo $_POST['news'] ?><?php }?></textarea><span class="smallerrormessage">Required</span><br /><? echo $whatsediterror[2] ?></span><br /> 
<input type="Submit" name="Submit" value="Submit"> 
<input type="hidden" name="form_secret" id="form_secret" value="<?php echo $_SESSION['FORM_SECRET'];?>" />
</form> 
</div>

might be an idea to add to your submit button:

onclick="this.disabled='disabled';"

just for the quick JS protection.

 

Does the number of { equal the number of } ?

If so, then it is likely to be a missing ;

  • Author

Thanks penguin! Ive added the submit script but spent the last hour looking and checking for {};!

Ive commented the {} but still cant find the problem! Maybe I need glasses! I still get the same error even though I added a {!????

 

Will keep looking!

 

<?php 

session_name ('YourVisitID');
session_start(); // Start the session.

// If no session value is present, redirect the user.
if (!isset($_SESSION['user_id'])) {

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
	$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/db_login.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}


$secret=md5(uniqid(rand(), true));
$_SESSION['FORM_SECRET']=$secret;




ini_set("memory_limit","38M");

/************************** change to match your connection method ********************/ 

require_once ('../mysql_connect.php'); // Connect to the db. 

/*************************** THE FUNCTION BIT **************************************/ 
function resize($name, $newname){ 


$size = 100; // the thumbnail height 

 $filedir = 'uploads/'; // the directory for the original image 
 $thumbdir = 'uploads/'; // the directory for the thumbnail image 
 $prefix = 'thumbnail_'; // the prefix to be added to the original name 

 $maxfile = '8000000'; 
 $mode = '0777'; 

 $userfile_name = $_FILES[$name]['name']; 
 $userfile_tmp = $_FILES[$name]['tmp_name']; 

	 $prod_img = $filedir.$userfile_name; 

	 $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 

   $returnName=$prefix.$userfile_name; // set a name to send back for the db 

	 move_uploaded_file($userfile_tmp, $prod_img); 
	 chmod ($prod_img, octdec($mode)); 

	 $sizes = getimagesize($prod_img); 

	 $aspect_ratio = $sizes[1]/$sizes[0]; 

	 if ($sizes[1] <= $size) 
	 { 
		 $new_width = $sizes[0]; 
		 $new_height = $sizes[1]; 
	 }else{ 
		 $new_height = $size; 
		 $new_width = abs($new_height/$aspect_ratio); 
	 } 

	 $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image:' . $name); 

	$srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image: '. $name); 

	ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing: '. $name); 

	ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving: '. $name); 

	imagedestroy($destimg); 

	unlink($prod_img);//Delete the original image 

	return $returnName; // return the name of the thumb nail 

   } 

/************************************** END OF FUNCTION ****************************/ 

//Retrieve the value of the hidden field
$form_secret=$_POST['form_secret'];

if(isset($_SESSION['FORM_SECRET'])) {
if(strcasecmp($form_secret,$_SESSION['FORM_SECRET'])===0) {

	if(isset($_POST['Submit'])) { ///// CHECK FORM  WHEN SUMBMITTED (if One) 

  		// check that the first image has been selected if not return error 
	if ($_FILES['imageOne']['name'] <> null) {	
 	 $nameOne = resize("imageOne","thumbOne");//resize function and return name 
	   } else { 
 	 $whatsediterror[3]='<p class="style1"> You Need to select an image(1).</p>'; 
   	 $error='false'; 
  } // end if 




/**************************** CHECK FORM INPUT SECTIONS *********************************************************/ 

if(empty($_POST['heading'])){ /// CHECK HEADING FORM HAVE BEEN FILLED IN 
   $whatsediterror[1]='<p class="style1"> Please complete Heading.</p>'; 
   $error='false'; 
}  // end if 

if(empty($_POST['news'])){ /// CHECK NEWS FORM HAVE BEEN FILLED IN 
   $whatsediterror[2]='<p class="style1"> Please complete News.</p>'; 
   $error='false'; 


} else { 


/**************************** ALL PARTS OF FORM CHECKED *****************************************************/ 


  if (empty($error)){ // IF THERE ARE NO ERRORS THE CARRY ON 

	 // place uploaded thumbnail images into variables for db 
	 $imageOne=$nameOne; 



	 // obtain the input form details into variables for the db 
	 $heading=$_POST['heading']; 
	 $news=nl2br( $_POST['news']);


	 $query = "INSERT INTO newspage (heading, news, registration_date, file1) VALUES ('$heading', '$news', NOW(), '$imageOne')";  
	 $result = @mysql_query ($query); // Run the query. 

		 if (mysql_affected_rows() == 0) { // A record was added so what next 
		 echo '<p>There seems to be an error.</p>'; 
		 echo '<p>ERROR: The insertion into the db</p>'; 
			exit; 
		} // end if

	 /****************** ALL WENT OK AND ENTERED INTO DATABASE DO SOMETHING ***************************/ 

   $imagesize='height="100" width="100"';
   $imagesizeuploaded='height="1100" width="100"';
   $alignright='right';


	// display the heading,news and image 
  echo '<div id="maincentredbig">Add News</div><div id="maincentredbig1"><p class="alertHd" align="center">Latest News has been added.</p>';	
  echo '<p class="alertHd" align="left">' .$heading .'</p>'; 
  echo '<p class="alertHd" align="left"><img '. $imagesize .' src="uploads/'.$imageOne .'" align="'.$alignright .'" />'.$news.'</p></div>';



 unset($_SESSION['FORM_SECRET']);



	}else {
		//Secret key missing
		echo 'Form data has already been processed!';

 } // end else 

} // end if empty error

} // end if post

} // end if form secret 

/****************** DISPLAY FORM WITH ERROR OR NOT ***************************/ 
?> 
<div id="maincentredbig">Add News</div> 
<br /> 
<div id="maincentredbigleft"><br /> 
<form action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" method="post"> 
<span>File 1 to upload : <input type="file" name="imageOne" /><br /></span><br /> <? echo $whatsediterror[3] ?><br /> 
<span>Heading: <input type="text" name="heading" <?php if (isset($_POST['heading'])) {?> value="<?php echo $_POST['heading'] ?>"<?php }?> size="53" /><span class="smallerrormessage">Required</span><br /><? echo $whatsediterror[1] ?></span><br /> 
<span>News: <textarea name="news" cols="52" rows="16"><?php if (isset($_POST['news'])) {?><?php echo $_POST['news'] ?><?php }?></textarea><span class="smallerrormessage">Required</span><br /><? echo $whatsediterror[2] ?></span><br /> 
<input type="Submit" onclick="this.disabled='disabled'" name="Submit" value="Submit"> 
<input type="hidden" name="form_secret" id="form_secret" value="<?php echo $_SESSION['FORM_SECRET'];?>" />
</form> 
</div>

onclick="this.disabled=true;  submit();"

 

 

I always use that on submit buttons but I'm sure it does the same thing. the beauty of using a program like php designer is you can select a bracket and see its counterpart. This bit of code is your problem. The two first ifs don't have closing brackets.

 

if(isset($_SESSION['FORM_SECRET'])) {
if(strcasecmp($form_secret,$_SESSION['FORM_SECRET'])===0) {

	if(isset($_POST['Submit'])) { ///// CHECK FORM  WHEN SUMBMITTED (if One) 

	   // check that the first image has been selected if not return error 
	if ($_FILES['imageOne']['name'] <> null) {	
	  $nameOne = resize("imageOne","thumbOne");//resize function and return name 
	   } else { 
	  $whatsediterror[3]='<p class="style1"> You Need to select an image(1).</p>'; 
		$error='false'; 
  } // end if

  • Author

Awesome! Thanks!

 

Maybe I need glasses!

 

Do you know if there are simalir programs for mac osx?

there are probably, yes but Mac OS X is not really well known for being a programming platform...

 

If you can't find anytihng, then you may be able to use Win Emulator.

Check out - JEdit - "The Programmers Text Editor.

Written in Java, so it runs on Mac OS X, OS/2, Unix, VMS and Windows."

http://www.jedit.org

 

To prevent duplicate entries, why not use a simple query to see if the data already exists in the database.

 

I’m not sure but I think, form hidden elements should be above the submit button.

no, there is no affect on the submitted data where the form elements are themselves. The only thing which changes is the structure of the returned POST array.

  • Author

Great program!

 

Also found:

 

TacoHTMLEdit

 

phpstudio

 

But your find is loads better! :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.