添加复选框到PHP表单

时间:2012-08-15 18:13:08

标签: php html

我有一个使用PHP创建的表单(我几乎不了解我喜欢的东西) - 我在一段时间后使用在线教程制作表单,不能完全记住从哪里找不到它再次,但形式的工作原理,所以我很满意。

唯一的问题是,我需要在我的表单中添加复选框,他们不需要验证(如果没有选中任何内容,表单仍然可以发送,如果它们都被检查了表单仍然可以发送..)

我需要做的是发回给我的电子邮件,让我知道勾选了哪些复选框。

这是我的HTML / PHP页面:

<!--Contact Form Section -->    
<div id="contact-form" class="clearfix">  

        <?php  
//init variables  
$cf = array();  
$sr = false;  

if(isset($_SESSION['cf_returndata'])){  
$cf = $_SESSION['cf_returndata'];  
$sr = true;  
}  
?>

<form method="post" action="process.php">  

    <input type="email" id="email" name="email" placeholder="Your e-mail" value="    <?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" required="required" />  

    <input type="text" id="name" name="name" placeholder="Your name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" required="required" />  

    <textarea id="message" name="message" placeholder="Your message..." required="required" data-minlength="20"><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>  

<span id="loading"></span>  
<input type="submit" value="&nbsp;" id="submit-button" /> 

    <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">  
    <li id="info">There is a problem:</li>  
<?php  
if(isset($cf['errors']) && count($cf['errors']) > 0) :  
    foreach($cf['errors'] as $error) :  
?>  
    <li><?php echo $error ?></li>  
<?php  
    endforeach;  
endif;  
?>  
</ul>  

<p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for your message!</p>
</div>

</form>  
<?php unset($_SESSION['cf_returndata']); ?>   
<!--End Contact Form Section -->    

这是我的流程PHP文档:

<?php  
if( isset($_POST) ){  

//form validation vars  
$formok = true;  
$errors = array();  

//submission data  
$ipaddress = $_SERVER['REMOTE_ADDR'];  
$date = date('d/m/Y');  
$time = date('H:i:s');  

//form data  
$name = $_POST['name'];  
$email = $_POST['email'];  
$telephone = $_POST['telephone'];  
$enquiry = $_POST['enquiry'];  
$message = $_POST['message'];  

//validate form data  

//validate name is not empty  
if(empty($name)){  
    $formok = false;  
    $errors[] = "You have not entered a name";  
}  

//validate email address is not empty  
if(empty($email)){  
    $formok = false;  
    $errors[] = "You have not entered an email address";  
//validate email address is valid  
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){  
    $formok = false;  
    $errors[] = "You have not entered a valid email address";  
}  

//validate message is not empty  
if(empty($message)){  
    $formok = false;  
    $errors[] = "You have not entered a message";  
}  
//validate message is greater than 20 characters  
elseif(strlen($message) < 20){  
    $formok = false;  
    $errors[] = "Your message must be greater than 20 characters";  
}  

//send email if all is ok  
if($formok){  
    $headers = "From: Goldie Locks online contact form" . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

    $emailbody = "<p>You have received a new message from the enquiries form on your website.</p> 
                  <p><strong>Name: </strong> {$name} </p> 
                  <p><strong>Email Address: </strong> {$email} </p> 
                  <p><strong>Message: </strong> {$message} </p> 
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";  

    mail("MY EMAIL!","New Enquiry",$emailbody,$headers);  

}  

//what we need to return back to our form  
$returndata = array(  
    'posted_form_data' => array(  
        'name' => $name,  
        'email' => $email,  
        'telephone' => $telephone,  
        'enquiry' => $enquiry,  
        'message' => $message  
    ),  
    'form_ok' => $formok,  
    'errors' => $errors  
);  

//if this is not an ajax request  
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&     strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){  
    //set session variables  
    session_start();  
    $_SESSION['cf_returndata'] = $returndata;  

    //redirect back to form  
    header('location: ' . $_SERVER['HTTP_REFERER']);  
}  
}

正如你从html代码中看到的那样,我拿出了PHP的一些功能,但是在PHP过程中留下了它们(例如查询类型和电话号码),因为我完全不确定会破坏什么。

我需要做的就是在html / php文档中添加复选框(准确地说是5),并且当有人填写页面时,我收到的电子邮件中会显示勾选的复选框。

希望对于拥有PHP知识的人来说这是一件容易的事情,但遗憾的是我很少。希望有人可以提供帮助,如果您需要更多信息,请发表评论,我会尽我所能。

谢谢

3 个答案:

答案 0 :(得分:2)

只需添加一个复选框输入元素,即可向表单添加复选框:

<input type="checkbox" name"myCheckbox" value="true" />

value=""设置为您想要的任何内容。然后,您可以使用以下命令在PHP脚本中选择value

$myCheckbox = $_POST['myCheckbox'];

变量$myCheckbox将包含表格中名称为“myCheckbox”的复选框的值(如果已选中),或者如果未选中该复选框,则变为空白。

然后,您可以像使用其他变量一样将值添加到电子邮件中,即

$emailbody = "<p>You have received a new message from the enquiries form on your website.</p> 
    <p><strong>Name: </strong> {$name} </p> 
    <p><strong>Email Address: </strong> {$email} </p> 
    <p><strong>Message: </strong> {$message} </p>
    <p><strong>myCheckbox value: </strong> {$myCheckbox}</p>
    <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

如果您希望在表单上保留已选中/未选中状态(如果已提交并因任何原因未通过验证),则可以将复选框值添加到$returndata变量中:

$returndata = array(
    'posted_form_data' => array(  
        'name' => $name,  
        'email' => $email,  
        'telephone' => $telephone,  
        'enquiry' => $enquiry,  
        'message' => $message,
        'myCheckbox' => $myCheckbox,
    ),  
    'form_ok' => $formok,  
    'errors' => $errors
);

这会将其发送回表单,然后您需要更改复选框以像其他元素一样查找此值,并将“checked = Checked”值添加到复选框(如果之前已选中):

<input type="checkbox" name"myCheckbox" value="true" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['myCheckbox]=="true") ? 'checked="checked"' : '' ?> />

true的{​​{1}}部分是复选框的$cf['posted_form_data']['myCheckbox]=="true"值。

我希望这是有道理的。

答案 1 :(得分:1)

您只需要在表单中添加复选框输入,然后在PHP中检索它们:

HTML:

<form method="post" action="process.php">  

<input type="email" id="email" name="email" placeholder="Your e-mail" value="    <?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" required="required" />  

<input type="text" id="name" name="name" placeholder="Your name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" required="required" />  

<input type="checkbox" id="CHECKBOX1" name="CHECKBOX1" value="" />Checkbox 1

(用你想要的复选框重复上一次输入)

PHP:

<?php  
if( isset($_POST) ){  

//form validation vars  
$formok = true;  
$errors = array();  

//submission data  
$ipaddress = $_SERVER['REMOTE_ADDR'];  
$date = date('d/m/Y');  
$time = date('H:i:s');  

//form data  
$name = $_POST['name'];  
$email = $_POST['email'];  
$telephone = $_POST['telephone'];  
$enquiry = $_POST['enquiry'];  
$message = $_POST['message']; <br />
**$checkbox1 = $_POST['CHECKBOX1'];**

//validate form data  

//validate name is not empty  
if(empty($name)){  
    $formok = false;  
    $errors[] = "You have not entered a name";  
}  

//validate email address is not empty  
if(empty($email)){  
    $formok = false;  
    $errors[] = "You have not entered an email address";  
//validate email address is valid  
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){  
    $formok = false;  
    $errors[] = "You have not entered a valid email address";  
}  

//validate message is not empty  
if(empty($message)){  
    $formok = false;  
    $errors[] = "You have not entered a message";  
}  
//validate message is greater than 20 characters  
elseif(strlen($message) < 20){  
    $formok = false;  
    $errors[] = "Your message must be greater than 20 characters";  
}  

//send email if all is ok  
if($formok){  
    $headers = "From: Goldie Locks online contact form" . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

    $emailbody = "<p>You have received a new message from the enquiries form on your website.</p> 
                  <p><strong>Name: </strong> {$name} </p> 
                  <p><strong>Email Address: </strong> {$email} </p> 
                  <p><strong>Message: </strong> {$message} </p> 
                  **<p><strong>Checkbox1: </strong>{$checkbox1}</p>**
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";  

    mail("MY EMAIL!","New Enquiry",$emailbody,$headers);  

}  

//what we need to return back to our form  
$returndata = array(  
    'posted_form_data' => array(  
        'name' => $name,  
        'email' => $email,  
        'telephone' => $telephone,  
        'enquiry' => $enquiry,  
        'message' => $message  
    ),  
    'form_ok' => $formok,  
    'errors' => $errors  
);  

//if this is not an ajax request  
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&     strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){  
    //set session variables  
    session_start();  
    $_SESSION['cf_returndata'] = $returndata;  

    //redirect back to form  
    header('location: ' . $_SERVER['HTTP_REFERER']);  
}  
}

答案 2 :(得分:1)

添加复选框:

<input type="checkbox" name="someName" id="someId" value=""/>Checkboxtitle 

用于生成复选框的HTML代码。然后,要将值添加到您的电子邮件中,您只需像普通文本框一样调用它。