1

如果有人可以告诉我,我需要这方面的帮助,那将非常棒且很有帮助。

我试图通过复选框显示一个以上的值,PHP 中的此信息已发送到电子邮件。应用时,我可以让所选列表中的任何人单独工作

这是我的 PHP 和 HTML 代码,一切正常,只是程序只会发送所选列表中的一个,即使我选择了多个。

$ch1, $ch2, $ch3, $ch4,$ch5

PHP

<?php
if(isset($_POST['email'])) {

    $email_to = "";
    $email_subject = "";


    function died($error) {
        // Error Code
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['firstname']) ||
        !isset($_POST['lastname']) ||
        !isset($_POST['checkboxes']) ||
        !isset($_POST['gender']) ||
        !isset($_POST['email']) ||
        !isset($_POST['subject']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $firstname = $_POST['firstname']; // required
    $lastname = $_POST['lastname']; // required
    $ch1 = 'unchecked'; // required
    $ch2 = 'unchecked'; // required
    $ch3 = 'unchecked'; // required
    $ch4 = 'unchecked'; // required
    $ch5 = 'unchecked'; // required
    $male_status = 'unchecked'; // required
    $female_status = 'unchecked'; // required
    $email_from = $_POST['email']; // required
    $subject = $_POST['subject']; // required
    $telephone = $_POST['telephone']; // required
    $message = $_POST['message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$firstname)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$lastname)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$subject)) {
    $error_message .= 'The Subject you entered does not appear to be valid.<br />';
  }

  if (isset($_POST['checkboxes'])) {

    $selected_checkboxes = $_POST['checkboxes'];

    if (isset($_POST['ch1'])) {
        $ch1 = $_POST['checkboxes'];

        if ($ch1 == 'googlechrome') {
            $ch1 = 'checked';
        }
    }

    if (isset($_POST['ch2'])) {
        $ch2 = $_POST['checkboxes'];

        if ($ch2 == 'firefox') {
            $ch2 = 'checked';
        }
    }

    if (isset($_POST['ch3'])) {
        $ch3 = $_POST['checkboxes'];

        if ($ch3 == 'safari') {
            $ch3 = 'checked';
        }
    }

    if (isset($_POST['ch4'])) {
        $ch4 = $_POST['checkboxes'];

        if ($ch4 == 'internetexplorer') {
            $ch4 = 'checked';
        }
    }

    if (isset($_POST['ch5'])) {
        $ch5 = $_POST['checkboxes'];

        if ($ch5 == 'opera') {
            $ch5 = 'checked';
        }
    }
}

  if (isset($_POST['gender'])) {

    $selected_radio = $_POST['gender'];

        if ($selected_radio == 'male') {
            $male_status = 'checked';

        }
        else if ($selected_radio == 'female') {
            $female_status = 'checked';
        }
    }
  if(strlen($message) < 2) {
    $error_message .= 'The Message you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($firstname)."\n";
    $email_message .= "Last Name: ".clean_string($lastname)."\n";
    $email_message .= "Gender: ".clean_string($selected_radio)."\n";
    $email_message .= "Telephone: ".clean_string($telephone).
"\n";
    $email_message .= "Email: ".clean_string($email_from).
"\n";
    $email_message .= "Subject: ".clean_string($subject).
"\n";
$email_message .= "Browsers Used: ".clean_string($selected_checkboxes)."\n";
    $email_message .= "Message: ".clean_string($message).
"\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

HTML

<form id="contact" name="contact" action="contact.php" method="post">
  <p>
    <label for="firstname">First Name *<br>
    </label>
    <input  type="text" name="firstname" maxlength="50" size="30" id="firstname" placeholder="First Name" required autofocus>
    <label for="lastname"><br>
      <br>
      Last Name *<br>
    </label>
    <input  type="text" name="lastname" maxlength="50" size="30" id="lastname" placeholder="Last Name" required>
    <label for="email"><br>
      <br>
      Email Address *<br>
    </label>
    <input  type="text" name="email" maxlength="80" size="30" id="email" placeholder="example@domain.com" required>
    <label for="telephone"><br>
      <br>
      Telephone <br>
    </label>
    <input  type="text" name="telephone" maxlength="30" size="30" id="subject" placeholder="Phone Number" required>
    <br>
    <br>
    <label for="subject">Subject *</label>
    <br>
  <input type="text" name="subject" maxlength="30" size="30" id="subject" placeholder="Hello" required>
    <br>
    <br>
    <Input type = "Radio" Name ="gender" value= "Male">Male
  <Input type = "Radio" Name ="gender" value= "Female" >Female
  <br>
  </p>
  <p>&quot;What browser are you using, to view this website&quot;<br>
    <Input type = "Checkbox" Name ="checkboxes" value="Google Chrome">Google Chrome
    <Input type = "Checkbox" Name ="checkboxes" value="Firefox">Firefox
    <Input type = "Checkbox" Name ="checkboxes" value="Safari">Safari
    <Input type = "Checkbox" Name ="checkboxes" value="Internet Explorer" >Internet Explorer
    <Input type = "Checkbox" Name ="checkboxes" value="Opera" >Opera
    <br>
    <br>
    Message *<br>
    </label>
    <textarea  name="message" maxlength="1000" cols="90" rows="6" id="message" placeholder="Type message here" required></textarea>
    <br>
    <input type="submit" value="Submit">
  </p>
</form>
4

3 回答 3

4
<Input type = "Checkbox" Name ="checkboxes[]" value="Google Chrome">Google Chrome
<Input type = "Checkbox" Name ="checkboxes[]" value="Firefox">Firefox
<Input type = "Checkbox" Name ="checkboxes[]" value="Safari">Safari
<Input type = "Checkbox" Name ="checkboxes[]" value="Internet Explorer" >Internet Explorer
<Input type = "Checkbox" Name ="checkboxes[]" value="Opera" >Opera
于 2012-11-13T02:54:11.490 回答
2

正确的方法是为您的复选框指定不同的名称。

<input type="checkbox" name="chrome" value="Google Chrome" />Chrome
<input type="checkbox" name="firefox" value="Firefox" />Firefox
<input type="checkbox" name="safari" value="Safari" />Safari
<input type="checkbox" name="ie" value="Internet Explorer" />IE
<input type="checkbox" name="opera" value="Opera" />Opera

仅供参考,该<label>标签在这里非常有用,因此当您单击或悬停时,文本将“绑定”到框上。

<label for='cb1'>
    <input id='cb1' type = "Checkbox" name ="chrome" value="Google Chrome" />Chrome
</label>

你可以像这样设计它

label:hover { background:orange; }

这也适用于单选按钮。


此外,最好避免混淆属性的大写和小写(typename等称为属性)。您希望尽可能使用小写字母。

于 2012-11-13T02:54:13.463 回答
0

如果将 html 中的 name 参数更改为 checkboxes[]:

<Input type = "Checkbox" Name ="checkboxes[]" value="Google Chrome">Google Chrome
<Input type = "Checkbox" Name ="checkboxes[]" value="Firefox">Firefox
<Input type = "Checkbox" Name ="checkboxes[]" value="Safari">Safari
<Input type = "Checkbox" Name ="checkboxes[]" value="Internet Explorer" >Internet Explorer
<Input type = "Checkbox" Name ="checkboxes[]" value="Opera" >Opera

然后$_POST['checkboxes']将包含一个选定值的数组。

请注意,它不会为未选中的复选框返回任何内容,因此您需要跟踪所有值以与选中的值进行比较。

于 2012-11-13T03:00:21.390 回答