Link to home
Create AccountLog in
Avatar of rincewind666
rincewind666

asked on

Amending php script ("all" fields to "no" fields required) 500 points

I have a php script where ALL fields are required.  I want to have it where NO fields are required.

I am giving the maximum 500 points in addition to my grateful thanks.
<?php
require_once( 'common.php' );
require_once( 'check-login.php' );
 
 
	# initialize the error fields
	$errfields = array(  );
if( isset( $_POST['submit'] ) ){ # checks if the user clicked the submit button
 
	# start of form validation
	$errors = array(); 
 
	if( $_POST['name'] == '' ){
		$errors[] = '&bull; Please Enter Name';
		$errfields[] = 'name';
	}
		
	if( $_POST['email'] == '' ){
		$errors[] = '&bull; Please Enter Email';	
		$errfields[] = 'email';							
	}
	if( $_POST['email'] != $_POST['reemail'] ){
		$errors[] = '&bull; Wrong Confirm Email';
		$errfields[] = 'email';
	}
	if( !validEmail( $_POST['email'] ) ){
		$errors[] = '&bull; Please Enter A Valid Email';
		$errfields[] = 'email';	
	}
		
	if( $_POST['nname'] == '' ){
		$errors[] = '&bull; Please Enter Name for Nomination';
		$errfields[] = 'nname';		
	}
	
	if( $_POST['country'] == '' ){
		$errors[] = '&bull; Please Select Country';	
		$errfields[] = 'country';			
	}
	
	if( $_POST['state'] == '' ){
		$errors[] = '&bull; Please Select State';
		$errfields[] = 'state';		
	}
	
	if( $_POST['color'] == '' ){
		$errors[] = '&bull; Please Select Color';
		$errfields[] = 'color';		
	}
	
	if( $_POST['keywords'] == '' ){
		$errors[] = '&bull; Please Enter Keywords';
		$errfields[] = 'keywords';		
	}
	
	if( $_POST['link'] == '' ){
		$errors[] = '&bull; Please Enter Link';
		$errfields[] = 'link';		
	}
	if( strstr( $_POST['link'], 'http://' ) == false ){
		$errors[] = '&bull; You need to enter your link as shown in the example';
		$errfields[] = 'link';		
	}	
	
	if( $_POST['description'] == '' ){
		$errors[] = '&bull; Please Enter Description';
		$errfields[] = 'description';		
	}
	
	if( strip_tags( trim( $_POST['html'] ) ) == '' ){
		$errors[] = '&bull; Please Enter HTML Code';
		$errfields[] = 'html';		
	}
	
	if( $_POST['conduct'] == '' ){
		$errors[] = '&bull; Please Select Conduct';
		$errfields[] = 'conduct';		
	}
	
	if( $_POST['options'] == '' ){
		$errors[] = '&bull; Please Select Options';
		$errfields[] = 'options';		
	}
	
	if( !is_uploaded_file( $_FILES['image']['tmp_name'] ) && $_POST['id'] == 0 ){
		$errors[] = '&bull; Please Upload Image';
		$errfields[] = 'image';		
	}
	
	if( !is_uploaded_file( $_FILES['zip']['tmp_name'] ) && $_POST['id'] == 0 ){
		$errors[] = '&bull; Please Upload Zip file';
		$errfields[] = 'zip';		
	}	
	
	# end of form validation
	
		
	if( count( $errors ) ){ # checks if there are errors in validation
	
		# display message to reupload the image file
		if( !in_array( 'image', $errfields ) ){
			$errors[] = '&bull; Due to the errors above, the uploaded image is not retained in the upload field, kindly reupload it again';
			$errfields[] = 'image';
		}
		
		# display message to reupload the zip file
		if( !in_array( 'zip', $errfields ) ){		
			$errors[] = '&bull; Due to the errors above, the uploaded zip file is not retained, kindly reupload it again';
			$errfields[] = 'zip';	
		}
		
		# set the validation error message
		$msg = implode( '<br />', $errors );
		$msgclass = 'errmsg';
	}
	else{ # if there is no error in validation code goes here
		
		# upload image and set the sql paramters
		if( is_uploaded_file( $_FILES['image']['tmp_name'] ) ){
			$ext = explode( '.', $_FILES['image']['name'] );
			$imgExt = $ext[ count( $ext )- 1];
			$imgName = md5( $_SERVER['REMOTE_ADDR'].time() ).'.'.$imgExt;
			move_uploaded_file( $_FILES['image']['tmp_name'], $settings['image_folder'].'/'.$imgName );
			$imgSQL = ",
					  image = '".$imgName."',
					  image_name = '".$_FILES['image']['name']."'";
		}
		
		# upload zip file and set the sql paramters
		if( is_uploaded_file( $_FILES['zip']['tmp_name'] ) ){		
			$ext = explode( '.', $_FILES['zip']['name'] );
			$zipExt = $ext[ count( $ext )- 1];
			$zipName = md5( $_SERVER['REMOTE_ADDR'].time() ).'.'.$zipExt;
			move_uploaded_file( $_FILES['zip']['tmp_name'], $settings['zip_folder'].'/'.$zipName );	
			$zipSQL = ",
					  zip = '".$zipName."',
					  zip_name = '".$_FILES['zip']['name']."'";			
		}
		
	
		//foreach( $_POST as $key=>$val )
			//$_POST[$key] = addslashes( trim( $val ) );
			
		
		if( $_POST['id'] == 0 ){ # checks if this is a new nomination
		
			# add nomination in database
			$query = "INSERT INTO nominations SET
					  name = '".$_POST['name']."',
					  email = '".$_POST['email']."',
					  nname = '".$_POST['nname']."',
					  country = '".$_POST['country']."',
					  state = '".$_POST['state']."',
					  color = '".$_POST['color']."',
					  keywords = '".$_POST['keywords']."',
					  link = '".$_POST['link']."',
					  description = '".$_POST['description']."',
					  html = '".$_POST['html']."',
					  conduct = '".$_POST['conduct']."',
					  options = '".$_POST['options']."',
					  image = '".$imgName."',
					  image_name = '".$_FILES['image']['name']."',
					  zip = '".$zipName."',
					  zip_name = '".$_FILES['zip']['name']."',
					  date = '".date( 'Y-m-d H:i:s' )."',
					  user_id = ".$_SESSION['userid'];
		}
		else{
			# edit the selected nomination
			$query = "UPDATE nominations SET
					  name = '".$_POST['name']."',
					  email = '".$_POST['email']."',
					  nname = '".$_POST['nname']."',
					  country = '".$_POST['country']."',
					  state = '".$_POST['state']."',
					  color = '".$_POST['color']."',
					  keywords = '".$_POST['keywords']."',
					  link = '".$_POST['link']."',
					  description = '".$_POST['description']."',
					  html = '".$_POST['html']."',
					  conduct = '".$_POST['conduct']."',
					  options = '".$_POST['options']."'
					  ".$imgSQL."
					  ".$zipSQL."
					  WHERE id = ".$_POST['id'];
			$editmode = 1;	
		}
		mysql_query( $query );
		
		
		if( $editmode ){ # check if this is an edit and set the success message
			$msg = 'Successfully Updated the item';
			$msgclass = 'msg';
		}
		else{
			# send confirmation email if this is a new nomination
			require_once('PHPMailer_v2.0.3/class.phpmailer.php');
			
			$mail = new PHPMailer(); 
			$body = $mail->getFile('nomination-confirmation-email.txt');
			
			foreach( $_POST as $key=>$val )
				$body = str_replace( '<'.$key.'>',$val, $body );
			
			$mail->From = $settings['from_email'];
			$mail->FromName = $settings['from_name'];
			
			$mail->Subject = $settings['nomination_subject'];
			$mail->AltBody = strip_tags( $body );
			
			$mail->MsgHTML( $body );
			$mail->AddAddress( $_POST['email'], $_POST['name'] );
			
			if( $settings['send_confirmation_to_admin'] )
				$mail->AddBCC( $settings['to_email'] , $settings['to_name']  );
			
			
			//$mail->AddAttachment("images/phpmailer.gif");
			
			$mail->Send();
			
			# redirect to success page
			header( 'Location: nomination-success.php' );
		}
	}		
}
 
if( isset( $_GET['e'] ) ){ # checks if script is in edit mode
 
	# get the selected nomination to edit
	$query = "SELECT *
			  FROM nominations
			  WHERE id = ".intval( $_GET['e'] );
	$result = mysql_query( $query );
	$row = mysql_fetch_assoc( $result );
	
	# load data to the post variables 
	foreach( $row as $key=>$val )
		$_POST[$key] = $val;
		
	$_POST['reemail'] = $row['email'];
	$_POST['image_path'] = $row['image'];
	$_POST['zip_path'] = $row['zip'];	
}
 
include('header.php');
?>
<form method="post" action="nomination.php" enctype="multipart/form-data">
<input name="id" type="hidden" value="<?=intval( $_POST['id'] )?>" />
<p class="<?=$msgclass?>"><?=$msg?></p>
<h3>your details</h3>
<!-- Your Name Field -->
<p class="<?=in_array( 'name', $errfields ) ? 'errorfield' : ''?>" >
	Your Name: <br />
	<input name="name" type="text" value="<?=$_POST['name']?>">
</p>
 
<!-- Email and Confirm Field -->
<p class="<?=in_array( 'email', $errfields ) ? 'errorfield' : ''?>">
	Your Email: <br />
	<input name="email" type="text" value="<?=$_POST['email']?>"><br />
	Confirm Your Email: <br />
	<input name="reemail" type="text" value="<?=$_POST['reemail']?>">	
</p>
 
<h3>Nomination:</h3>
 
<!-- Name Field -->
<p class="<?=in_array( 'nname', $errfields ) ? 'errorfield' : ''?>">
	Name: <br />
	<input name="nname" type="text" value="<?=$_POST['nname']?>">
</p>
 
<!-- Country Field -->
<p class="<?=in_array( 'country', $errfields ) ? 'errorfield' : ''?>">
	Country: <br />
<select name="country" id="country">
<option value="">--- Select ---</option>
<option>Afghanistan</option><option>&Aring;land Islands</option><option>Albania</option><option>Algeria</option><option>American Samoa</option><option>Andorra</option><option>Angola</option><option>Anguilla</option><option>Antarctica</option><option>Antigua and Barbuda</option><option>Argentina</option><option>Armenia</option><option>Aruba</option><option >Australia</option><option>Austria</option><option>Azerbaijan</option><option>Bahamas</option><option>Bahrain</option><option>Bangladesh</option><option>Barbados</option><option>Belarus</option><option>Belgium</option><option>Belize</option><option>Benin</option><option>Bermuda</option><option>Bhutan</option><option>Bolivia</option><option>Bosnia and Herzegovina</option><option>Botswana</option><option>Bouvet Island</option><option>Brazil</option><option>British Indian Ocean territory</option><option>Brunei Darussalam</option><option>Bulgaria</option><option>Burkina Faso</option><option>Burundi</option><option>Cambodia</option><option>Cameroon</option><option>Canada</option><option>Cape Verde</option><option>Cayman Islands</option><option>Central African Republic</option><option>Chad</option><option>Chile</option><option>China</option><option>Christmas Island</option><option>Cocos (Keeling) Islands</option><option>Colombia</option><option>Comoros</option><option>Congo</option><option>Congo, Democratic Republic</option><option>Cook Islands</option><option>Costa Rica</option><option>C&ocirc;te d'Ivoire (Ivory Coast)</option><option>Croatia (Hrvatska)</option><option>Cuba</option><option>Cyprus</option><option>Czech Republic</option><option>Denmark</option><option>Djibouti</option><option>Dominica</option><option>Dominican Republic</option><option>East Timor</option><option>Ecuador</option><option>Egypt</option><option>El Salvador</option><option>Equatorial Guinea</option><option>Eritrea</option><option>Estonia</option><option>Ethiopia</option><option>Falkland Islands</option><option>Faroe Islands</option><option>Fiji</option><option>Finland</option><option >France</option><option>French Guiana</option><option>French Polynesia</option><option>French Southern Territories</option><option>Gabon</option><option>Gambia</option><option>Georgia</option><option >Germany</option><option>Ghana</option><option>Gibraltar</option><option>Greece</option><option>Greenland</option><option>Grenada</option><option>Guadeloupe</option><option>Guam</option><option>Guatemala</option><option>Guinea</option><option>Guinea-Bissau</option><option>Guyana</option><option>Haiti</option><option>Heard and McDonald Islands</option><option>Honduras</option><option>Hong Kong</option><option>Hungary</option><option>Iceland</option><option>India</option><option>Indonesia</option><!-- copyright Felgall Pty Ltd --><option>Iran</option><option>Iraq</option><option>Ireland</option><option>Israel</option><option>Italy</option><option>Jamaica</option><option>Japan</option><option>Jordan</option><option>Kazakhstan</option><option>Kenya</option><option>Kiribati</option><option>Korea (north)</option><option>Korea (south)</option><option>Kuwait</option><option>Kyrgyzstan</option><option>Lao People's Democratic Republic</option><option>Latvia</option><option>Lebanon</option><option>Lesotho</option><option>Liberia</option><option>Libyan Arab Jamahiriya</option><option>Liechtenstein</option><option>Lithuania</option><option>Luxembourg</option><option>Macao</option><option>Macedonia, Former Yugoslav Republic Of</option><option>Madagascar</option><option>Malawi</option><option>Malaysia</option><option>Maldives</option><option>Mali</option><option>Malta</option><option>Marshall Islands</option><option>Martinique</option><option>Mauritania</option><option>Mauritius</option><option>Mayotte</option><option>Mexico</option><option>Micronesia</option><option>Moldova</option><option>Monaco</option><option>Mongolia</option>Montenegro<option></option><option>Montserrat</option><option>Morocco</option><option>Mozambique</option><option>Myanmar</option><option>Namibia</option><option>Nauru</option><option>Nepal</option><option>Netherlands</option><option>Netherlands Antilles</option><option>New Caledonia</option><option >New Zealand</option><option>Nicaragua</option><option>Niger</option><option>Nigeria</option><option>Niue</option><option>Norfolk Island</option><option>Northern Mariana Islands</option><option>Norway</option><option>Oman</option><option>Pakistan</option><option>Palau</option><option>Palestinian Territories</option><option>Panama</option><option>Papua New Guinea</option><option>Paraguay</option><option>Peru</option><option>Philippines</option><option>Pitcairn</option><option>Poland</option><option>Portugal</option><option>Puerto Rico</option><option>Qatar</option><option>R&eacute;union</option><option>Romania</option><option>Russian Federation</option><option>Rwanda</option><option>Saint Helena</option><option>Saint Kitts and Nevis</option><option>Saint Lucia</option><option>Saint Pierre and Miquelon</option><option>Saint Vincent and the Grenadines</option><option>Samoa</option><option>San Marino</option><option>Sao Tome and Principe</option><option>Saudi Arabia</option><option>Senegal</option><option>Serbia</option><option>Seychelles</option><option>Sierra Leone</option><option>Singapore</option><option>Slovakia</option><option>Slovenia</option><option>Solomon Islands</option><option>Somalia</option><option>South Africa</option><option>South Georgia and the South Sandwich Islands</option><option>Spain</option><option>Sri Lanka</option><option>Sudan</option><option>Suriname</option><option>Svalbard and Jan Mayen Islands</option><option>Swaziland</option><option>Sweden</option><option>Switzerland</option><option>Syria</option><option>Taiwan</option><option>Tajikistan</option><option>Tanzania</option><option>Thailand</option><option>Togo</option><option>Tokelau</option><option>Tonga</option><option>Trinidad and Tobago</option><option>Tunisia</option><option>Turkey</option><option>Turkmenistan</option><option>Turks and Caicos Islands</option><option>Tuvalu</option><option>Uganda</option><option>Ukraine</option><option>United Arab Emirates</option><option >United Kingdom</option><option>United States of America</option><option>Uruguay</option><option>Uzbekistan</option><option>Vanuatu</option><option>Vatican City</option><option>Venezuela</option><option>Vietnam</option><option>Virgin Islands (British)</option><option>Virgin Islands (US)</option><option>Wallis and Futuna Islands</option><option>Western Sahara</option><option>Yemen</option><option>Zaire</option><option>Zambia</option><option>Zimbabwe</option></select>	
</p>
 
<!-- State Field -->
<p class="<?=in_array( 'state', $errfields ) ? 'errorfield' : ''?>">State: <br />
<select name="state" id="state">
	<option value="">--- Select ---</option>
	<option value="OTHER">None U.S. State</option>	
	<option value="AL">Alabama</option>
	<option value="AK">Alaska</option>
	<option value="AZ">Arizona</option>
	<option value="AR">Arkansas</option>
	<option value="CA">California</option>
	<option value="CO">Colorado</option>
	<option value="CT">Connecticut</option>
	<option value="DE">Delaware</option>
	<option value="DC">District of Columbia</option>
	<option value="FL">Florida</option>
	<option value="GA">Georgia</option>
	<option value="HI">Hawaii</option>
	<option value="ID">Idaho</option>
	<option value="IL">Illinois</option>
	<option value="IN">Indiana</option>
	<option value="IA">Iowa</option>
	<option value="KS">Kansas</option>
	<option value="KY">Kentucky</option>
	<option value="LA">Louisiana</option>
	<option value="ME">Maine</option>
	<option value="MD">Maryland</option>
	<option value="MA">Massachusetts</option>
	<option value="MI">Michigan</option>
	<option value="MN">Minnesota</option>
	<option value="MS">Mississippi</option>
	<option value="MO">Missouri</option>
	<option value="MT">Montana</option>
	<option value="NE">Nebraska</option>
	<option value="NV">Nevada</option>
	<option value="NH">New Hampshire</option>
	<option value="NJ">New Jersey</option>
	<option value="NM">New Mexico</option>
	<option value="NY">New York</option>
	<option value="NC">North Carolina</option>
	<option value="ND">North Dakota</option>
	<option value="OH">Ohio</option>
	<option value="OK">Oklahoma</option>
	<option value="OR">Oregon</option>
	<option value="PA">Pennsylvania</option>
	<option value="RI">Rhode Island</option>
	<option value="SC">South Carolina</option>
	<option value="SD">South Dakota</option>
	<option value="TN">Tennessee</option>
	<option value="TX">Texas</option>
	<option value="UT">Utah</option>
	<option value="VT">Vermont</option>
	<option value="VA">Virginia</option>
	<option value="WA">Washington</option>
	<option value="WV">West Virginia</option>
	<option value="WI">Wisconsin</option>
	<option value="WY">Wyoming</option>
</select>
</p>
 
<!-- Color Field -->
<p class="<?=in_array( 'color', $errfields ) ? 'errorfield' : ''?>">
	Color: <br />
	<select name="color" id="color">
		<option value="">--- Select ---</option>	
		<option value="blue" style="background-color:blue">Blue</option>
		<option style="background-color:green" value="green">Green</option>
		<option style="background-color:red" value="red">Red</option>
		<option style="background-color:yellow" value="yellow">Yellow</option>						
	</select>
</p>
 
<script type="text/javascript">
	// set the dropdown boxes values 
	document.getElementById( 'country' ).value = '<?=$_POST['country']?>';
	document.getElementById( 'state' ).value = '<?=$_POST['state']?>';
	document.getElementById( 'color' ).value = '<?=$_POST['color']?>';		
</script>
 
<!-- Keywords Field -->
<p class="<?=in_array( 'keywords', $errfields ) ? 'errorfield' : ''?>">
	Keywords: <br />
	<input name="keywords" type="text" value="<?=$_POST['keywords']?>">
</p>
 
<!-- Link Field -->
<p class="<?=in_array( 'link', $errfields ) ? 'errorfield' : ''?>">
	Link: <small>(example http://yourwebsite.com, http://www.sitename.com )</small><br />
	<input name="link" type="text" value="<?=$_POST['link']?>">
</p>
 
<!-- Description Field -->
<p class="<?=in_array( 'description', $errfields ) ? 'errorfield' : ''?>">
	Description: <br />
	<textarea name="description" cols="30" rows="5"><?=$_POST['description']?></textarea>
</p>
 
<!-- Load the fckeditor -->
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<!-- HTML CODE field -->
<p class="<?=in_array( 'html', $errfields ) ? 'errorfield' : ''?>">
	HTML code: <br />
	<textarea name="html" id="html" cols="30" rows="5"><?=stripslashes( $_POST['html'] )?></textarea>
</p>
<script type="text/javascript">
	//apply fckeditor to the html code textarea
	var sBasePath = 'fckeditor/';
	var oFCKeditor = new FCKeditor( 'html' ) ;
	oFCKeditor.Width = 600;
	oFCKeditor.Height = 400;	
	oFCKeditor.BasePath	= sBasePath ;
	oFCKeditor.ReplaceTextarea() ;
</script>
<!-- Conduct field -->
<p class="<?=in_array( 'conduct', $errfields ) ? 'errorfield' : ''?>">
	Conduct: <br />
	<label><input name="conduct" type="radio" value="poor" <?=( $_POST['conduct'] == 'poor' ? 'checked="checked"' : '' )?>> Poor</label>
	<label><input name="conduct" type="radio" value="good" <?=( $_POST['conduct'] == 'good' ? 'checked="checked"' : '' )?>> Good</label>
	<label><input name="conduct" type="radio" value="excellent" <?=( $_POST['conduct'] == 'excellent' ? 'checked="checked"' : '' )?>> Excellent</label>		
</p>
 
<!-- Options field -->
<p class="<?=in_array( 'options', $errfields ) ? 'errorfield' : ''?>">
	Options: <br />
	<label><input name="options" type="radio" value="Option 1" <?=( $_POST['options'] == 'Option 1' ? 'checked="checked"' : '' )?> > Option 1</label>
	<label><input name="options" type="radio" value="Option 2" <?=( $_POST['options'] == 'Option 2' ? 'checked="checked"' : '' )?> > Option 2</label>
	<label><input name="options" type="radio" value="Option 3" <?=( $_POST['options'] == 'Option 3' ? 'checked="checked"' : '' )?>> Option 3</label>		
	<label><input name="options" type="radio" value="Option 4" <?=( $_POST['options'] == 'Option 4' ? 'checked="checked"' : '' )?>> Option 4</label>
	<label><input name="options" type="radio" value="Option 5" <?=( $_POST['options'] == 'Option 5' ? 'checked="checked"' : '' )?>> Option 5</label>			
</p>
 
<!--  Image field -->
<p class="<?=in_array( 'image', $errfields ) ? 'errorfield' : ''?>">
<?php
	if( $_POST['image_path'] != '' ){
	?>
	Current Image: <a href="<?=$settings['image_folder']?>/<?=$_POST['image_path']?>" target="_blank"><?=$_POST['image_name']?></a><br /> Replace
	<input name="image_path" type="hidden" value="<?=$_POST['image_path']?>" />
	<input name="image_name" type="hidden" value="<?=$_POST['image_name']?>" />
	<?php
	}
?>
	Image: <br />
	<input name="image" type="file">
</p>
 
<!--  Zip field -->
<p class="<?=in_array( 'zip', $errfields ) ? 'errorfield' : ''?>">
<?php
	if( $_POST['zip_path'] != '' ){
	?>
	Current Zip: <a href="<?=$settings['zip_folder']?>/<?=$_POST['zip_path']?>" target="_blank"><?=$_POST['zip_name']?></a><br /> Replace 
	<input name="zip_path" type="hidden" value="<?=$_POST['zip_path']?>" />
	<input name="zip_name" type="hidden" value="<?=$_POST['zip_name']?>" />
	<?php
	}
?>
	Zip file: <br />
	<input name="zip" type="file">
</p>
 
<!--  Download field -->
<p>
	Downloads: <br />
	<?php
	
	# retrieve files from download directory set in config.php
	$dirFiles = dirList( $settings['download_folder'] );
	foreach( $dirFiles as $dF ){
		$downloadLink = $settings['download_folder'].'/'.$dF;
	?>
		<a href="<?=$downloadLink?>"><?=$dF?></a><br />
	<?php
	}
	?>
</p>
 
<!-- Submit button -->
<p><input name="submit" value="Submit!" type="submit" /></p>
</form>
<?php 
include('footer.php');
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

When you say "no fields required" - please explain a little more.  The script cannot work if no fields are included, right?
ASKER CERTIFIED SOLUTION
Avatar of James Looney
James Looney
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of rincewind666
rincewind666

ASKER

Thanks for the tip Ray_Paseur, I didn't think of that.  Many thanks to both of you for your  help.