/    Sign up×
Community /Pin to ProfileBookmark

Single checkbox in form..

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….

[code=php]<?php

$to = “[email protected]”;

if (!isset($_POST[‘submit’])) {

showForm();

} else {

$error = 0;

if(empty($_POST[‘name’])) {
$error = 1;
$errstr[] = “Please enter a name”;
}

if(!preg_match(“/^(?:[wd]+.?)+@(?:(?:[wd]-?)+.)+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 />nn”;

showForm();

} else {
@mail($to, “Enquiry”,”On” . date(“r”) . “, ” . $_POST[‘name’] . ” from ” . $_POST[’email’] .
” sent the following message.nEnquiry – ” .
$_POST[‘reason’] . “nn” . “nn” . 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>&nbsp;</td>
<td class=”style4″>(Enter above code) &nbsp; <input type=”text” size=”20″ name=”imagetext” /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td class=”body” align=”left” colspan=”2″ ><input type=”submit” name=”submit” value=”Submit” />&nbsp;<input name=”reset” type=”reset” id=”reset” tabindex=”5″ value=”Reset” /></td>
</tr>
</table>
</form>
EOD;
}

?>[/code]

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…

Thanks in advance for any responses this may produce.

Bolty2uk

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@benifactorApr 08.2007 — well this is simple....

when your validating your form simply check the value...

[code=php]
if ($_POST['subscribe'] == "yes") {
$newsletter = yes;
}
else {
$newsletter = no;
}
[/code]


then when your sending your mail or whatever, just append something like

Newsletter subscribe: $newsletter
Copy linkTweet thisAlerts:
@bolty2ukauthorApr 08.2007 — Thanks benifactor..

That did the trick perfectly..

Much appreciated.

Thanks
×

Success!

Help @bolty2uk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...