Question

OK so I have been trying to set up my own contact form and as this is my first time stepping outside of CMS setups like Wordpress and Joomla is causing me some difficulty as within these it is generally all done for you and does not require you to write any code yourself.

I have managed to set my form up using examples I have found but none of these included a checkbox option so I have had no way to view anothers work and figure it out from there.

  <form class="form-horizontal" method="post" action="mailertest.php">
<fieldset>

<!-- Form Name -->
<legend>Form Name</legend>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="cfname">Name</label>  
  <div class="col-md-4">
  <input id="cfname" name="cfname" placeholder="Enter name here..." class="form-control input-md" required type="text">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="cfnumber">Phone Number</label>  
  <div class="col-md-4">
  <input id="cfnumber" name="cfnumber" placeholder="Optional" class="form-control input-md" type="text">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="cfemail">eMail</label>  
  <div class="col-md-4">
  <input id="cfemail" name="cfemail" placeholder="Enter eMail here..." class="form-control input-md" required type="text">

  </div>
</div>

<!-- Textarea -->
<div class="form-group">
  <label class="col-md-4 control-label" for="cfmessagebox">Message</label>
  <div class="col-md-4">                     
    <textarea class="form-control" id="cfmessagebox" name="cfmessagebox"></textarea>
  </div>
</div>

<!-- Multiple Checkboxes -->
<div class="form-group">
  <label class="col-md-4 control-label" for="cfselection">What would you like to talk about today?</label>
  <div class="col-md-4">
  <div class="checkbox">
    <label for="cfselection-0">
      <input name="cfselection" id="cfselection-0" value="" type="checkbox">
      Website Design
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-1">
      <input name="cfselection" id="cfselection-1" value="" type="checkbox">
      Search Engines and Ranking
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-2">
      <input name="cfselection" id="cfselection-2" value="" type="checkbox">
      Social Media Marketing and Campaigns
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-3">
      <input name="cfselection" id="cfselection-3" value="" type="checkbox">
      Other
    </label>
    </div>
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" for="cfsubmit"></label>
  <div class="col-md-4">
    <button id="cfsubmit" name="cfsubmit" class="btn btn-success">Submit</button>
  </div>
</div>

</fieldset>
</form>

and this is the mailertest.php file

<?php
if(isset($_POST['cfsubmit'])) 
{

$message=
'Name:  '.$_POST['cfname'].'<br />
Phone:  '.$_POST['cfphone'].'<br />
Email:  '.$_POST['cfemail'].'<br />
Message:    '.$_POST['cfmessage'].'
';
    require "phpmailer/class.phpmailer.php"; //include phpmailer class
    require "phpmailer/class.smtp.php";

    // Instantiate Class  
    $mail = new PHPMailer();  

    // Set up SMTP  
    $mail->IsSMTP();                // Sets up a SMTP connection  
    $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
    $mail->Host = "xxx";  //Gmail SMTP server address
    $mail->Port = xxx;  //Gmail SMTP port

    // Authentication  
    $mail->Username   = "xxxx"; // Your full Gmail address
    $mail->Password   = "xxxx"; // Your Gmail password


    // Compose Message to Send
    $mail->SetFrom($_POST['cfemail'], $_POST['cfname']);
    $mail->AddReplyTo($_POST['cfemail'], $POST['cfname']);
    $mail->Subject = "New Customer Question via Contact Page";
    $mail->MsgHtml($message);

    // Send To  
    $mail->AddAddress("xxxxxx", "Recipient Name"); // Where to send it - Recipient
    $result = $mail->Send();        // Send!  
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';      
    unset($mail);

}
?>

I have been looking all around for some examples I can learn from but so far everything I have tried has not worked so if anyone is willing to offer their help/advice or even point me in the right direction I would be very grateful.

Thanks

Jordan

P.S I am guessing it would have something to do with value but have no clue where I can find some examples of this to figure out how to have their selection(value) sent to my email address.

Was it helpful?

Solution

You need to set values for the check-boxes first of all.

then add the checkbox line in the php accordingly.

$message=
'Name:   '.$_POST['cfname'].'<br />
Phone:   '.$_POST['cfphone'].'<br />
Email:   '.$_POST['cfemail'].'<br />
Message: '.$_POST['cfmessage'].'
Checkbox: '.$_POST['cfselection'].' // Add this line
';

Try that :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top