从 URL 传递参数以在下一页上预填充表单字段

Passing a parameter from a URL to pre-populate a form field on next page

基于 'listing' (URL) link 从 'Listing Page' 使用...我需要能够预填充 'selectedListing' 'Form Page' 字段上的特定数据。

我已经有 'formHandler' 页面的有效 php 代码。

我确定我还需要在这两个页面之间玩一些 PHP,我只是不知道该怎么做。

列表页

<!DOCTYPE html>

<head>

<title>Listing Page</title>

<style>

.theListings {

margin: auto;
text-align: center;

}

</style>

</head>

<body>

<div class="theListings">

<h1>The Listings</h1>

<a>To get a quote, please select the listing you are interested from the list below:</a>
<br>
<br>   
<a href="userForm.php" value="Listing A">Listing A</a>
<br>
<br>
<a href="userForm.php" value="Listing B">Listing B</a>
<br>
<br>
<a href="userForm.com" value="Listing C">Listing C</a>

</div>

</body>

</html>

表单页面

<!DOCTYPE html>

<head>

<title>Form Page</title>

<style>

.formContainer {

margin: auto;
text-align: center;

}

</style>

</head>

<body>

<div class="formContainer">

<h1>The Form</h1>

<a>To complete your quote, please fill out and submit the form below.</a><br><br>   

<form action="/formHandler.php" method="post" enctype="multipart/form-data">

<input type="text" class="selectedListing" name="selectListing" placeholder="Selected 
Listing"><br><br>

<input type="text" name="userName" placeholder="Full Name"><br><br>

<input type="email" name="userEmail" placeholder="Email Address"><br><br>

<input type="tel" name="userPhone" placeholder="Phone Number"><br><br>

<input type="submit" name="submitButton" value="Submit Form">

</form>

</div>

</body>

</html>

ahttps://developer.mozilla.org/en-US/docs/Web/HTML/Element/a 上没有值属性。您可以在 URL 上将值作为 GET 参数传递。变化:

<a href="userForm.php" value="Listing A">

至:

<a href="userForm.php?listing=A">

然后在 userForm.php 使用:

$_GET['listing']

读取值。

如果 input 假设具有您可以执行的值:

<input type="text" class="selectedListing" name="selectListing" placeholder="Selected 
Listing" value="<?php echo (!empty($_GET['listing']) && in_array($_GET['listing'], array('A', 'B', 'C')) ? $_GET['listing'] : ''); ?>">