首页 > 解决方案 > 如何通过使用 PHP 单击单独页面上的链接来自动填充表单页面上的下拉菜单

问题描述

所以我有两个页面,一个是包含表单输入等的contact.php 页面。另一个页面是index.php,其中我有将您引导到contact.php 页面的链接。

我要做的是在单击索引页面上的链接之一后自动填充我的联系页面上的下拉菜单。因此,假设您单击“链接 A”,那么它应该将您重定向到在下拉菜单中选择“链接 A”的联系页面。

我已经尝试在我的 URL 中使用查询字符串,例如:“URL?department=Shipping%20Department”,我相信我一直在做正确的事情,但是由于我没有在我的表单中使用 GET,所以它不起作用?

最后,表单需要能够在提交后发送电子邮件,我已经弄清楚了,只是自动填充让我卡住了。

我的作业表显示“您还将有一个联系页面,该页面将查询字符串发送到邮件页面以自动填充部门字段”

以下是我迄今为止对每一页的内容。另外,提前抱歉,这是我的第一个 PHP 课程,所以大部分内容来自课程,这可能是一个我没有看到的简单错误。

索引.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>

这没有带有查询字符串的所有 html 链接。

联系方式.php

<?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

<?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

<?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;
}

标签: phpdrop-down-menuquery-stringauto-populate

解决方案


请更新 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>

如果两个文件都在同一个目录中,它应该可以工作。


推荐阅读