简体   繁体   English

无法正确处理HTML PHP表单中的值

[英]Can't processing values in HTML PHP Form correctly

This is seriously doing my head in and I can't figure out what I'm doing wrong but I've got an online php form with checkboxes but when I leave the checkboxes empty and submit the form, receive the email ... the very first checkbox option shows up (almost like I've ticked / chosen that option) and the second checkbox option doesn't show up (which seems correct cos I didn't tick it) 这是认真的,我无法弄清楚我在做什么错,但是我有一个带有复选框的在线php表单,但是当我将复选框保留为空并提交表单时,接收电子邮件...第一个复选框选项显示(几乎就像我已选中/选中该选项一样),第二个复选框选项未显示(这似乎是正确的,因为我没有勾选)

Then when I tick both checkboxes, the same results come back as above. 然后,当我同时选中两个复选框时,相同的结果将如上返回。

I want it to behave normally like: 1) only the ticked checkbox options must show up 2) If I don't tick any checkboxes, they must not show up in the form email submission. 我希望它的行为正常:1)只有选中的复选框选项必须显示2)如果我没有选中任何复选框,则它们一定不能出现在电子邮件提交表单中。

My PHP Form Processor: 我的PHP表单处理器:

<?php if (!isset($_SESSION)) session_start();

if(!$_POST) exit;

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");



$address = "email@example.com";
$bcc = "email@example.com";

    $twitter_active     = 0;
    $twitter_user       = "";
    $consumer_key       = "";
    $consumer_secret    = "";
    $token              = "";
    $secret             = "";

$title = $_POST['title'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$idnum = $_POST['idnum'];
$birth = $_POST['birth'];
$married = $_POST['married'];
$officephone = $_POST['officephone'];
$cellphone = $_POST['cellphone'];
$email = $_POST['email'];
$company = $_POST['company'];
$position = $_POST['position'];
$addressline1 = $_POST['addressline1'];
$addressline2 = $_POST['addressline2'];
$city = $_POST['city'];
$postalcode  = $_POST['postalcode'];
$bookingdate = $_POST['bookingdate'];
$estimate = $_POST['estimate'];
$currentpackages = $_POST['currentpackages'];

$treatment01 = $_POST['treatment01'];

$specialrequests = $_POST['specialrequests'];

if (isset($_POST['verify'])) :
    $posted_verify   = $_POST['verify'];
    $posted_verify   = md5($posted_verify);
else :
    $posted_verify = '';
endif;

// Important Variables
$session_verify = $_SESSION['verify'];

if (empty($session_verify)) $session_verify = $_COOKIE['verify'];

$error = '';

    if(trim($title) == '') {
        $error .= '<li>Your title is required.</li>';
    }

    if(trim($firstname) == '') {
        $error .= '<li>Your first name is required.</li>';
    }

    if(trim($lastname) == '') {
        $error .= '<li>Your last name is required.</li>';
    }

    if(trim($idnum) == '') {
        $error .= '<li>Your ID Number is required.</li>';
    }

    if(trim($birth) == '') {
        $error .= '<li>Your Date of Birth is required.</li>';
    }

    if(trim($cellphone) == '') {
        $error .= '<li>Your cell number is required.</li>';
    } elseif(!is_numeric($cellphone)) {
        $error .= '<li>Your cell number can only contain digits (numbers and no 
 spaces).</li>';
    }

    if(trim($email) == '') {
        $error .= '<li>Your e-mail address is required.</li>';
    } elseif(!isEmail($email)) {
        $error .= '<li>You have entered an invalid e-mail address.</li>';
    }

    if(trim($bookingdate) == '') {
        $error .= '<li>You must enter your booking date.</li>';
    }

    if(trim($estimate) == '') {
        $error .= '<li>You must enter your estimate time of arrival.</li>';
    }

    if(trim($currentpackages) == '') {
        $error .= '<li>You must enter a package.</li>';
    }

    if($session_verify != $posted_verify) {
        $error .= '<li>The verification code you entered is incorrect.</li>';
    }

    if($error != '') {
        echo '<div class="error_title">Attention! Please correct the errors below and 
try again.';
        echo '<ul class="error_messages">' . $error . '</ul>';
        echo '<div class="closebox"><img src="icon-close.png" /></div>';
        echo '</div>';

    } else {

    if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }


     $e_subject = 'Check in Details from example.com';


     $msg = '<html><body>
<table width="600" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="600" height="88" valign="top"><table width="100%" border="0" 
cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="600" height="88" align="center" valign="top" bgcolor="#F0F5FB"><span 
style="font-family:Georgia, \'Times New Roman\', Times, serif; font-size: 24px; 
font-style:italic; color:#0099CC; line-height:48px; "><strong>Check in 
Details</strong> from:</span><br />
      <span style="background-color:#FFF; padding: 5px 20px 5px 20px; 
font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#818181"><a 
style="color:#818181; text-decoration:none;" href="http://www.example.com" 
target="_blank">www.example.com</a></span></td>
</tr>
</table>    </td>
</tr>
<tr>
<td height="30">&nbsp;</td>
</tr>
<tr>
<td height="790" valign="top"><table width="100%" border="0" cellpadding="0" 
cellspacing="0">
  <!--DWLayoutTable-->

  <tr>
    <td width="225" height="570" valign="top"><table width="100%" border="0" 
cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
        <td width="225" height="30" valign="middle"><span style="font-family:Arial, 
Helvetica, sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-  
left:15px;">Title:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">First 
Name:</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 

sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">Last 
Name:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">ID 
Number:</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">Date 
of Birth:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">If 
Married, Date</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Office Phone</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">Cell  
Phone</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">Email:
</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
 left:15px;">Company:</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Position:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Address 
Line 1:</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Address Line 2:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-left:15px;">City:
</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Postal Code:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Booking 
Date:</span></td>
          </tr>
      <tr>
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Estimate 
Time of Arrival:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Current 
Packages:</span></td>
          </tr>
      <tr bgcolor="#F8F8F8">
        <td height="30" valign="middle"><!--DWLayoutEmptyCell-->&nbsp;</td>
          </tr>









      </table></td>
      <td width="375" valign="top"><table width="100%" border="0" cellpadding="0" 
cellspacing="0">
          <!--DWLayoutTable-->
          <tr>
            <td width="375" height="30" valign="middle"><span style="font-
family:Arial, Helvetica, sans-serif; font-size:14px;">'.$_POST['title'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['firstname'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['lastname'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['idnum'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['birth'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['married'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['officephone'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['cellphone'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['email'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['company'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['position'].'</span></td>

          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['addressline1'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['addressline2'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['city'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['postalcode'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['bookingdate'].'</span></td>
          </tr>
          <tr>
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['estimate'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['currentpackages'].'</span></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td height="30" valign="middle"><!--DWLayoutEmptyCell-->&nbsp;</td>
          </tr>










      </table></td>
    </tr>
  <tr>
    <td height="119" colspan="2" valign="top"><table width="100%" border="0" 
cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
        <td height="19" colspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          </tr>
      <tr>
        <td width="225" height="30" valign="middle"><span style="font-family:Arial, 
Helvetica, sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Treatments Category 1:</span></td>
            <td width="375" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['treatment01'].'</span></td>
          </tr>
      <tr>
        <td height="20" colspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          </tr>













      </table></td>
    </tr>
  <tr>
    <td height="71" colspan="2" valign="top"><table width="100%" border="0" 
cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
        <td height="19" colspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          </tr>
      <tr>
        <td width="225" height="30" valign="middle"><span style="font-family:Arial, 
Helvetica, sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Treatments Category 2:</span></td>
            <td width="375" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;"></span></td>
          </tr>
      <tr>
        <td height="20" colspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          </tr>













      </table></td>
    </tr>
  <tr>
    <td height="30" colspan="2" valign="top"><table width="100%" border="0" 
cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
        <td width="225" height="30" valign="middle"><span style="font-family:Arial, 
Helvetica, sans-serif; font-size:14px; color:#333333; font-weight:bold; padding-
left:15px;">Special Requests</span></td>
            <td width="375" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['specialrequests'].'</span></td>
          </tr>

      </table></td>
    </tr>




</table></td>
</tr>
<tr>
<td height="27" valign="top"><table width="100%" border="0" cellpadding="0" 
 cellspacing="0">
    <!--DWLayoutTable-->
    <tr>
      <td width="600" height="27">&nbsp;</td>
    </tr>
</table></td>
</tr>
<tr>
<td height="54" valign="top"><table width="100%" border="0" cellpadding="0" 
cellspacing="0">
    <!--DWLayoutTable-->
    <tr>
      <td width="600" height="54" align="center" valign="middle" 
bgcolor="#F0F5FB"><span style="font-family:Georgia, \'Times New Roman\', Times, serif; 
font-size:12px; font-style:italic; text-decoration:underline;"><a 
style="color:#626262;" href="http://www.example.com/" target="_blank">Click here to 
see your website</a></span>   
</td>
    </tr>
</table></td>
</tr>
</table>
     </body></html>';

    if($twitter_active == 1) {

        $twitter_msg = $name . " - " . $comments . ". You can contact " . $name . " 
via email, " . $email ." or via phone " . $phone . ".";
        twittermessage($twitter_user, $twitter_msg, $consumer_key, $consumer_secret, 
$token, $secret);

    }

    $msg = wordwrap( $msg, 70 );

    $headers = "From: $email\r\nBCC:{$bcc}\r\n" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
    $headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n" . PHP_EOL;

    if(mail($address, $e_subject, $msg, $headers)) {


     echo "<div class='greenbox' id='success_page'>";
     echo "<strong>Email Sent Successfully.</strong>";
     echo "<div class='closebox'><img src='icon-close.png' /></div>";
     echo "</div>";


     } else {

     echo 'ERROR!'; // Dont Edit.

     }

}

function twittermessage($user, $message, $consumer_key, $consumer_secret, $token, 
$secret) { // Twitter Direct Message function, do not edit.

require_once('twitter/EpiCurl.php');
require_once('twitter/EpiOAuth.php');
require_once('twitter/EpiTwitter.php');

$Twitter = new EpiTwitter($consumer_key, $consumer_secret);
$Twitter->setToken($token, $secret);

$direct_message = $Twitter->post_direct_messagesNew( array('user' => $user, 'text' =>  
$message) );
$tweet_info = $direct_message->responseText;

}

function isEmail($email) { // Email address verification, do not edit.

return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*
[[:alnum:]])\.)+  
  (ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|s

csd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt |tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9] [0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]).){3}([0-9][0-9]?|[0-1][0-9][0-9]| [2][0-4][0-9]|[2][5][0-5]))$/i",$email)); 惩教署| SE | SG | SH | SI | SJ | SK | SL | SM | SN |所以| SR | ST |苏| SV | SY |深圳| TC | TD | TF | TG |日| TJ | TK | TM | tn | to | tp | tr | tt | tv | tw | tz | ua | ug | uk | um | us | uy | uz | va | vc | ve | vg | vi | vn | vu | wf | ws | ye | yt | yu | za | zm | zw)$ |(([[0-9] [0-9]?| [0-1] [0-9] [0-9] | [2] [0-4] [0-9] | [2] [5] [0-5])){3}([0-9] [0-9] |。?[0-1] [0-9] [0-9 ] | [2] [0-4] [0-9] | [2] [5] [0-5]))$ / i“,$ email));

}
?>

The code that refers to the checkbox is: 引用该复选框的代码是:

$treatment01 = $_POST['treatment01'];

MY HTML code: 我的HTML代码:

<p><input type="checkbox" name="treatment01[]" id="checkboxG5" class="css-checkbox   
checkboxG5 treatment01" value="Back & Neck Aroma Massage" /><label for="checkboxG5" 
class="css-label">Back & Neck Aroma Massage</label></p>

<p><input type="checkbox" name="treatment01[]" id="checkboxG6" class="css-checkbox 
checkboxG6 treatment01" value="Hello" /><label for="checkboxG6" class="css-
label">Hello</label></p>

and then the javascript that does the form validation (I'm not sure if it's necessary posting this but here it is anyway): 然后执行表单验证的javascript (我不确定是否有必要发布此代码,但是无论如何这里都是这样):

jQuery(document).ready(function() {
$('.checkinform').submit(function() {
    var action = $(this).attr('action');
    var form = this;
    $('.submit', this).attr('disabled', 'disabled').after(
          '<img src="assets/ajax-loader.gif" class="loader" />');
    $('.message', this).slideUp(750, function() {
        $(this).hide();
        $.post(action, {
            title: $('.title', form).val(),
            firstname: $('.firstname', form).val(),
            lastname: $('.lastname', form).val(),
            idnum: $('.idnum', form).val(),
            birth: $('.birth', form).val(),
            married: $('.married', form).val(),
            officephone: $('.officephone', form).val(),
            cellphone: $('.cellphone', form).val(),         
            email: $('.email', form).val(),
            company: $('.company', form).val(),
            position: $('.position', form).val(),
            addressline1: $('.addressline1', form).val(),
            addressline2: $('.addressline2', form).val(),
            city: $('.city', form).val(),
            postalcode: $('.postalcode', form).val(),
            bookingdate: $('.bookingdate', form).val(),
            estimate: $('.estimate', form).val(),
            currentpackages: $('.currentpackages', form).val(),
            treatment01: $('.treatment01', form).val(),
            specialrequests: $('.specialrequests', form).val(),
            verify: $('.verify', form).val()
        },
        function(data) {
            $('.message', form).html(data);
            $('.message', form).slideDown('slow');
            $('img.loader', form).fadeOut('fast', function() {
                $(this).remove();
            });
            $('.submit', form).removeAttr('disabled');
        });
    });
    return false;
});
});

I though checkboxes would be a walk in the park but I've spent so many hours trying to work this out to the point where I'm making guesses hoping to come right! 虽然复选框很容易在公园散步,但是我花了很多时间试图解决这个问题,以至于我猜到了希望!

Thank you in advance! 先感谢您!

Here's your problem: 这是您的问题:

name="treatment01[]"

Not only are you using the same name twice, you are naming it like it's an empty array, or something. 您不仅会两次使用相同的名称,而且还会像是一个空数组或其他东西一样对其进行命名。 I see that you have different IDs on them. 我发现您在它们上具有不同的ID。 But that doesn't matter as the NAME is what gets passed through. 但这无关紧要,因为NAME是通过的。 So you can't check them seperately like you're trying. 因此,您无法像尝试一样单独检查它们。

OK - since you want a more traditional submission of an array to be processed in PHP, you can do this in the post params 好的-由于您希望用PHP处理更传统的数组提交,因此可以在post params中执行此操作

currentpackages: $('.currentpackages', form).val(),
treatment01: $('.treatment01:checked', form).serializeArray(),
specialrequests: $('.specialrequests', form).val(),

Then in your PHP 然后在您的PHP中

$selectedTreatments = '';
if(isset($_POST['treatment01'])){
    $treatments = $_POST['treatment01'];
    foreach($treatments as $treatment){
        //Here we concatenate all treatments in a string with a `<br/>` placed at the end of each one to make them break into separate lines
        $selectedTreatments .= 'Treatment selected: ' . $treatment . '<br/>';
    }
} else {
    $selectedTreatments = 'No treatments selected';
}

and then later on in the file change 然后在文件更改中

<td width="375" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$_POST['treatment01'].'</span></td>

To

<td width="375" valign="middle"><span style="font-family:Arial, Helvetica, 
sans-serif; font-size:14px;">'.$selectedTreatments.'</span></td>

How to send a checkbox group via ajax in jquery? 如何在jQuery中通过ajax发送复选框组?

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

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