duanping5306 2018-05-24 10:40
浏览 72
已采纳

使用php检查复选框的解决方案。 认为我必须使用一个值

How should I define the if isset? If I write it as follows, the php does not perceive the click.

`if(!isset($_POST['checkbox1']) || !isset($_POST['checkbox2']) || !isset($_POST['checkbox'])   )
    {
        echo json_encode(array('info' => 'error', 'msg' => 'Füllen Sie bitte alle Felder aus.'));
    }`this?

The checkboxes query works offline, but not online. I think I have to work with a value. It should not be a selection, but all checkboxes must be selected to be able to send.

I can not find the right solution in PHP. The checkboxes are checked but not processed.

Can somebody help me with the php-code?

<form id="contact-form" action="#">

  <div class="row contact-row">
    <div class="col-md-6 col-sm-6 col-xs-6 contact-name">
      <input type="text" id="firstname" name="firstname" placeholder="Vorname" required>
    </div>
    <div class="col-md-6 col-sm-6 col-xs-6 contact-name">
      <input type="text" id="lastname" name="lastname" placeholder="Nachname" required>
    </div>
  </div>

  <div class="row contact-row">
    <div class="col-md-6 col-sm-6 col-xs-6 contact-name">
      <input name="telefon" id="telefon" type="text" placeholder="Telefon" required>
    </div>
    <div class="col-md-6 col-sm-6 col-xs-6 contact-email">
      <input name="email" id="email" type="email" placeholder="E-mail" required>
    </div>
  </div>

  <input name="subject" id="subject" type="text" placeholder="Anfrage">
  <textarea name="message" id="message" placeholder="Nachricht" rows="5"></textarea>

  <div class="col-md-12 col-sm-12 col-xs-12 mb-30">
    <h6>Your Choose</h6>
    <ul class="checkboxes">
      <li>
        <input type="checkbox" class="input-checkbox" name="checkbox1" id="checkbox1" required>
        <label for="checkbox1">*&nbsp;&nbsp;Red</label>
      </li>
      <li>
        <input type="checkbox" class="input-checkbox" name="checkbox2" id="checkbox2" required>
        <label for="checkbox2">*&nbsp;&nbsp;White </label>
      </li>
      <li>
        <input type="checkbox" class="input-checkbox" name="checkbox3" id="checkbox3" required>
        <label for="checkbox3">*&nbsp;&nbsp;Blue</label>
      </li>
    </ul>
  </div>

  <input type="submit" class="btn btn-lg btn-color btn-submit" value="Nachricht senden" id="submit-message">
  <div id="msg" class="message"></div>
</form>
<?php
if ($_POST) {

    $to = "abcd.de"; // email here
    $subject = 'Kontaktformular '; // Subject message here

}

//Send mail function
function send_mail($to, $subject, $message, $headers)
{
    if (@mail($to, $subject, $message, $headers)) {
        echo json_encode(array('info' => 'success', 'msg' => "Message send. "));
    } else {
        echo json_encode(array('info' => 'error', 'msg' => "Message not send."));
    }
}

//Check if $_POST vars are set
if (!isset($_POST['firstname']) || !isset($_POST['lastname']) || !isset($_POST['telefon']) || !isset($_POST['email']) || !isset($_POST['message'])) {
    echo json_encode(array('info' => 'error', 'msg' => 'Fill out all fields.'));
}


//Sanitize input data, remove all illegal characters    
$firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$lastname = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);

$telefon = filter_var($_POST['telefon'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

$website = filter_var($_POST['website'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);


//Validation
if ($firstname == '') {
    echo json_encode(array('info' => 'error', 'msg' => "Firstname please."));
    exit();
}
if ($lastname == '') {
    echo json_encode(array('info' => 'error', 'msg' => "Lastname please."));
    exit();
}

if ($telefon == '') {
    echo json_encode(array('info' => 'error', 'msg' => "Telefon?"));
    exit();
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(array('info' => 'error', 'msg' => "Valid e-mail."));
    exit();
}

if ($message == '') {
    echo json_encode(array('info' => 'error', 'msg' => "Message?"));
    exit();
}

if ($checkbox1 == '') {
    echo json_encode(array('info' => 'error', 'msg' => "choose 1."));
    exit();
}
if ($checkbox2 == '') {
    echo json_encode(array('info' => 'error', 'msg' => "choose 2."));
    exit();
}
if ($checkbox3 == '') {
    echo json_encode(array('info' => 'error', 'msg' => "choose 3."));
    exit();
}


//Send Mail
$headers = 'From: ' . $email . '' . "
" .
    'Reply-To: ' . $email . '' . "
" .
    'X-Mailer: PHP/' . phpversion();

send_mail($to, $subject, $message . "

" . 'Vorname: ' . $firstname . "
" . 'Nachname: ' . $lastname . "
" . 'Telefon: ' . $telefon . "
" . 'Email: ' . $email, $headers);
?>
(function($) {
  "use strict";
  var submitContact = $('#submit-message'),
    message = $('#msg');

  submitContact.on('click', function(e) {
    e.preventDefault();

    var $this = $(this);

    $.ajax({
      type: "POST",
      url: 'mail-send.php',
      dataType: 'json',
      cache: false,
      data: $('#contact-form').serialize(),
      success: function(data) {

        if (data.info !== 'error') {
          $this.parents('form').find('input[type=text],input[type=email],input[type=checkbox],textarea,select').filter(':visible').val('');
          message.hide().removeClass('success').removeClass('error').addClass('success').html(data.msg).fadeIn('slow').delay(5000).fadeOut('slow');
        } else {
          message.hide().removeClass('success').removeClass('error').addClass('error').html(data.msg).fadeIn('slow').delay(5000).fadeOut('slow');
        }
      }
    });
  });

});
  • 写回答

2条回答 默认 最新

  • doushan6161 2018-05-24 10:49
    关注

    If the checkboxes are checked, $_POST will have it's values, otherwise it wont. To check if checkboxes are actually in checked state,

    $checkbox1 = isset($_POST['checkbox1']) ? true : false;
    $checkbox2 = isset($_POST['checkbox2']) ? true : false;
    $checkbox3 = isset($_POST['checkbox3']) ? true : false;
    

    alternatively, to get it's value, you can do

    $checkbox1 = isset($_POST['checkbox1']) ? $_POST['checkbox1'] : false;
    $checkbox2 = isset($_POST['checkbox2']) ? $_POST['checkbox2'] : false;
    $checkbox3 = isset($_POST['checkbox3']) ? $_POST['checkbox3'] : false;
    

    Below does same as above and more with PHP 7's null coalescing operator. The more side is, it also requires the value not to be null, to have it assigned to the variable.

    $checkbox1 = $_POST['checkbox1'] ?? false;
    $checkbox2 = $_POST['checkbox2'] ?? false;
    $checkbox3 = $_POST['checkbox3'] ?? false;
    

    and change your verifications to

    if($checkbox1 == false) {
        echo json_encode(array('info' => 'error', 'msg' => "choose 1."));
        exit();
    }
    if($checkbox2 == false) {
        echo json_encode(array('info' => 'error', 'msg' => "choose 2."));
        exit();
    }
    if($checkbox3 == false) {
        echo json_encode(array('info' => 'error', 'msg' => "choose 3."));
        exit();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教