Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP Contact form with attachment and validation
Old 09-02-2008, 07:46 AM PHP Contact form with attachment and validation
mrmark's Avatar
Junior Talker

Posts: 2
Name: Mark de Jong
Location: Beijing, China
Trades: 0
For a website I'm making I'm putting together a form script out of PHP i pulled from the internet some time ago, but I can't seem to get some things working.

At the moment the form submits the information even if nothing is entered. I wish to have a validation script running through it before it's being sent out. And the form can only be sent when an actual file has been selected for upload and sending.

right now i have this script:
Code:
<!-- on top of the page -->
<?php
// Your e-mail adress:
$mailto = "adressee@website.com";

# Maximum size of attachment in bytes:
$max_attach_size = 500000;

?>
<!-- END on top of the page -->

<!-- START PHP form -->
<?php
/*if (empty($_POST['form_submitted']))
 {
  ?><p>Please fill out the form:</p>
<?php
 }
*/
 if (isset($_POST["form_submitted"]))
 {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $text = $_POST['text'];

  unset($errors);
  if ($email != "" and !preg_match("/^[^@]+@.+\.\D{2,5}$/", $email)) $errors[] = "E-mail address seems to be incorrect";
  if ($name != "") $errors[] = "Please enter your name";
  if ($_FILES['attachm']['size'] > $max_attach_size) $errors[] = "Attachment file size is too big(".number_format($_FILES['attachm']['size']/1000,0,",","")." KB) - maximum size: ".number_format($max_attach_size/1000,0,",","")." KB";

  if (empty($errors))
   {
    $text = stripslashes($text);
    if ($name != "") $mail_name=$name; else $mail_name="Unknown";
    if ($email != "") $mail_email = $email; else $mail_email = "mark.dejong@me.com";
    $ip = $_SERVER["REMOTE_ADDR"];

    // if attachment, MIME-Mail:
    if (isset($_FILES['attachm']['name']) && trim($_FILES['attachm']['name']) != "")
     {
      // read and encode file:
      $datei_content = fread(fopen($_FILES['attachm']['tmp_name'],"r"),filesize($_FILES['attachm']['tmp_name']));
      $datei_content = chunk_split(base64_encode($datei_content),76,"\n");
      // Boundary:
      $boundary = md5(uniqid(rand()));
      // Mail-Header:
      $mail_header = "From: ".$mail_name." <".$mail_email.">\n";
      $mail_header .= "X-Sender-IP: ".$ip."\n";
      $mail_header .= "MIME-Version: 1.0\n";
      $mail_header .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\n";
      $mail_header .= "This is a multi-part message in MIME format.\n";
      // Mail-Text:
      $mail_header .= "--".$boundary;
      $mail_header .= "\nContent-Type: text/plain";
      $mail_header .= "\nContent-Transfer-Encoding: 8bit";
      $mail_header .= "\n\n".$text;
      // Attachment:
      $mail_header .= "\n--".$boundary;
      $mail_header .= "\nContent-Type: ".$_FILES['attachm']['type']."; name=\"".$_FILES['attachm']['name']."\"";
      $mail_header .= "\nContent-Transfer-Encoding: base64";
      $mail_header .= "\nContent-Disposition: attachment; filename=\"".$_FILES['attachm']['name']."\"";
      $mail_header .= "\n\n".$datei_content;
      // End:
      $mail_header .= "\n--".$boundary."--";
      // Sende E-Mail und gebe Fehler bzw. Bestaetigung aus
      if (@mail($mailto,$mail_subject,"",$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
     }
    // no attachment, normal E-mail:
    else
     {
      $mail_header = "From: ".$mail_name." <".$mail_email.">\n";
      $mail_header .= "X-Sender-IP: $ip\n";
      $mail_header .= "Content-Type: text/plain";
      if (@mail($mailto,$mail_subject,$text,$mail_header)) $sent = true; else $errors[] = "no connection to the mailserver - please try again later";
     }

    // copy to sender:
    if (isset($sent) && isset($email) && $email != "" && isset($_POST['copy']))
     {
      if (isset($_FILES['attachm']['name']) && trim($_FILES['attachm']['name']) != "") $copy_mail_text = "Copy of the e-mail:\n\n".$text."\n\nAttachment: ".$_FILES['attachm']['name']; else $copy_mail_text = "Copy of the e-mail:\n\n".$text;
      $header= "From: ".$mailto."\n";
      $header .= "X-Sender-IP: ".$ip."\n";
      $header .= "Content-Type: text/plain";
      @mail($email, $mail_subject, $copy_mail_text, $header);
     }
   }
 }

if (empty($sent))
 {
  if(isset($errors))
   {
    ?><p>Error:</p><ul>
    <?php foreach($errors as $f) { ?><li>
    <?php echo $f; ?></li><?php } ?></ul>
<br /><?php
   }

  ?><form name='myform' method="post" action="" enctype="multipart/form-data"><div>
  <table cellpadding="2" cellspacing="0">
	  <tr>
		<td class="style66" width="100">Name:</td>
		<td class="style66"><input name="name" value="<?php if (isset($name)) echo htmlentities(stripslashes($name)); else echo ""; ?>" size="25" /></td>
	  </tr>
	  <tr>
		<td class="style66">E-mail:</td>
		<td class="style66"><input name="email" value="<?php if (isset($email)) echo htmlentities(stripslashes($email)); else echo ""; ?>" size="25" /></td>
	  </tr>
	  <tr>
		<td class="style66">Message:<br>(optional)</td>
		<td class="style66"><textarea name="text" cols="25" rows="7"><?php if (isset($text)) echo htmlentities(stripslashes($text)); else echo ""; ?></textarea></td>
	  </tr>
	  <tr>
		<td class="style66">CV file:</td>
		<td class="style66"><input type="file" name="attachm" value="<?php if (isset($_POST['attachm'])) echo htmlentities(stripslashes($_POST['attachm'])); else echo ""; ?>" size="16" /></td>
	  </tr>
	  <tr>
	  	<td></td>
		<td class="style66" colspan="2"><input type="checkbox" name="copy" value="true" />Send a copy to yourself</td>
	  </tr>
	  <tr>
	  	<td></td>
	  	<td class="style66"><input type="submit" name="form_submitted" value="OK - Verzenden" /></td>
	  </tr>
  </table>
  </div></form>
<?php
 }
else
 {
  if (empty($email)) { ?>
<p class="style66">Thank you.<br />Your mail has been sent, but your address is missing so you won't be getting any reply from me!</p>
<?php }
  else { ?>
<p class="style66">Thank you very much for your submission.<br />Your message has been successfully sent and received.</p>
<?php }
 }
?>
<!-- END PHP form -->
Could somebody here help me out perfecting this script please? Since I'm just a designer I don't know much about writing PHP.
All help would be greatly appreciated.

To make everything even more clear I will describe what I want the form to be doing eventually:
The user must fill in a name, must fill in a valid e-mail address. The message is optional, but the file must be selected. When the user clicks the SEND button the form will be checked for validation errors and when it has errors it shows them in either a pop-up or in the page on top of the form.
When everything is filled out correctly and a file is selected the form will be send per e-mail to the given e-mail address.
mrmark is offline
Reply With Quote
View Public Profile Visit mrmark's homepage!
 
 
Register now for full access!
Old 09-02-2008, 08:14 AM Re: PHP Contact form with attachment and validation
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
You will have to disable the submit button and use an onclick event to call a javascript to validate before sending. It would be much simpler to validate at server, if you use ajax to do this it will be transparent to user.

<td class="style66"><input type="button" name="form_submitted" value="OK - Verzenden" onclick = "handler()"/></td>

Last edited by Sleeping Troll; 09-02-2008 at 08:17 AM..
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP Contact form with attachment and validation
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30157 seconds with 12 queries