fill formfields based on dropdown selection database

fill formfields based on dropdown selection database

I have made a dropdown in a form with names taken from the database. That has succeeded. But now the intention is that on the basis of the choice of the dropdown below 3 fields will be filled in from the database table "ships". Can someone help me to achieve this.

Thank you for your help
add.php
Ron BaasDirecteurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
replace :
<select name="vervoermiddel">

Open in new window

by :
<select name="vervoermiddel" id="vervoermiddel">

Open in new window


and :
<div style="width: 500px; float: left;">

Open in new window

by :
<div style="width: 500px; float: left;" id="fields">

Open in new window


add a new page, api.php, like this ;
<?php
include "includes/config.php";

$q = "SELECT column1, column2, column3 FROM ships where ship_klant_id=" . $_GET["klant_id"];
$sth = $db->prepare($q);
$sth->execute();
header('Content-Type: application/json');
if($r = $sth->fetch(PDO::FETCH_ASSOC) )   echo '{"column1":"' . $r["column1"] . '","column2":"' . $r["column1"] . '","column3":"' . $r["column1"] . '"}';
?>   

Open in new window


and in your add.php page replace :
	<head>
		
		<link href="style4.css" rel="stylesheet" type="text/css" media="screen" />
	</head>

Open in new window

by :
	<head>
		
		<link href="style4.css" rel="stylesheet" type="text/css" media="screen" />
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script>
				jQuery(function($) {
						$("#vervoermiddel").change(function() {
						      $.getJSON("api.php?klant_id=" + $(this).val(), function(columns) {
						      		$("#fields input:eq(0)").val(columns["column1"]);
						      		$("#fields input:eq(1)").val(columns["column2"]);
						      		$("#fields input:eq(2)").val(columns["column3"]);
						      });
						});
				});
		</script>
	</head>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ron BaasDirecteurAuthor Commented:
Thanks a lot, I'll try it today
0
Ron BaasDirecteurAuthor Commented:
Thanks, it works fantastic
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.