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?
 
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
 
Ron BaasDirecteurAuthor Commented:
Thanks a lot, I'll try it today
0
 
Ron BaasDirecteurAuthor Commented:
Thanks, it works fantastic
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.