Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Contact form-checkbox PHP coding doesnt work

New Here ,
Dec 29, 2010 Dec 29, 2010

I am a beginner to web page design so i would be grateful if you can explain to me what PHP coding i would need for 'Checkbox' and where to place it in the PHP coding below. I can submit the form to my email with all the headings and the filled in forms with checkbox Yes or No.

Only problem i have is that part of the check box info appear right at the top of the email message rather than the 'Name', 'Tel', 'Email' etc..Another half of the checkbox appear where it suppose to be wih double heading as shown below. I would also like to remove the <br> from the email message. Can you also tell me how to change the colour for heading so its clear.

I have marked the checkbox code in BLUE for both HTML and PHP coding below. Sorry for the long coding. Look forward to hearing from someone. Much appreciated.

Message delivered to the email address as received:

Rear ground extension: Yes

Two-storey rear extension: Yes
Two-storey side extension: No
Loft: Yes
New Build: Yes

Other: Yes

Name: Test<br>
Email: tesf@hj.cm<br>
Contact No: 87576as<br>
Address1: addy1<br>
Address2: addy2<br>
Town: town<br>
County: county<br>
Postcode: post<br>
Property Type: Cottage<br>
Property Area: None<br>
Message:<br>
test msg

My HTML coding for the form:

<div class="contactForm">
      <form id="form1" name="form1" method="post" action="test2.php">
        <fieldset>
          <legend><strong>Personal Information</strong></legend>
          <table width="690" border="0">
            <tr>
              <td width="340"><p>
            <label for="name" class="contactLable">Name:</label>
            <input name="name" type="text" id="name" tabindex="10" size="30" />
          </p></td>
              <td width="340"><p>
            <label for="contact_number" class="contactLable">Contact No:</label>
            <input name="contact_number" type="text" id="contact_number" tabindex="20" size="30" />
          </p></td>
            </tr>
          </table>
          <table width="690" border="0">
            <tr>
              <td><p>
            <span id="sprytextfield1">
            <label for="email" class="contactLable">Email:</label>
            <input name="email" type="text" id="email" tabindex="30" size="30" />
          <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span> </p></td>
            </tr>
          </table>
        </fieldset>
        <fieldset>
          <legend><strong>Project Information</strong></legend>
          <p><table width="690" border="0">
  <tr>
    <td width="340"><p>
            <label for="address1" class="contactLable">Address 1:</label>
            <input name="address1" type="text" id="address1" tabindex="40" size="30" />
          </p></td>
    <td width="340"> <p>
            <label for="address2" class="contactLable">Address 2:</label>
            <input name="address2" type="text" id="address2" tabindex="50" size="30" />
          </p></td>
  </tr>
  <tr>
    <td><p>
            <label for="town" class="contactLable">Town:</label>
            <input name="town" type="text" id="town" tabindex="60" size="30" />
          </p></td>
    <td><p>
            <label for="county" class="contactLable">County:</label>
            <input name="county" type="text" id="county" tabindex="70" size="30" />
          </p></td>
  </tr>
  <tr>
    <td><p>
            <label for="postcode" class="contactLable">Post Code:</label>
            <input name="postcode" type="text" id="postcode" tabindex="80" size="10" />
          </p></td>
    <td> </td>
  </tr>
  <tr>
    <td><p>
            <label for="propertyType" class="contactLable">Property Type:</label>
            <select name="propertyType" id="propertyType" tabindex="90">
              <option>Please select</option>
              <option>Detached</option>
              <option>Semi-Detached</option>
              <option>End of Terrace</option>
              <option>Mid-Terrace</option>
              <option>Flat</option>
              <option>Bungalow</option>
              <option>Cottage</option>
              <option>Other</option>
            </select>
          </p></td>
    <td><p>
            <label for="propertyArea" class="contactLable">Property Area:</label>
            <select name="propertyArea" id="propertyArea" tabindex="100">
              <option>Please select</option>
              <option>None</option>
              <option>Listed Building</option>
              <option>Conservation Area</option>
            </select>
          </p></td>
  </tr>
</table>
</p>
       </fieldset><fieldset><legend><strong>Project Type</strong></legend>
          <table width="690">
            <tr>
              <td width="338"><label>
                <input name="rearExt" type="checkbox" class="contactCheckbox" id="CheckboxGroup1_0" tabindex="110" value="rearExt"/>
              Rear ground extension</label></td>
              <td width="340"><input name="sideExt" type="checkbox" class="contactCheckbox" id="CheckboxGroup1_1" tabindex="120" value="sideExt"/>
Side ground extension</td>
            </tr>
            <tr>
              <td><input name="twoStoreyRear" type="checkbox" class="contactCheckbox" id="twoStoreyRear" tabindex="130" value="twoStoreyRear"/>
Two-storey rear extension</td>
              <td><input name="twoStoreySide" type="checkbox" class="contactCheckbox" id="twoStoreySide" tabindex="140" value="twoStoreySide"/>
Two-storey side extension</td>
            </tr>
            <tr>
              <td><input name="loft" type="checkbox" class="contactCheckbox" id="loft" tabindex="150" value="loft"/>
Loft conversion </td>
              <td><input name="newBuild" type="checkbox" class="contactCheckbox" id="newBuild" tabindex="160" value="newBuild"/>
New build </td>
            </tr>
            <tr>
              <td><input name="flat" type="checkbox" class="contactCheckbox" id="flat" tabindex="170" value="flat"/>
Flat conversion</td>
              <td><input name="other" type="checkbox" class="contactCheckbox" id="other" tabindex="180" value="other"/>
Other conversion</td>
            </tr>
          </table>

        </fieldset>
        <fieldset><legend><strong>Message</strong></legend>
          <p>
            <label for="message" class="contactLable">Your Message or Comments:</label>
            <textarea name="message" id="message" cols="45" rows="5" tabindex="190"></textarea>
          </p>
        </fieldset>
        <br />
        <input type="submit" name="Sumbit" id="Sumbit" value="Submit" tabindex="200" />
      </form>
    </div>

PHP Coding to send to my email:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="refresh" content="0;URL=thankyou.html">
<title>Email Form</title>
</head>

<body>
<?php
  $name=addslashes($_POST['name']);
  $contact_number=addslashes($_POST['contact_number']);
  $email=addslashes($_POST['email']);
 
  $address1=addslashes($_POST['address1']);
  $address2=addslashes($_POST['address2']);
  $town=addslashes($_POST['town']);
  $county=addslashes($_POST['county']);
  $postcode=addslashes($_POST['postcode']);
 
  $propertyType=addslashes($_POST['propertyType']);
  $propertyArea=addslashes($_POST['propertyArea']);
 
  $rearExt=($_POST['rearExt'])?"Rear ground extension: Yes":"Rear ground extension: No";
  $sideExt=($_POST['sideExt'])?"Side ground extension: Yes":"Side ground extension: No";
  $twoStoreyRear=($_POST['twoStoreyRear'])?"Two-storey rear extension: Yes":"Two-storey rear extension: No";
  $twoStoreySide=($_POST['twoStoreySide'])?"Two-storey side extension: Yes":"Two-storey side extension: No";
  $loft=($_POST['loft'])?"Loft: Yes":"Loft: No";
  $newBuild=($_POST['newBuild'])?"New Build: Yes":"New Build: No";
  $flat=($_POST['flat'])?"Flat: Yes":"Flat: No";
  $other=($_POST['other'])?"Other: Yes":"Other: No";
  $msg.="\n$rearExt\n$wsideExt\n$twoStoreyRear\n$twoStoreySide\n$loft\n$newBuild\n$wflat\n$other";

 

  $message=addslashes($_POST['message']);

// you can specify which email you want your contact form to be emailed to here

  $toemail = "email@hotmail.com";
  $subject = "From Website Test";

  $headers = "MIME-Version: 1.0\n"
            ."From: \"".$name."\" <".$email.">\n"
            ."Content-type: text/html; charset=iso-8859-1\n";

  $body = "Name: ".$name."<br>\n"
            ."Email: ".$email."<br>\n"
            ."Contact No: ".$contact_number."<br>\n"
           
            ."Address1: ".$address1."<br>\n"
            ."Address2: ".$address2."<br>\n"
            ."Town: ".$town."<br>\n"
            ."County: ".$county."<br>\n"
            ."Postcode: ".$postcode."<br>\n"
           
            ."Property Type: ".$propertyType."<br>\n"
            ."Property Area: ".$propertyArea."<br>\n"
           
            ."Rear ground extension: ".$rearExt."<br>\n"
            ."Side ground extension: ".$sideExt."<br>\n"
            ."Two-storey rear extension: ".$twoStoreyRear."<br>\n"
            ."Two-storey side extension: ".$twoStoreySide."<br>\n"
            ."Loft conversion: ".$loft."<br>\n"
            ."New build: ".$newBuild."<br>\n"
            ."Flat: ".$flat."<br>\n"
            ."Other: ".$other."<br>\n"

           
            ."Message:<br>\n"
            .$message;
                   
                   
  if (!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
  {
    echo "That is not a valid email address.  Please return to the"
           ." previous page and try again.";
    exit;
  }

    mail($toemail, $subject, $body, $msg, $headers);
    echo "Thanks for submitting your comments";
?>
</body>
</html>

TOPICS
Server side applications
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 30, 2010 Dec 30, 2010

This thread has been moved to the Dreamweaver Application Development forum, which deals with PHP and other server-side issues.

I don't know where you got your PHP script from, but it's poorly written. The reason you don't get any results from the checkboxes is because the script fails to add them to the body of the email message, and submits them to the mail() function in the argument that should contain the headers. That also explains why <br> appears in the email, because the HTML header is in

...
Translate
LEGEND ,
Dec 30, 2010 Dec 30, 2010

This thread has been moved to the Dreamweaver Application Development forum, which deals with PHP and other server-side issues.

I don't know where you got your PHP script from, but it's poorly written. The reason you don't get any results from the checkboxes is because the script fails to add them to the body of the email message, and submits them to the mail() function in the argument that should contain the headers. That also explains why <br> appears in the email, because the HTML header is in the wrong argument.

Remove the addslashes() from all the variable assignments. For example:

$name=addslashes($_POST['name']);

should be:

$name = $_POST['name'];

The code for getting checkbox results should look like this (inserting "isset"):

$rearExt = isset($_POST['rearExt'])?"Rear ground extension: Yes":"Rear ground extension: No";

Change the following line:

$msg.="\n$rearExt\n$wsideExt\n$twoStoreyRear\n$twoStoreySide\n$loft\n $newBuild\n$wflat\n$other";

to this:

$msg = "$rearExt<br>\r\n$wsideExt<br>\r\n$twoStoreyRear<br>\r\n
        $twoStoreySide<br>\r\n$loft<br>\r\n$newBuild<br>\r\n$wflat<br>\r\n";

The headers should look like this:

$headers = "MIME-Version: 1.0\r\n
            From: $name <" . $email . ">\r\n
            Content-type: text/html; charset=iso-8859-1";

Each line in the body of the message should be followed by a carriage return and newline character, not just a newline character. Also, because you're using double quotes, you don't need to keep opening and closing them. The variables can be enclosed within the quotes. The first few lines should look like this:

$body = "Name: $name<br>\r\n
        Email: $email<br>\r\n
        Contact No: $contact_number<br>\r\n

The final closing quotes come at the end of the $body declaration, which should include the values from your checkboxes like this:

        Other: $other<br>\r\n

        $msg
        Message:<br>\r\n
        $message";

The line that sends your email should look like this:

mail($toemail, $subject, $body, $headers);

There are other improvements that could be made to the script, but those I have listed should solve your problems.

If you're interested in learning how to use PHP correctly, you might like to take a look at my book, PHP Solutions, 2nd Edition. Among other things, it shows you how to create a much more user-friendly email form that checks for required fields, and preserves the user's input if any mistakes are detected.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 30, 2010 Dec 30, 2010

Dear David

Thank you so much for the clear explanation. I have been using Lynda.com Dreamweaver CS5 tutorial. I found the coding for checkbox through search and added to the coding from Lynda.com tutorial.

I made few changes to the coding by removing:

$msg = "$rearExt<br>\r\n$sideExt<br>\r\n$twoStoreyRear<br>\r\n
          $twoStoreySide<br>\r\n$loft<br>\r\n$newBuild<br>\r\n$flat<br>\r\n$other\r\n";

as the email message shows the results from the checkbox twice.

I also removed the <br> from:

$body = "Name: $name<br>\r\n
          Email: $email<br>\r\n
          Contact No: $contact_number<br>\r\n

....

....

         Other: $other\r\n
         
          Message:\r\n
            $message";

I have add the result and the PHP coding you have helped to fixed for anyone needing a working PHP code containing Text Fields, Drop Down List, Checkbox and Text Area. Many thanks for all your help. If you have a coding to change the heading text colour I very much appericate it.

Message sent to email:

          From: Name test <email@test.com>

             Content-type: text/html; charset=iso-8859-1

Name: Name test

          Email: email@test.com

                  Contact No: 0123456789


                  Address1: addy1

                  Address2: addy2

                  Town: T

                  County: C

                  Postcode: P/C


                  Property Type: End of Terrace

                  Property Area: Listed Building



                  Rear ground extension: Rear ground extension: No

                 Side ground extension: Side ground extension: No

                 Two-storey rear extension: Two-storey rear extension: Yes

                 Two-storey side extension: Two-storey side extension: No

                 Loft conversion: Loft conversion: No

                 New build: New Build: No

                 Flat: Flat: Yes

                 Other: Other: No

         Message:

           Test message

PHP coding for customer contact page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="refresh" content="0;URL=thankyou.html">
<title>Email Form</title>
</head>

<body>
<?php
  $name=$_POST['name'];
  $contact_number=$_POST['contact_number'];
  $email=$_POST['email'];
 
  $address1=$_POST['address1'];
  $address2=$_POST['address2'];
  $town=$_POST['town'];
  $county=$_POST['county'];
  $postcode=$_POST['postcode'];
 
  $propertyType=$_POST['propertyType'];
  $propertyArea=$_POST['propertyArea'];
 
  $rearExt= isset($_POST['rearExt'])?"Rear ground extension: Yes":"Rear ground extension: No";
  $sideExt= isset($_POST['sideExt'])?"Side ground extension: Yes":"Side ground extension: No";
  $twoStoreyRear= isset($_POST['twoStoreyRear'])?"Two-storey rear extension: Yes":"Two-storey rear extension: No";
  $twoStoreySide= isset($_POST['twoStoreySide'])?"Two-storey side extension: Yes":"Two-storey side extension: No";
  $loft= isset($_POST['loft'])?"Loft conversion: Yes":"Loft conversion: No";
  $newBuild= isset($_POST['newBuild'])?"New Build: Yes":"New Build: No";
  $flat= isset($_POST['flat'])?"Flat: Yes":"Flat: No";
  $other= isset($_POST['other'])?"Other: Yes":"Other: No";
 
  $message=$_POST['message'];

// you can specify which email you want your contact form to be emailed to here

  $toemail = "youremail@here.com";
  $subject = "From Website Test";

  $headers = "MIME-Version: 1.0\r\n
             From: $name <".$email.">\r\n
             Content-type: text/html; charset=iso-8859-1";

  $body = "Name: $name\r\n
          Email: $email\r\n
          Contact No: $contact_number\r\n
           
          Address1: $address1\r\n
          Address2: $address2\r\n
          Town: $town\r\n
          County: $county\r\n
          Postcode: $postcode\r\n
           
          Property Type: $propertyType\r\n
          Property Area: $propertyArea\r\n
         
          Rear ground extension: $rearExt\r\n
          Side ground extension: $sideExt\r\n
          Two-storey rear extension: $twoStoreyRear\r\n
          Two-storey side extension: $twoStoreySide\r\n
          Loft conversion: $loft\r\n
          New build: $newBuild\r\n
          Flat: $flat\r\n
          Other: $other\r\n
         
          Message:\r\n
            $message";
                   
                   
  if (!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
  {
    echo "That is not a valid email address.  Please return to the"
           ." previous page and try again.";
    exit;
  }

    mail($toemail, $subject, $body, $headers);
    echo "Thanks for submitting your message";
?>
</body>
</html>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 30, 2010 Dec 30, 2010

i2008 wrote:

Message sent to email:

          From: Name test <email@test.com>

             Content-type: text/html; charset=iso-8859-1

Name: Name test

The Content-type header shouldn't be displayed in your message.

I don't use HTML email, but looking at the PHP documentation for mail(), it's clear that you need to put the Content-type immediately after the MIME type. Fix your headers like this:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

$headers .= "From: $name <".$email.">\r\n";

That's why you needed to take out the <br>, because the mail wasn't being sent as HTML.

If you have a coding to change the heading text colour I very much appericate it.

You don't have a heading in there. But if you're using HTML email, you add an inline style to the HTML tags, for example:

<h1 style="color:red">$name</h1>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 31, 2010 Dec 31, 2010
LATEST

Dear David

Thank you for all your help. Much appreciated. The email work. I will try the colour coding later. Thank you once again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines