php code for checkbox html form
Solution 1
Couple issues ... your checkbox does not have a name defined, therefore, you won't get it in the post. You'll want it to look more like:
<input id="checkbox" type="checkbox" name="updates" checked="checked" value="yes"/> i want to recive weekly updates from you <br />
If the checkbox is not checked, it will not be included in the post, so your php could be:
$updates = isset($_POST['updates']) ? 'Yes' : 'No';
Solution 2
OK here the list of mistakes i found:
1) You dont have name
attribute for checkbox
2) checked
attribute value is "checked". this is wrong => checked="yes"
3) You dont have any validation for checkbox input
Solution 3
On the form, modify your checkbox code to look like this:
<input id="weekly_updates" name="weekly_updates" type="checkbox" checked="yes" value="yes" />I want to recive weekly updates from you!<br />
In your PHP code, add this:
$weekly_updates = ( strtolower($_POST['weekly_updates']) === 'yes' ) ? 'Yes' : 'No';
If this PHP code looks confusing, take a quick look at the PHP documentation for the Ternary Operator found here: http://php.net/manual/en/language.operators.comparison.php
fafchook
Updated on June 14, 2022Comments
-
fafchook almost 2 years
this is my first time here, so please bear with me.. this is my html form :
<form method="post" action="contactformprocess.php"> <input id="name" name="name" type="text" value="" /> <br /> <input id="phone" name="phone" type="text" value=" /><br /> <input id=" email " name="email " type="text " value=" " /><br /> <textarea rows="5 " id="message " name="message " cols="20 "></textarea><br /> <input id="submit " name="submit " value="שלחו " type="submit " ><br /> <input id="checkbox " type="checkbox " checked="yes " value="i want to recive weekly updates from you "/> i want to recive weekly updates from you <br /> </form>
this is the php code im using :
<?php $emailSubject = '.....'; $webMaster = 'aa@aa'; $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; $message = $_POST['message']; $body = <<<EOD <br><hr><br> Subject: $option<br> Name: $name <br> Phone: $phone<br> Email: $email <br> Message: $message <br> EOD; $headers = "From: $email\r\n"; $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; $success = mail($webMaster, $emailSubject, $body, $headers); ?>
what I am missing is the php script that validates if the checkbox was unchecked(it's set to checked) or checked and sends the info together with the other fields. if checked send 'YES', if uncheck send 'NO' or nothing. thanks :)