简体   繁体   English

PHP复选框表单-显示值

[英]PHP checkbox form - displaying values

I have a checkbox in my contact form and I don't seem to be able to display the values in the email being sent via php. 我的联系表单中有一个复选框,但似乎无法显示通过php发送的电子邮件中的值。 I know this is probably an isset problem, but I cannot fix it to show whether the user has clicked the checkbox or not. 我知道这可能是一个isset问题,但我不能修复它显示用户是否点击复选框与否。

Form: 形成:

<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: 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.";
    }
}
?>

As I remember not checked checkboxes are not sent with POST . 我记得未选中的复选框未与POST发送。 In Your php script change 在您的PHP脚本更改

$z = $_POST['checkbox1'];

to

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

Additionally: You assigned different post values to this same variable (look at $s ) - You are losing some post data. 另外:您为该变量分配了不同的发布值(请参见$s )-您正在丢失一些发布数据。

Additionally 2: add name attributes to Your inputs in html - all data from forms are assigned to their names. 附加2:将name属性添加到html输入中-表单中的所有数据都分配给它们的名称。

Additionally 3: Good practice is to name variables as their are should be, for example $sex or $gender would be better name than $s - You will quick get lost in that way. 另外3:优良作法是按照变量的名称命名,例如$sex$gender$s更好地命名-这样您很快就会迷路。

The reason why all your checkboxes are showing as checked even if they aren't is because you're manually adding all of the inputs regardless of whether they are checked or not. 即使您的所有复选框都未选中,它们也都显示为选中状态的原因是,您正在手动添加所有输入,无论是否选中它们。 There's no reason to be adding each input to your FormData individually. 没有理由将每个输入单独添加到FormData Instead, add the entire form. 而是,添加整个表单。 You'll just need to name your form inputs inline. 您只需要内联命名表单输入。

Replace 更换

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

with

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

and update your inputs to have a name attribute 并更新您的输入以具有name属性

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

It would be smart to have more sensible names, but I'll leave that up to you to decide if you want to change that. 拥有更明智的名称是明智的,但是我将由您决定是否要更改它。

I also suggest that you learn a framework like jQuery (or Angular, whatever you want). 我还建议您学习类似jQuery(或Angular,随便什么)的框架。 They make using ajax a lot easier. 他们让使用AJAX轻松了许多

Further, I suggest that you clean up your isset() logic and add some parenthesis around the groups that you're trying to check. 此外,我建议您清理isset()逻辑,并在要检查的组周围添加一些括号。 I can't tell from what you have what you're really going for there, so I can't do that for you. 我无法从您拥有的东西中得知您真正要去的地方,所以我无法为您做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM