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

Issue

Based on which ‘listing’ (URL) link is used from the ‘Listing Page’… I need to be able to pre-populate the ‘selectedListing’ field on the ‘Form Page’ with that particular data.

I already have a working php code for the ‘formHandler’ page.

I am sure I also need to some PHP in play here between these two pages, I just don’t know how to go about it.

Listing Page

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

Form Page

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

Solution

There’s no value attribute on an a, https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a. You can pass the value as a GET parameter on the URL. Change:

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

to:

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

then on userForm.php use:

$_GET['listing']

to read the value.

If the input is suppose to have the value you could do:

<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'] : ''); ?>">

Answered By – user3783243

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.