2

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 :)

2
  • You'll soon figure it out if you use print_r($_POST) Commented Mar 26, 2012 at 16:01
  • @Pete: Better use var_dump instead of print_r. You want to see the types, as well. And since he is missing the checkbox's name-attribute, viewing $_POST won't help him. ;-) Commented Mar 26, 2012 at 16:12

6 Answers 6

2

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';
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

HTML:

<input type="checkbox" checked="checked" name="receive_updates" />

PHP:

$receive_updates = isset($_POST['receive_updates']) ? "YES" : "NO";

If the user checked your checkbox, your form will contain the value $_POST["receive_updates"] == "on". If the user did not check your checkbox, the request won't contain $_POST["receive_updates"] at all. So you can easily check with isset whether the checkbox has been checked by the user.

Comments

0

If the checkbox is not checked, the variable is simply not set. Also, you are missing the "name" attribute in the checkbox.

So: $checked = (isset($_POST['checkboxname']))? 'yes' : 'no'; would work. Aditionally, you can add a value='value' to the checkbox and add to the if statement && $_POST['checkboxname'] == 'value'.

Comments

0

Modify the HTML to:

<input id="checkbox" type="checkbox" checked="yes" name="updates">

Add this code to your PHP:

if(isset($_POST["updates"]) and $_POST["updates"]=="on") $body.="Updates: Yes <br>\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.