send checkbox value in PHP form

前端 未结 5 1518
别那么骄傲
别那么骄傲 2020-12-03 06:05

I have a question regarding a php form. I\'ve added a checkbox to the existing form, but not sure how to add it to the php. I would like it to send \"yes\" if the visitores

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 06:54

    If the checkbox is checked you will get a value for it in your $_POST array. If it isn't the element will be omitted from the array altogether.

    The easiest way to test it is like this:

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

    For your code, add it immediately below the other preprocessing:

    $name = $_POST['name']; 
    $email_address = $_POST['email']; 
    $message = $_POST['tel']; 
    
    if (isset($_POST['newsletter'])) {
      $newsletter = "yes";
    } else {
      $newsletter = "no";
    }
    

    You'll also need to change the HTML slightly. Change this line:

    i want to sign up for newsletter

    to this:

    i want to sign up   for newsletter
    ^^^ remove square brackets here.

提交回复
热议问题