Form problem. Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
savio_mf
Forum Newbie
Posts: 1
Joined: Sun Jun 18, 2006 8:30 pm

Form problem. Help

Post by savio_mf »

Hi everyone. I was just wondering if anyone could please help me. I am new to php and have written a quote/comment form. I dont know what I am doing wrong. There are three prblems I wish to tackle, which are as follows.

1. When the form is filled and sent, i get the email sent from 'nobody' (Nobody [nobody@ns5.sovdns.com]) in my email account. How can I make the persons name which he/she has entered in the form appear under the from section of my email inbox?

2. I need to validate the checkboxes so atleast one check box is clicked upon. Is there anyway of doing that?

3. For the fields marked with * , only the name and comment seems to be working.

Please can anyone help. Will be much appreciated. Cheers.

SMF

the code is below. Please copy and post in your editor.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Lesson 3</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style3 {font-size: 11px; font-family: Arial, Helvetica, sans-serif; color: #666666; }
.style10 {font-size: 10px; font-family: Arial, Helvetica, sans-serif; color: #666666; }
.style11 {font-size: 10px}
.style5 {	font-family: Arial, Helvetica, sans-serif;
	font-size: 11px;
	color: #FF6600;
}
.style12 {color: #FF6600}
.style13 {color: #666666}
.style17 {color: #FF6600; font-family: Arial, Helvetica, sans-serif; }
-->
</style>
</head>
<body>
<p>
  <?php
   if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>
  <img src="images/quote.gif" alt="quote" width="93" height="30"></p>
<p>  <span class="style5">Please complete the form below. * <span class="style13">Required fields</span> </span></p>
<form name="form1" method="post" action="<?=$me?>">
<table width="270" border="0" align="center" cellpadding="1" cellspacing="0">
      <tr>
        <td width="53" align="right" valign="middle" bordercolor="#F1F3F8"><div align="left"><span class="style3">Name:</span><span class="style17">*</span></div></td>
         <td width="220"><div align="left">
           <input name="Name" type="text" size="20">         
         </div></td>
      </tr>
      <tr>
        <td align="right" valign="middle"><div align="left"><span class="style3">Business Name:</span></div></td>
        <td><div align="left">
          <input name="Bname" type="text" size="20">
        </div></td>
      </tr>
      <tr>
        <td align="right" valign="middle"><div align="left"><span class="style3">Phone:</span><span class="style17">*</span></div></td>
        <td><div align="left">
          <input name="Phone" type="text" size="20">        
        </div></td>
      </tr>
      <tr>
        <td align="right" valign="middle"><div align="left"><span class="style3">E-mail:</span><span class="style17">*</span></div></td>
        <td><div align="left">
          <input name="Email" type="text" size="25">        
        </div></td>
      </tr>
      <tr>
        <td align="right" valign="middle"><div align="left"><span class="style3">Address:</span></div></td>
        <td><div align="left">
          <input name="Addr" type="text" size="25">        
        </div></td>
      </tr>
      <tr>
        <td height="10" colspan="2" align="right" valign="middle"><div align="left"><span class="style3">How did you hear about us?</span><span class="style17"> *</span> <span class="style3">
          <select name="hear" class="style3">
            <option value="" selected>Select one</option>
            <option value="Newspaper">Newspaper</option>
            <option value="Yellow Pages">Yellow Pages</option>
            <option value="Business card">Business card</option>
            <option value="Letter">Letter</option>
            <option value="Search engine">Search engine</option>
            <option value="Word of mouth">Word of mouth</option>
            <option value="Event">Event</option>
            <option value="Other">Other</option>
          </select>
        </span></div></td>
      </tr>
      <tr>
         <td height="10" colspan="2" align="right" valign="middle"><div align="left"><span class="style3">Project requirements:</span><span class="style17"> *</span> </div></td>
      </tr>
      <tr>
         <td colspan="2" align="center"><table width="275" border="0" align="left">
           <tr>
             <td colspan="2" align="left" valign="top"><span class="style10">
               <input name="webdesign" type="checkbox" 
            id="webdesign" value="website design">
             Website design</span></td>
             <td width="121" align="left" valign="middle"><span class="style10">
               <input name="flash" type="checkbox"
            id="flash" value="flash development">
               Flash development</span></td>
           </tr>
           <tr>
             <td width="68" align="left" valign="middle"><div align="left"><span class="style3">
               <input name="seo" type="checkbox"
            id="seo" value="SEO">
               <span class="style11">SEO</span></span></div></td>
             <td width="72" align="left" valign="middle"><span class="style3"><span class="style11"><span class="style10">
               <input name="bespoke" type="checkbox"
            id="bespoke" value="bespoke">
Bespoke </span></span></span></td>
             <td align="left" valign="middle"><span class="style3"><span class="style11"><span class="style10">
               <input name="redesign" type="checkbox"
            id="redesign" value="redesign site">
Redesign site</span></span></span></td>
           </tr>
           <tr>
             <td height="23" colspan="2" align="left" valign="middle"><span class="style10">
               <input name="ecomm" type="checkbox"
            id="ecomm" value="ecommerce">
E-commerce</span></td>
             <td align="left" valign="middle"><span class="style10">
               <input name="webmain" type="checkbox"
            id="webmain" value="web maintenance">
Web maintenancee</span></td>
           </tr>
         </table>         </td>
      </tr>
      <tr>
         <td align="right" valign="middle"><div align="left" class="style3">Comments:<span class="style12">*</span></div></td>
         <td><textarea name="MsgBody" cols="23" rows="1"></textarea></td>
      </tr>
      <tr>
         <td> </td>
         <td><input type="submit" name="Submit"
               value="Send">         </td>
      </tr>
  </table>
</form>
<?php
   } else {
      error_reporting(0);
      // initialize a array to 
      //hold any errors we encounter
      $errors = array();
      // test to see if the form was actually 
      // posted from our form
      $page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
      if (!ereg($page, $_SERVER['HTTP_REFERER']))
         $errors[] = "Invalid referer<br>\n";
      // check to see if a name was entered

      if (!$_POST['Name'])
         $errors[] = "Name is required";
		 
		 if (!$_POST['Phone'])
         $errors[] = "Phone number is required";
		 
		 if (!$_POST['Email'])
         $errors[] = "Email address is required";
		 
		  if (!$_POST['hear'])
         $errors[] = "Please tell us how you heard about us";
		 
				 
		 if (!$_POST['MsgBody'])
         $errors[] = "Please enter a comment in the comment box";
		 
      // if there are any errors, display them
      if (count($errors)>0) {
         foreach($errors as $err)
		     echo "<font face=\"Arial\" color=\"#FF6600\" size=\"2\">";
            echo "$err<br>\n";
         echo "<br>Please use your browsers Back button to fix.";
      } else {
         // no errors, so we build our message
        
		 $recipient = 'savio_mf@yahoo.com';
         $subject = "Quote form";
         $from = stripslashes($_POST['Name']);
         $msg = "Message sent by $from\n";
		 
		 $msg.="\nNAME:  ".stripslashes($_POST['Name'])."\n";
		 $msg.="\nBUSINESS NAME:  ".stripslashes($_POST['Bname'])."\n";
		 $msg.="\nPHONE:  ".stripslashes($_POST['Phone'])."\n";
		 $msg.="\nEMAIL:  ".stripslashes($_POST['Email'])."\n";
		 $msg.="\nADDRESS:  ".stripslashes($_POST['Addr'])."\n";
		 
		 
		 
		
		 
         $msg.="\nHOW DID YOU HEAR FROM US? : ".$_POST['hear'];
		 
		  $msg.="\n\nPROJECT REQUIREMENTS\n ";
         
         $webdesign=($_POST['webdesign'])?"Website design: Yes":"Website design: No";
         $flash=($_POST['flash'])?"Flash development: Yes":"Flash development: No";
         $seo=($_POST['seo'])?"SEO: Yes":"SEO: No";
		 $ecomm=($_POST['ecomm'])?"Ecommerce: Yes":"Ecommerce: No";
         $webmain=($_POST['webmain'])?"Web maintenance: Yes":"Web maintenance: No";
		 $bespoke=($_POST['bespoke'])?"Bespoke: Yes":"Bespoke: No";
         $redesign=($_POST['redesign'])?"Redesign site: Yes":"Redesign site: No";
         $msg.="\n$webdesign\n$flash\n$seo\n$ecomm\n$webmain\n$bespoke\n$redesign";
         $msg.="\n\nCOMMENTS:".stripslashes($_POST['MsgBody'])."\n";
         if (mail($recipient,$from,$subject,$msg)){
            echo "<font face=\"Arial\" color=\"#FF6600\" size=\"2\">";
            echo "<p>Thank you for your enquiry. We will contact you shortly</p>";
         } else
            echo "An unknown error occurred.";
      }
   }
?>
</body>
</html>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Form problem. Help

Post by RobertGonzalez »

savio_mf wrote:Hi everyone. I was just wondering if anyone could please help me. I am new to php and have written a quote/comment form. I dont know what I am doing wrong. There are three prblems I wish to tackle, which are as follows.

1. When the form is filled and sent, i get the email sent from 'nobody' (Nobody [nobody@ns5.sovdns.com]) in my email account. How can I make the persons name which he/she has entered in the form appear under the from section of my email inbox?

2. I need to validate the checkboxes so atleast one check box is clicked upon. Is there anyway of doing that?

3. For the fields marked with * , only the name and comment seems to be working.
1) First of all I would recommend reading the PHP manual page on the mail() function. There is a clean example of how to send an email and how to enter header information (like who the mail is from). Then it is a simple grabbing of the $_POST vars.

2) Instead of using if(!$_POST['field']) try using isset() and !empty(). This will eliminate undefined index warnings in your code if the fields are left blank by the user. They maybe you can turn error_reporting back on. As for checkboxes, you can check each checkbox for a value of 'on' as a cheap validation. If there are no checkboxes set to 'on' then you know they were unchecked when sent.

3. Try not using raw post data. Assign your post vars to internal vars ($email = $_POST['email'] for example) then use some validation on those. I mean real validation also, not just checking againt if the value is there, which by the way, may be causing you problems you are not aware because error reporting is off.
Post Reply