开发者

PHP Checkbox - return 'Yes' instead of 1 how?

开发者 https://www.devze.com 2023-03-23 11:37 出处:网络
I have a simple contact form with a checkbox, once valid and submitted I echo out the results of the form. I would like the checkbox item to return YES if checked and NO if blank.

I have a simple contact form with a checkbox, once valid and submitted I echo out the results of the form. I would like the checkbox item to return YES if checked and NO if blank.

Here is my snippet:

 $subject = "Website Contact Form Enquiry";

//If the form is submitted
if(isset($_POST['submit'])) {

    //Check to make sure that the name field is not empty
    if(trim($_POST['contactname']) == '') {
        $hasError = true;
    } else {
        $name = trim($_POST['contactname']);
    }

    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    //Check to make sure comments were entered
    if(trim($_POST['message']) == '') {
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['message']));
        } else {
            $comments = trim($_POST['message']);
        }
    }

    if(isset($_POST['tour']) &&
       $_POST['tour'] == 'yes')
    {
        $tour = true;
    }
    else
    {
        $tour = false;
    } 

    //If there is no error, send the email
    if(!isset($hasError)) {
        $emailTo = 'info@bgv.co.za'开发者_StackOverflow社区; //Put your own email address here
        $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;

    }
}

Heres the echo

        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
        <div id="sadhu">
        <p class="general_site">Name:</p><p class="general_siter"><strong><?php echo $name;?></strong></p>
        <p class="general_site">Email:</p><p class="general_siter"><strong><?php echo $email;?></strong></p>
        <p class="general_site">Message:</p><p class="general_siter"><strong><?php echo $comments;?></strong></p>
        <p class="general_site">Tour:</p><p class="general_siter"><strong><?php echo $tour;?></strong></p>
        </div>
        <?php } ?>


I think you could do (but i'm guessing because it's not clear what element is the checkbox):

if(isset($_POST['tour']) &&
   $_POST['tour'] == 'yes')
{
    $tour = 'Yes';
}
else
{
    $tour = 'No';
} 

In you case you echo 1 because you are echoing true whic is converted to 1 or false which is converted to 0


You don't control what value the client returns to you. If they're returning 0 or 1 instead of Yes or No, you can translate it with:

echo ($name == 1) ? "Yes" : "No

That's the ternary operator; it lets you return a different value based on whether a condition ($name == 0) is true.


In your form the checkbox should have a value

<input type="checkbox" name="tour" value="yes"/>

So in the PHP you should do

if(isset($_POST['tour']) &&
   $_POST['tour'] == 'yes')
{
    $tour = "yes";
}
else
{
    $tour = "no";
} 


One thing that wasn't mentioned so far is that if the box is not checked, you will not receive any value! And the actual value you just set in the html as:


Checkboxes are boolean. They either have a value, or they don't. What value they actually have is irrelevant. All you need to do is check for the presence or absence of a value, and act accordingly.

$tour = isset($_POST['tour'])? 'Yes': 'No';
0

精彩评论

暂无评论...
验证码 换一张
取 消