Jump to content

Adding an photo upload to a contact form


spacepoet

Recommended Posts

Hello:

 

I wanted to learn how to add a file browse/save and send photo to a contact form.

 

Form works fine, but I am trying to add this so a user can upload and send a photo of his or her artwork along with the contact information.

 

Can someone tell me how this work? My other attempts failed so I'm trying to start with a clean form.

 

This is the code I currently have:

<?php

$error = NULL;
$myDate = NULL;
$FullName = NULL;
$Address = NULL;
$City  = NULL;
$State = NULL;
$Zip = NULL;
$Phone = NULL;
$Email = NULL;
$Website = NULL;
$Comments = NULL;

if(isset($_POST['submit'])) {

$myDate = $_POST['myDate'];
$FullName = $_POST['FullName'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$Website = $_POST['Website'];
$Comments = $_POST['Comments'];

if(empty($FullName)) {
   $error .= '<div style=\'margin-bottom: 6px;\'>- Enter your Name.</div>';
}


if(empty($Email) || !preg_match('~^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$~',$Email)) {
  $error .= '<div style=\'margin-bottom: 6px;\'>- Enter a valid Email.</div>';
}

if($error == NULL) {

$sql = sprintf("INSERT INTO myContactData(myDate,FullName,Address,City,State,Zip,Phone,Email,Website,Comments) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",

mysql_real_escape_string($myDate),
mysql_real_escape_string($FullName),
mysql_real_escape_string($Address),
mysql_real_escape_string($City),
mysql_real_escape_string($State),
mysql_real_escape_string($Zip),
mysql_real_escape_string($Phone),
mysql_real_escape_string($Email),
mysql_real_escape_string($Website),
mysql_real_escape_string($Comments));
                        
if(mysql_query($sql)) {
  
$error .= '<div style=\'margin-bottom: 6px;\'>Thank you for contacting us. We will reply to your inquiry shortly.<br /><br /></div>';
    
mail( "[email protected]", "Contact Request",

"Date Sent: $myDate\n Full Name: $FullName\n Address: $Address\n City: $City\n State: $State\n Zip: $Zip\n Phone: $Phone\n Email: $Email\n Website: $Website\n Comments: $Comments\n",
"From: $Email" );

unset($FullName); 
unset($Address);
unset($City);
unset($State);
unset($Zip);
unset($Phone);
unset($Email);
unset($Website);
unset($Comments);

  }
  else {
    $error .= 'There was an error in our Database, please Try again!';
  }
}
}


?>

			<form name="myform" action="" method="post">
			<input type="hidden" name="myDate" size="45" maxlength="50" value="<?php echo date("F j, Y"); ?>" />

				<div id="tableFormDiv">

					<fieldset><span class="floatLeftFormWidth"><span class="textErrorItalic">* - Required</span></span> <span class="floatFormLeft"> </span></fieldset>
					<?php echo '<span class="textError">' . $error . '</span>';?>
					<fieldset><span class="floatLeftFormWidth"><span class="textErrorItalic">*</span> Full Name:</span> <span class="floatFormLeft"><input type="text" name="FullName" size="45" maxlength="50" value="<?php echo $FullName; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Address:</span> <span class="floatFormLeft"><input type="text" name="Address" size="45" maxlength="50" value="<?php echo $Address; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">City:</span> <span class="floatFormLeft"><input type="text" name="City" size="45" maxlength="50" value="<?php echo $City; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">State:</span> <span class="floatFormLeft"><input type="text" name="State" size="45" maxlength="50" value="<?php echo $State; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Zip:</span> <span class="floatFormLeft"><input type="text" name="Zip" size="45" maxlength="50" value="<?php echo $Zip; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Phone:</span> <span class="floatFormLeft"><input type="text" name="Phone" size="45" maxlength="50" value="<?php echo $Phone; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth"><span class="textErrorItalic">*</span> Email:</span> <span class="floatFormLeft"><input type="text" name="Email" size="45" maxlength="50" value="<?php echo $Email; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Website:</span> <span class="floatFormLeft"><input type="text" name="Website" size="45" maxlength="50" value="<?php echo $Website; ?>" /></span></fieldset>
					<fieldset><span class="floatLeftFormWidth">Comments:</span> <span class="floatFormLeft"><textarea name="Comments" cols="40" rows="10"><?php echo $Comments; ?></textarea></span></fieldset>
				</div>	
					<input type="submit" name="submit" value="Submit" class="submitButton" /><br />
			</form>	

 

Thanks very much!

The upload handling section of the php.net documentation shows what you need for the <form> tag, for a form field, and for basic php code to access the uploaded file information - http://www.php.net/manual/en/features.file-upload.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.