PHP复选框表单 - 显示值

时间:2018-04-06 13:20:13

标签: javascript php html

我的联系表单中有一个复选框,我似乎无法显示通过php发送的电子邮件中的值。我知道这可能是一个isset问题,但我无法修复它以显示用户是否单击了复选框。

形式:

<script>
function _(id){ return document.getElementById(id); }
function submitForm(){
    _("mybtn").disabled = true;
    _("status").innerHTML = 'please wait ...';
    var formdata = new FormData();
    formdata.append( "n", _("n").value );
    formdata.append( "b", _("b").value );
    formdata.append( "c", _("c").value );
    formdata.append( "n", _("n").value );
    formdata.append( "s", _("s").value );
    formdata.append( "d", _("d").value );
    formdata.append( "p", _("p").value );
    formdata.append( "checkbox1", _("checkbox1").value );
    formdata.append( "checkbox2", _("checkbox2").value );
    formdata.append( "checkbox3", _("checkbox3").value );
    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "example_parser.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                _("my_form").innerHTML = '<h2>Thanks '+_("n").value+', your message has been sent.</h2>';
            } else {
                _("status").innerHTML = ajax.responseText;
                _("mybtn").disabled = false;
            }
        }
    }
    ajax.send( formdata );
}
</script>
</head>
<body>
<form id="my_form" onsubmit="submitForm(); return false;">
  <p><input id="n" placeholder="First name" required></p>
  <p><input id="b" placeholder="Last name" required></p>
  <p><input id="c" placeholder="C Number"></p>
  <p><input id="n" placeholder="N Number"</p>
    <p><input id="d" placeholder="Date of birth"></p>
    <p><input id="p" placeholder="Postcode"></p>
    <label for="sex">I am </label>
    <select id="s" name="s">
      <option value="female">Female</option>
      <option value="male">Male</option>
      <option value="other">Other</option>
    </select>
    <label>
        <p>
        <br>
                <input type="checkbox" name="checkbox1" value="1" id="checkbox1"> 
              data1</label>
      <br>
      <label>
        <input type="checkbox" name="checkbox2" value="1" id="checkbox2">
        data2</label>
      <br>
      <label>
       <input type="checkbox" name="checkbox3" value="1" id="checkbox3">
        data3</label></p>
      <br>
  <p><input id="mybtn" type="submit" value="Submit Form"> <span id="status"></span></p>
</form>

example_parser.php:

<?php
if( isset($_POST['n']) && isset($_POST['b']) && isset($_POST['c']) && isset($_POST['n']) && isset($_POST['s']) || isset($_POST['d']) || isset($_POST['p']) && isset($_POST['checkbox1']) || isset($_POST['checkbox2']) || isset($_POST['checkbox3']) ){
    $n = $_POST['n']; // HINT: use preg_replace() to filter the data
    $b = $_POST['b'];
    $c = $_POST['c'];
    $n = $_POST['n'];
    $s = $_POST['s'];
    $s = $_POST['d'];
    $s = $_POST['p'];
    $z = $_POST['checkbox1'];
    $y = $_POST['checkbox2'];
    $x = $_POST['checkbox3'];
    $to = "";   
    $from = "";
    $subject = 'Please help';
    $message = '<b>First Name:</b> '.$n.' <b>Last Name</b> '.$b.' <b>C Number</b> '.$c.' <b>N Number</b> '.$n.' <b>Date of Birth</d> '.$d.' <b>Postcode</b> '.$p.' <b>Sex</b> '.$s.' <b>Option1</b> '.$z.' <b>Option2</b> '.$y.' <b>Option3</b> '.$x.' <br><b>End of message, thank you</b> '.$n.' <p>'.$b.'</p>';
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
}
?>

2 个答案:

答案 0 :(得分:0)

我记得没有选中复选框不会与POST一起发送。在您的PHP脚本更改

$z = $_POST['checkbox1'];

$z = $_POST['checkbox1'] ? : 'not_checked_value';

此外:您为此同一变量分配了不同的帖子值(查看$s) - 您正在丢失一些帖子数据。

另外2:在html中为您的输入添加name属性 - 表单中的所有数据都会分配给它们的名称。

另外3:好的做法是将变量命名为应有的,例如$sex$gender$s更好的名称 - 你会很快迷失方向

答案 1 :(得分:0)

所有复选框显示为已选中的原因即使它们不是,也是因为您手动添加所有输入,无论它们是否被选中。没有理由将每个输入单独添加到FormData。而是添加整个表单。您只需要内联命名表单输入。

替换

var formdata = new FormData();
formdata.append( "n", _("n").value );
formdata.append( "b", _("b").value );
formdata.append( "c", _("c").value );
formdata.append( "n", _("n").value );
formdata.append( "s", _("s").value );
formdata.append( "d", _("d").value );
formdata.append( "p", _("p").value );
formdata.append( "checkbox1", _("checkbox1").value );
formdata.append( "checkbox2", _("checkbox2").value );
formdata.append( "checkbox3", _("checkbox3").value );

var formdata = new FormData(document.getElementById('my_form'));

并更新您的输入以获得name属性

<input id="n" name="n" placeholder="First name" required>

拥有更明智的名字是明智之举,但我会由你决定是否要改变它。

我还建议您学习一个像jQuery(或Angular,无论你想要的)的框架。他们更容易使用ajax lot

此外,我建议您清理isset()逻辑,并在您要检查的组周围添加一些括号。我不知道你有什么东西,所以我不能为你做那件事。