如何使用PHP通过单击单独页面上的链接自动填充表单页面上的下拉菜单
所以我有两个页面,一个是contact.php页面,它有表单输入,等等。另一个页面是index.php,在这里我有链接,可以将您引导到contact.php页面 我试图做的是在单击索引页上的一个链接后,自动填充联系人页上的下拉菜单。因此,如果您单击“链接A”,那么它会将您重定向到在下拉菜单中选择“链接A”的联系人页面 我曾尝试在我的URL中使用查询字符串,例如:“URL?department=Shipping%20Department”,我相信我做得很好,但因为我没有在表单中使用GET,所以它不起作用 最后,表单需要能够在提交后发送电子邮件,这一点我都明白了,只是自动填充让我卡住了 我的任务表上写着“您还将有一个联系人页面,该页面将向邮件页面发送查询字符串,以自动填充部门字段” 下面是我到目前为止每一页的内容。 另外,请提前道歉,这是我的第一个PHP类,所以大部分内容都来自于课程,这可能是一个我没有看到的简单错误 Index.php如何使用PHP通过单击单独页面上的链接自动填充表单页面上的下拉菜单,php,drop-down-menu,query-string,auto-populate,Php,Drop Down Menu,Query String,Auto Populate,所以我有两个页面,一个是contact.php页面,它有表单输入,等等。另一个页面是index.php,在这里我有链接,可以将您引导到contact.php页面 我试图做的是在单击索引页上的一个链接后,自动填充联系人页上的下拉菜单。因此,如果您单击“链接A”,那么它会将您重定向到在下拉菜单中选择“链接A”的联系人页面 我曾尝试在我的URL中使用查询字符串,例如:“URL?department=Shipping%20Department”,我相信我做得很好,但因为我没有在表单中使用GET,所以它不
<?php
include('includes/validate.php');
include('includes/mail.php');
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.form-send {
width: 100%;
max-width: 400px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Contact Us</h1>
<a href="contact.php">Shipping Department</a>
<br/><br/>
<a href="contact.php">Billing Inquiries</a>
<br/><br/>
<a href="contact.php">Party Planning Committee</a>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
<?php
include('includes/validate.php');
include('includes/mail.php');
$hasMailErrors = false;
$hasFormErrors = false;
$isSentMail = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$reply_to_email = validate_email($_POST['email']);
$reply_to_name = validate_string($_POST['name']);
$department = validate_string($_POST['department']);
$message_subject = validate_string($_POST['message-subject']);
$message_body = validate_string($_POST['message-body']);
if ($reply_to_email != false &&
$reply_to_name != false &&
$department != false &&
$message_subject != false &&
$message_body != false) {
$isSentMail = send_custom_mail (
$reply_to_email,
$reply_to_name,
$department,
$message_subject,
"To Whom It May Concern, <br/> <br/>"
.$message_body.
"<br/> <br/> Sincerely, <br/> <br/>"
.$reply_to_name."<br>".$reply_to_email
);
if ($isSentMail) {
$hasMailErrors = true;
}
} else {
$hasFormErrors = true;
}
//validate
}
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.form-send {
width: 100%;
max-width: 400px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<div class="text-center">
<?php if (!$isSentMail) {?>
<form class="form-send" method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php if ($hasFormErrors) { ?>
<div class="alert alert-danger" role="alert">
There was an error with the form
</div>
<?php } ?>
<div class="form-input">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
<div class="form-input">
<label for="email-field">Email address</label>
<input type="email" class="form-control" id="email-field" name="email" placeholder="name@example.com">
</div>
<div class="form-input">
<label for="department">Department</label>
<select name="department" class="form-control" id="department">
<option selected="selected" value="Shipping Department">Shipping Department</option>
<option selected="selected" value="Billing Inquiries">Billing Inquiries</option>
<option selected="selected" value="Party Planning Committee">Party Planning Committee</option>
</select>
</div>
<div class="form-input">
<label for="subject">Subject</label>
<input type="text" class="form-control" id="subject" name="message-subject" placeholder="subject">
</div>
<div class="form-input">
<label for="message-body">Enter Message Below</label>
<textarea name="message-body" class="form-control" id="message-body" rows="3" placeholder="enter your message"></textarea>
</div>
<button class="submit-button" type="submit">Send</button>
</form>
<?php } else { ?>
<h1>Thank you!</h1>
<?php } ?>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
<?php
require __DIR__.'/../vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function send_custom_mail($reply_to_email, $reply_to_name, $department, $subject, $message_body) {
$gmail_username = "";
$gmail_password = "";
$gmail_name = "";
$mailDebug = false;
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = $gmail_username;
$mail->Password = $gmail_password;
$mail->SMTPSecure = "tls";
if ($mailDebug) {
$mail ->SMTPDebug = 2;
}
$mail->Host ="smtp.gmail.com";
$mail->Port = "587";
$mail->From = $gmail_username;
$mail->FromName = $reply_to_name;
$mail->AddReplyTo($reply_to_email, $reply_to_name);
$mail->AddAddress($gmail_username, $gmail_name);
$mail->IsHTML(true);
$mail->Subject = $department.' - '.$subject;
$mail->Body = $message_body;
if ($mail->Send()) {
return true;
} else {
if ($mailDebug) {
echo $mail->ErrorInfo;
}
return false;
}
}
<?php
function validate_email($email) {
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
return $email;
}
function validate_string($string) {
$string = filter_var($string, FILTER_SANITIZE_STRING);
if ($string === "") {
return false;
}
return $string;
}
.表格寄出{
宽度:100%;
最大宽度:400px;
填充:15px;
保证金:0自动;
}
联系我们
这没有所有带有查询字符串的html链接
Contact.php
<?php
include('includes/validate.php');
include('includes/mail.php');
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.form-send {
width: 100%;
max-width: 400px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Contact Us</h1>
<a href="contact.php">Shipping Department</a>
<br/><br/>
<a href="contact.php">Billing Inquiries</a>
<br/><br/>
<a href="contact.php">Party Planning Committee</a>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
<?php
include('includes/validate.php');
include('includes/mail.php');
$hasMailErrors = false;
$hasFormErrors = false;
$isSentMail = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$reply_to_email = validate_email($_POST['email']);
$reply_to_name = validate_string($_POST['name']);
$department = validate_string($_POST['department']);
$message_subject = validate_string($_POST['message-subject']);
$message_body = validate_string($_POST['message-body']);
if ($reply_to_email != false &&
$reply_to_name != false &&
$department != false &&
$message_subject != false &&
$message_body != false) {
$isSentMail = send_custom_mail (
$reply_to_email,
$reply_to_name,
$department,
$message_subject,
"To Whom It May Concern, <br/> <br/>"
.$message_body.
"<br/> <br/> Sincerely, <br/> <br/>"
.$reply_to_name."<br>".$reply_to_email
);
if ($isSentMail) {
$hasMailErrors = true;
}
} else {
$hasFormErrors = true;
}
//validate
}
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.form-send {
width: 100%;
max-width: 400px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<div class="text-center">
<?php if (!$isSentMail) {?>
<form class="form-send" method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php if ($hasFormErrors) { ?>
<div class="alert alert-danger" role="alert">
There was an error with the form
</div>
<?php } ?>
<div class="form-input">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
<div class="form-input">
<label for="email-field">Email address</label>
<input type="email" class="form-control" id="email-field" name="email" placeholder="name@example.com">
</div>
<div class="form-input">
<label for="department">Department</label>
<select name="department" class="form-control" id="department">
<option selected="selected" value="Shipping Department">Shipping Department</option>
<option selected="selected" value="Billing Inquiries">Billing Inquiries</option>
<option selected="selected" value="Party Planning Committee">Party Planning Committee</option>
</select>
</div>
<div class="form-input">
<label for="subject">Subject</label>
<input type="text" class="form-control" id="subject" name="message-subject" placeholder="subject">
</div>
<div class="form-input">
<label for="message-body">Enter Message Below</label>
<textarea name="message-body" class="form-control" id="message-body" rows="3" placeholder="enter your message"></textarea>
</div>
<button class="submit-button" type="submit">Send</button>
</form>
<?php } else { ?>
<h1>Thank you!</h1>
<?php } ?>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
<?php
require __DIR__.'/../vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function send_custom_mail($reply_to_email, $reply_to_name, $department, $subject, $message_body) {
$gmail_username = "";
$gmail_password = "";
$gmail_name = "";
$mailDebug = false;
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = $gmail_username;
$mail->Password = $gmail_password;
$mail->SMTPSecure = "tls";
if ($mailDebug) {
$mail ->SMTPDebug = 2;
}
$mail->Host ="smtp.gmail.com";
$mail->Port = "587";
$mail->From = $gmail_username;
$mail->FromName = $reply_to_name;
$mail->AddReplyTo($reply_to_email, $reply_to_name);
$mail->AddAddress($gmail_username, $gmail_name);
$mail->IsHTML(true);
$mail->Subject = $department.' - '.$subject;
$mail->Body = $message_body;
if ($mail->Send()) {
return true;
} else {
if ($mailDebug) {
echo $mail->ErrorInfo;
}
return false;
}
}
<?php
function validate_email($email) {
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
return $email;
}
function validate_string($string) {
$string = filter_var($string, FILTER_SANITIZE_STRING);
if ($string === "") {
return false;
}
return $string;
}
.表格寄出{
宽度:100%;
最大宽度:400px;
填充:15px;
保证金:0自动;
}
表单有错误
名称
电子邮件地址
部门
航运部
账单查询
党的计划委员会
主题
在下面输入消息
发送
非常感谢。
然后我有我的邮件和验证包括
Mail.php
<?php
include('includes/validate.php');
include('includes/mail.php');
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.form-send {
width: 100%;
max-width: 400px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Contact Us</h1>
<a href="contact.php">Shipping Department</a>
<br/><br/>
<a href="contact.php">Billing Inquiries</a>
<br/><br/>
<a href="contact.php">Party Planning Committee</a>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
<?php
include('includes/validate.php');
include('includes/mail.php');
$hasMailErrors = false;
$hasFormErrors = false;
$isSentMail = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$reply_to_email = validate_email($_POST['email']);
$reply_to_name = validate_string($_POST['name']);
$department = validate_string($_POST['department']);
$message_subject = validate_string($_POST['message-subject']);
$message_body = validate_string($_POST['message-body']);
if ($reply_to_email != false &&
$reply_to_name != false &&
$department != false &&
$message_subject != false &&
$message_body != false) {
$isSentMail = send_custom_mail (
$reply_to_email,
$reply_to_name,
$department,
$message_subject,
"To Whom It May Concern, <br/> <br/>"
.$message_body.
"<br/> <br/> Sincerely, <br/> <br/>"
.$reply_to_name."<br>".$reply_to_email
);
if ($isSentMail) {
$hasMailErrors = true;
}
} else {
$hasFormErrors = true;
}
//validate
}
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.form-send {
width: 100%;
max-width: 400px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<div class="text-center">
<?php if (!$isSentMail) {?>
<form class="form-send" method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php if ($hasFormErrors) { ?>
<div class="alert alert-danger" role="alert">
There was an error with the form
</div>
<?php } ?>
<div class="form-input">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
<div class="form-input">
<label for="email-field">Email address</label>
<input type="email" class="form-control" id="email-field" name="email" placeholder="name@example.com">
</div>
<div class="form-input">
<label for="department">Department</label>
<select name="department" class="form-control" id="department">
<option selected="selected" value="Shipping Department">Shipping Department</option>
<option selected="selected" value="Billing Inquiries">Billing Inquiries</option>
<option selected="selected" value="Party Planning Committee">Party Planning Committee</option>
</select>
</div>
<div class="form-input">
<label for="subject">Subject</label>
<input type="text" class="form-control" id="subject" name="message-subject" placeholder="subject">
</div>
<div class="form-input">
<label for="message-body">Enter Message Below</label>
<textarea name="message-body" class="form-control" id="message-body" rows="3" placeholder="enter your message"></textarea>
</div>
<button class="submit-button" type="submit">Send</button>
</form>
<?php } else { ?>
<h1>Thank you!</h1>
<?php } ?>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
<?php
require __DIR__.'/../vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function send_custom_mail($reply_to_email, $reply_to_name, $department, $subject, $message_body) {
$gmail_username = "";
$gmail_password = "";
$gmail_name = "";
$mailDebug = false;
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = $gmail_username;
$mail->Password = $gmail_password;
$mail->SMTPSecure = "tls";
if ($mailDebug) {
$mail ->SMTPDebug = 2;
}
$mail->Host ="smtp.gmail.com";
$mail->Port = "587";
$mail->From = $gmail_username;
$mail->FromName = $reply_to_name;
$mail->AddReplyTo($reply_to_email, $reply_to_name);
$mail->AddAddress($gmail_username, $gmail_name);
$mail->IsHTML(true);
$mail->Subject = $department.' - '.$subject;
$mail->Body = $message_body;
if ($mail->Send()) {
return true;
} else {
if ($mailDebug) {
echo $mail->ErrorInfo;
}
return false;
}
}
<?php
function validate_email($email) {
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
return $email;
}
function validate_string($string) {
$string = filter_var($string, FILTER_SANITIZE_STRING);
if ($string === "") {
return false;
}
return $string;
}
请更新index.php中的链接,如下所示:
<a href="contact.php?department=Shipping Department">Shipping Department</a>
<a href="contact.php?department=Billing Inquiries">Billing Inquiries</a>
<a href="contact.php?department=Party Planning Committee">Party Planning Committee</a>
并更改contact.php中的选项,如下所示:
<?php $department = isset($_GET['department']) ? $_GET['department'] : "Shipping Departmen"; ?>
<option <?php if ($department == "Shipping Department"): ?>selected="selected"<?php endif; ?> value="Shipping Department">Shipping Department</option>
<option <?php if ($department == "Billing Inquiries"): ?>selected="selected"<?php endif; ?> value="Billing Inquiries">Billing Inquiries</option>
<option <?php if ($department == "Party Planning Committee"): ?>selected="selected"<?php endif; ?> value="Party Planning Committee">Party Planning Committee</option>
value=“运输部门”>运输部门
value=“账单查询”>账单查询
value=“党的计划委员会”>党的计划委员会
如果两个文件都在同一目录中,它应该可以工作。如果您在索引
页面上更改HTML,如下所示:
<a href="contact.php?department=shipping">Shipping Department</a>
<a href="contact.php?department=billing">Billing Inquiries</a>
<a href="contact.php?department=planning">Party Planning Committee</a>
通过将selected hidden disabled
添加到select
菜单中的初始选项
,可以确保用户看到该菜单,并且必须从中选择一个选项,而不是依赖默认选择的菜单中的第一个选项。如果用户修改querystring变量的值未出现在departments数组的键中,则将再次显示此请选择
,并且如果提交表单,将公布错误
的值
一点:不要使用$\u服务器['PHP\u SELF']
作为表单操作-如果脚本要发布到自身,则只需完全忽略该操作,因为$\u服务器['PHP\u SELF']
容易被劫持。它确实有效,谢谢!但是,当我在contact.php页面上使用下拉菜单时,选项现在会显示“注意:未定义索引:部门中…”。我如何才能使其成为正常选项?我已经更新了contact.php的相关代码,这将解决您的问题。谢谢,这已经解决了!!