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

Time:05-25

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 >

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

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

CodePudding user response:

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"  name="selectListing" placeholder="Selected 
Listing" value="<?php echo (!empty($_GET['listing']) && in_array($_GET['listing'], array('A', 'B', 'C')) ? $_GET['listing'] : ''); ?>">
  • Related