Jump to content

Single checkbox in form..


bolty2uk

Recommended Posts

Hello..

 

After quite a bit of reading up and playing about i just cant seem to find an answer that will work for something which should be very easy and straight forward.

 

Basically i have a self-validating php form that sends the users input to my email inbox.

 

What i need to do on the form is add a single check box (which i have already done) which the users will just tick if they wish to receive a newsletter..

 

Now my problem is i just cant get my head around the code i will have to use to send this details to me. All i need to receive is a simple "yes" in the email i get if they checked the box and nothing if they didn't...

 

I just don't know how to achieve this....

 

Here is the code i am working with....

 

<?php

$to = "admin@mymail.com";


if (!isset($_POST['submit'])) {

  showForm();

} else {

  $error = 0;


  if(empty($_POST['name'])) {
    $error = 1;
    $errstr[] = "Please enter a name";
  }

  if(!preg_match("/^(?:[\w\d]+\.?)+@(??:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) {
    $error = 1;
    $errstr[] = "Please enter a valid email address";
  }

  if(empty($_POST['message']) || preg_match("/^enter your message here$/i", $_POST['message'])) {
    $error = 1;
    $errstr[] = "Please enter a message";
  }

  if(empty($_POST['imagetext'])) {
    $error = 1;
    $errstr[] = "Please validate the security code";
  } else {
    include "securimage.php";
    $img = new securimage();
    $valid = $img->check($_POST['imagetext']);

    if(!$valid) {
      $error = 1;
      $errstr[] = "The security code you entered was incorrect";
    }
  }

  if ($error == 1) {
    echo "<center>\n<font style=\"color: #FF0000\">\n";
    foreach($errstr as $err) {
      echo "<li> " . $err . "</li>\n";
    }
    echo "</font>\n</center>\n<br />\n\n";

    showForm();

  } else {
    @mail($to, "Enquiry","On" . date("r") . ", " . $_POST['name'] . " from " . $_POST['email'] .
    " sent the following message.\nEnquiry -  " .
    $_POST['reason'] . "\n\n" . "\n\n" . stripslashes($_POST['message']), "From: " . $_POST['email']);

    echo "<font style=\"color: #003366\"><left>\n<b>Thank You.</b><br /><br /> Your contact form submission has now been successfully sent to us.<br /><br />One of our team will be responding to your enquiry very shortly.</font>";

  }

} //else submitted



function showForm()
{
  $_POST['message'] = @htmlspecialchars(@$_POST['message']);

  echo <<<EOD
<form method="POST">
<table cellpadding="5" cellspacing="1" width="90%" align="left">
  <tr>
    <td class="style3">Name:</td>
    <td align="left"><input type="text" name="name"  size="40" value="{$_POST['name']}" /></td>
  </tr>
  <tr>
    <td class="style3">Email:</td>
    <td align="left"><input type="text" name="email" size="40" value="{$_POST['email']}" /></td>
  </tr>
  <tr>
    <td class="style3">Enquiry:</td>
    <td align="left"><input type="text" name="reason" size="40" value="{$_POST['reason']}" /></td>
  </tr>
  <tr>
    <td class="style3">Message:</td>
    <td align="left"><textarea name="message" rows="5" cols="35">{$_POST['message']}</textarea></td>
  </tr>
    <tr>
    <td align="right" class="style3"><input name="subscribe" type="checkbox" value="{$_POST['subscribe']}" /></td>
    <td align="left" class="style2">Subscribe me to the  news letter.</td>
  </tr>
  <tr>
    <td align="center" colspan="2"><img src="securimage_show.php" alt="security code"></img></td>
  </tr>
  <tr>
    <td> </td>
    <td class="style4">(Enter above code)   <input type="text" size="20" name="imagetext" /></td>
  </tr>
  <tr>
    <td> </td>
    <td class="body" align="left" colspan="2" ><input type="submit" name="submit" value="Submit" /> <input name="reset" type="reset" id="reset" tabindex="5" value="Reset" /></td>
  </tr>
</table>
</form>
EOD;
}

?> 

 

I know this must be relatively straight forward but im afraid on this occasion i just cant seem to see the wood from the trees...

 

If anyone can offer any advice or know of a good tutorial it would be most appreciated...

 

Thanks in advance for any responses this may produce.

Link to comment
Share on other sites

Just make the checkbox checked value Yes.

 

Then in the script, do a simple if statement

 

if($_POST['checkboxname'] == 'Yes') {$newsletter = "Yes";} {else $newsletter = 'No';}

 

then put another bit in the mail function and you'll be flying.

Link to comment
Share on other sites

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.