Jump to content

Recommended Posts

I wanting help with a php contact form with attachment;. Seem to be working ok be I do have a couple of errors which I like to for fresh set of eyes to loo for me.

<?php
if($_POST["submit"]){

  //  Empty out errors array
  unset($errors);
  $errors = array();
  //  Validate fields
  if (empty($_POST['name'])){
		//Validate user Last Name
			$errors[] = "You have not entered in your last name.";
		} else{
			$names =$_POST['name'];
		}

	// Validate sending email
	if (empty($_POST['email'])){
			$errors[] = "please enter a validate eMail Address.";
		}else if(strlen($_POST['email']) > 350){
			$errors[] = "Your eMail maybe to long. Please review and correct.";
		}else if (filter_var($_POST['email'] , FILTER_VALIDATE_EMAIL) === false){
			$errors[] = "Please provide your right email address.";
		}else {
			$email = $_POST['email'];
		}

  if (empty($_POST['looking'])){
		//Validate user Last Name
			$errors[] = "You have not entered in your last name.";
		} else{
			$comments =$_POST['looking'];
		}

}
if (count($errors)==0) {
   echo "Your form entries have errors:<br />";
   foreach($errors as $key => $error) {
      echo $error . "<br />";
      }
 } else {
	$myemail = 'root@localhost';
	$to = $myemail;
	$subject = "Your Lady of interest name: $names ";	
	$names =$_POST['name'];			
	$email = $_POST['email'];
	$comments =$_POST['looking'];

$message = "$names $email $comments";

	$att = $_FILES['att'];
	$att_path = $_FILES['att']['tmp_name'];
	$att_name = $_FILES['att']['name'];
	$att_size = $_FILES['att']['size'];
	$att_type = $_FILES['att']['type'];

	#open, read then close the file
	$fp = fopen( $att_path, "rb");
	$file = fread( $fp, $att_size );
	fclose( $fp );

	#create a boundary string
	$num = md5(time());
	$str = "==Multipart_Boundary_x{$num}x";

	#encode the data for safe transit 
	#and intersperse 76-character chunks with \r\n
	$file = chunk_split(base64_encode($file));

	#define header
	$hdr  = "www.deanross.com.au\n";
	$hdr .= "MIME-Version: 1.0\n";
	$hdr .= "Content-Type: multipart/mixed;\n ";
	$hdr .= "boundary=\"{$str}\"";

	#define message
	$msg = "This is a multi-part message in MIME format\n\n";
	$msg .= "--{$str}\n";
	$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
	$msg .= "Content-Transfer-Encoding: 16bit\n\n";
	$msg .= "$message\n\n";
	$msg .= "--{$str}\n";

	#define the non-text attachment
	$msg .= "Content-Type: {$att_type};\n";
	$msg .= "name=\"{$att_name}\"\n";
	$msg .= "Content-Disposition: attachment;\n";
	$msg .= "filename=\"{$att_name}\"\n";
	$msg .= "Content-Transfer-Encoding: base64\n\n";
	$msg .= "$file\n\n";
	$msg .= "--{$str}";
	
	$ok = mail( $to, $subject,  $msg, $hdr);
//	if ($ok ==true){
//		header('Location: sends.php');
//	}else{
 //   	 header('Location: conactPage.php');
//	}
}
?>











<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>

	<?php
                If(empty($errors)=== false){?>
<table class="errorT" ><tr >
<td><fieldset><legend>You are missing some fields</legend><div class="errorIndent"><ol class="errorRed">
       
                 <?php
                            foreach ($errors as $error){
                                echo "<li>", $error," <font >Please fix</font></li>";
								
                            }
                        ?>
                        </ol>Click to Return to contacts page to fill in <font >  required</font> missing fields:<INPUT TYPE="button" VALUE="Fix missing fields" onClick="history.go(-1);">  	</div></fieldset></td>
</tr>
</table>
                        <?php
                        }else {
                            if(isset(  $to, $subject,$msg, $hdr)) {
							$ok; 
                        
                            }
                }
            ?>


<form method="post" name="contactform" action=""  enctype="multipart/form-data"> 
<input name="name" id="name" size="16" maxlength="16"  placeholder="Type in first Name" /><br>
<input name="email" id="email" type="text" size="36"  placeholder="Type in your Email Adderess"n/><br>
<textarea rows="5" cols="70" id="looking" name="looking" placeholder="test purpores"></textarea><br>
<label for='photo'> Have a nice photograph? I would love to see it</label><label for='photo' >:</label>
<input type="file" name="att" value="  "/><br>
<input type="submit" value="Send Email"  name="submit" > <input type="reset" value="Reset Form"> <input name="send" type="hidden" value="true" /> 



</form>
</body>
</html>

if any one has or nows of a better and simple contact form with attachment who can help.

Link to comment
https://forums.phpfreaks.com/topic/298413-help-with-php-conact-form/
Share on other sites

Sorry did forget the errors. they are as follows

 

Notice: Undefined index: submit in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 2

Notice: Undefined variable: errors in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 34
Your form entries have errors:

Notice: Undefined variable: errors in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 36

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 36

Those errors are pretty descriptive.

 

Notice: Undefined index: submit in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 2

You're trying to use an array index that doesn't exist. It doesn't exist yet if you haven't submitted your form. You should either do isset($_POST['submit']) or just check for a non-empty $_POST array, !empty($_POST)

 

Notice: Undefined variable: errors in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 34

You have not defined $errors in this scope. You should initialize $errors at the top of your script so that it's available everywhere. As an aside, you don't need to be doing unset($errors) - it cannot persist between page reloads.

 

Notice: Undefined variable: errors in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 36

Same as above.

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\xenxarra\_php\graphicArt\testmail.php on line 36

Since $errors isn't defined, it therefore is not an array or iterator, so it can't be used with foreach(). In addition, you're checking that the array is empty and not that it is not empty. if (count($errors)==0) { should be if (count($errors) > 0) { or, simply, if (!empty($errors)) {
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.