我有一个联系表单,有一个下拉列表,可以选择查询应该去哪个部门......
<form action="contact-send.php" id="callback" method="post">
<ul>
<li class="left">
<label for="name"><span class="required">*</span> Your Name</label>
<br>
<input type="text" id="name" name="name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" placeholder="John Doe" required autofocus /></li>
<li class="right">
<label for="email"><span class="required">*</span> Email Address</label>
<br>
<input type="email" id="email" name="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" placeholder="johndoe@example.com" required /></li>
<div class="clear"></div>
<li class="left">
<label for="enquiry"><span class="required">*</span> Enquiry</label><br>
<select id="enquiry" name="enquiry">
<option value="Admin / Accounts enquiry" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'Admin / Accounts enquiry') ? "selected='selected'" : '' ?>>Admin / Accounts enquiry</option>
<option value="Plant Hire / Haulage / Recycling / Contract Earthworks" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'Plant Hire / Haulage / Recycling / Contract Earthworks') ? "selected='selected'" : '' ?>>Plant Hire / Haulage / Recycling / Contract Earthworks</option>
<option value="Waste Management / Skip Hire" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'Waste Management / Skip Hire') ? "selected='selected'" : '' ?>>Waste Management / Skip Hire</option>
</select></li>
<li>
<label for="message"><span class="required">*</span> Message</label><br />
<textarea id="message" name="message" placeholder="Your message must be greater than 20 charcters" required data-minlength="20"><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>
</li>
</ul>
<div class="clear"></div>
<p class="form-button right"><input class="submit" id="callback-submit" name="submit" value="Send" type="submit"></p>
</form>
我正在尝试弄清楚如何修改以下流程脚本,以便将查询发送到指定的电子邮件地址,具体取决于所选的部门...
<?php
function clean_input($input){
return strip_tags(trim($input));
}
if( isset($_POST) ){
//form validation vars
$formok = true;
$errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$name = clean_input($_POST['name']);
$email = clean_input($_POST['email']);
$enquiry = clean_input($_POST['enquiry']);
$message = clean_input($_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 charcters
elseif(strlen($message) < 20){
$formok = false;
$errors[] = "Your message must be greater than 20 characters";
}
//send email if all is ok
if($formok){
ini_set("sendmail_from","EMAIL_ADDRESS");
$headers = "From: EMAIL_ADDRESS" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$emailbody = "<p>You have received a new contact us enquiry.</p>
<p><strong>Name: </strong> {$name} </p>
<p><strong>Email: </strong> {$email} </p>
<p><strong>Enquiry: </strong> {$enquiry} </p>
<p><strong>Message: </strong> {$message} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
mail("EMAIL_ADDRESS","Contact us enquiry",$emailbody,$headers);
}
//what we need to return back to our form
$returndata = array(
'posted_form_data' => array(
'name' => $name,
'email' => $email,
'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']);
}
}
如果有人能提供一些指示,我将不胜感激。
感谢。
答案 0 :(得分:0)
执行此操作的最佳方法是构建数据库表(或只是config / php数组),如下所示:
CREATE TABLE `departments` (
id INT AUTO_INCREMENT,
name VARCHAR(255),
email VARCHAR(255),
PRIMARY (id)
);
将其打印为:
$selected = isset( $_POST['department']) ? $_POST['department'] : '';
echo '<select name="department">';
foreach( $db->fetch_all( 'SELECT * FROM departments') as $dep){
echo '<option value="' . $dep['id'] . '"' .
(($dep['id'] == $selected) ? 'selected="selected"' : '')
. '>' . htmlspecialchars( $dep['name']) . '</option>';
}
发送时:
$email = '';
if( !isset( $_POST['department'])){
$formok = false;
$errors[] = "Department not send";
} elseif( $row = $db->fetch_row( 'SELECT email FROM departments WHERE id = ?', array( $_POST['department'])){
$email = $row['email']
} else {
$formok = false;
$errors[] = "Invalid department";
}
/// ...
mail( $email, ...);
答案 1 :(得分:0)
使用您用来代表部门的表单元素的名称,只需使用switch case来更改传递地址?
值得注意的是,更简单的值,例如admin_enquiry
而非管理员/帐户查询可能更容易匹配。
switch ($select_department)
{
case "admin_enquiry":
// Set to email as appropriate
}