Skip to content
Advertisement

Populating my input fields depending on what selected in dropdown using ajax

I want to populate my input fields which are texts depending on what I selected on my dropdown and I am using a procedural prepared statement for my back end. I can already retrieve the data from the the dropdown but I can’t populate the fields whenever I click/select on dropdown data

Problem: I’ve already tried it but it doesn’t change input fields whenever I try to change values in my dropdown menu. I tried doing console in network and click my backend code and it does not give me an error.

myforms.php

JavaScript

getdetails.js

JavaScript

includes_getDetails.php

JavaScript

Edit: Added reference // I hope this added information will help.

This is my database. enter image description here

This is the response enter image description here

Advertisement

Answer

You have set dataType: 'json' in your AJAX function and then, in the callback, call JSON.parse. You do not need to set the dataType – in fact leaving that in will likely cause an error as jQuery automagically processes the JSON data.

Consider the following demo – made with static values as the select menu is unknown and no way to test your db/sql.

JavaScript

Example output when dataType:'json' is removed

When dataType:'json'

Error caused by dataType-json

Looking at the response in the newly added screenshot it shows a simple boolean true rather than any structured data.

JavaScript

or, with the procedural style:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement