When inputting forms into the database, identifiers are used instead of names. Despite this, the user interface functions without any issues

In this demonstration, I am building a dynamic conditional drop-down list using Java script and Ajax.

PHP: Add Data

<html>
<head>
    <title>Add Data</title>
</head>

<body>
<?php
//including the database connection file
include_once("config.php");

if(isset($_POST['Submit'])) {   
...

HTML: Create Data

<html>
    <head>
        <title>Create Data</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    </head>
    
    <body>
        <a href="index.php">Back to Home</a>
        <br/><br/>
    
        <form action="add.php" method="post" name="form1">
            <table width="25%" border="0">
                <tr> 
                    <td>Name of the Request</td>
...

Javascript:

$(document).ready(function(){

load_json_data('type');

function load_json_data(id, parent_id)
{
 var html_code = '';
 $.getJSON('options.json', function(data){
...

JSON DATA:

[
     ...

The form submission process currently saves the IDs instead of the corresponding names into the database from the JSON data.

For example,

{
 "id":"10",
 "name":"bus Archive",
 "parent_id":"2"
}

In the above scenario, the ID 10 gets stored in the database rather than the actual item name bus Archive.

Answer №1

Consider updating your JavaScript function to the following code snippet:

function fetch_json_data(identifier, parent_identifier) {
    var html_content = "";
    $.getJSON("data.json", function (info) {
      html_content += '<option value="">Choose ' + identifier + "</option>";
      $.each(info, function (index, item) {
        if (identifier == "category") {
          if (item.parent_id == "0") {
            html_content +=
              '<option value="' + item.name + '">' + item.name + "</option>";
          }
        } else {
          if (item.parent_id == parent_identifier) {
            html_content +=
              '<option value="' + item.name + '">' + item.name + "</option>";
          }
        }
      });
      $("#" + identifier).html(html_content);
    });
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to retrieve PHP variables in jQuery Ajax syntax

I have a custom PHP function to send messages: <?php function sendMessage($userId, $projectId, $message) { //some code } $userId = '1'; $projectId = '2'; $message = 'hello'; ?> <form id="myForm" method="post" ac ...

Obtain a report using a variety of different conditions

My database has a table with the following fields: TPI CLICKS IMPRESSION CLASSIFY I am looking to retrieve 3 specific results: 1) Calculate SUM(CLICKS)/SUM(IMPRESSION) * 100 GROUPED BY TPI 2) Calculate SUM(IMPRESSION) WHERE CLASSIFY = "XYZ" GROUPED BY ...

Comparison of various nodejs scripts

Snippet One net.createServer(function(socket){ socket.on('data',function(id){ getUserDetails(function(){console.log(id)}); }); }); function getUserDetails(next){ next(); } Snippet Two net.createServer(function(socket){ ...

Display a hyperlink in an iframe on the main page from a different domain using JavaScript

I'm currently using Angular with Wirecard as my payment provider. When I need to add a payment, I open an iframe that directs the user to the Wirecard site to accept the payment. Once the user clicks accept, I provide a link back to my site from Wirec ...

Enhancing the appearance of dropdown menus for WooCommerce Variable products with custom CSS styling

I currently have a Wordpress website with WooCommerce and several variable products. Each product's variation has a dropdown menu that displays different options when clicked. My main concern is how to customize the appearance of these dropdown menus ...

Steps to generate a script that tracks the number of visible divs and dynamically updates when a user applies a filter

I'm facing challenges in developing a script that can count and display the number of divs selected by the user compared to the total number of available divs. If you'd like to see the whole problem, check out this JSFiddle: http://jsfiddle.net/ ...

I am looking to retrieve a sophisticated/nested JSON data using jQuery

I need some assistance in fetching specific JSON object data. Specifically, I am looking to extract the name, poster image URL, size of the second backdrop image, and version number. As a newcomer to JSON, I was wondering if there is an easy way for me to ...

What steps should I follow to ensure that the processData function waits for the data returned by the getData function in Angular?

After calling the getData function, each object is stored in an array and returned. Now I need the processData function to await these results from getData and then further process them. However, when I try to console.log(cleaningData), I don't see an ...

I am consistently encountering issues with the ListView Builder returning null values

I am facing an issue with the following code snippet: class HonPage extends StatefulWidget { final Status? status; HonPage({Key? key, this.status}) : super(key: key); @override _HonPageState createState() { return _HonPageState(); } } clas ...

Troubleshooting Angular 2 Fallback Route Failure

My current project is using Angular 2 Webpack Starter but I am having trouble with the fallback route. In my app.routes.ts file, I have defined the routes as follows: import { Routes } from '@angular/router'; import { HomeComponent } from &apos ...

Guide to automatically updating a table with AJAX requests

My task involves utilizing AJAX to request a random string consisting of 10 numbers from an asp page. The numbers must be delimited by "||" and displayed in a table format. The table is designed to showcase only the top 10 results, with each new row addin ...

Having trouble performing a PUT request using AngularJS $http.put? Getting an error message saying "Invalid JSON

Recently, I have developed a service with the following functionality: app.factory('savedPropertiesService', ['$http', function ($http) { var sessionId = $('input[name=SessionGuid]').val(); var contactId = $('inp ...

Converting JavaScript objects into a JSON string and then into a PHP array via POST

Hello everyone, I could really use some assistance with this issue. I am passing a JSON object to PHP like so: var x = {}; x.xt = {}; x.xt.id = id; x.xt.to = foo; somearray.push(x); To convert the object to JSON: $.toJSON(x); The resulting JSON string ...

Enhance your data table by updating the content without the need to refresh the entire page in CodeIgniter

Every time I update or insert data using the bootstrap modal form and ajax, the entire page reloads. Instead of refreshing the whole page, I want only the data table to be refreshed. Below is my ajax script: <script> $(document).ready(function( ...

JQ: Substituting key values with their corresponding counterparts in a separate file

In the process of updating my original JSON, I am attempting to substitute the value of a key with the corresponding key's value from another document. These are the contents of my two files: File 1: { "-KaM0otlgWxXniYiacFe": { "-LNxx1Ii ...

Bring in a template from a URL using Angular and then compile it within a div

Being a beginner in Angular JS, I am curious about how to load an external template and compile it with specific data into a targeted div. Here is the sample template: <script type="text/ng-template"> <img src="{{Thumb}}" /> <script& ...

Having trouble with the auto-complete feature in the search box when using Jquery

I'm currently working on implementing TypeAhead functionality for a search textbox. Within the form, I have 2 radio buttons and if one of them is selected, I need the type-ahead feature to populate the list of masters in the search box. //html < ...

Is there an array containing unique DateTime strings?

I am dealing with an Array<object> containing n objects of a specific type. { name: 'random', startDate: '2017-11-10 09:00', endDate: '2017-11-23 11:00' } My goal is to filter this array before rendering the resu ...

Tips for efficiently printing invoices on specific paper: Print a maximum of 20 items per sheet, and if it exceeds this limit, continue onto the next page. Ensure the total amount is

$(document).ready(function(){ var j = 23; for (var i = 0; i < j+11; i++) { if (i != 0 && i % 11 == 0) { $("#printSection div").append("<?php echo '<tr><td>fff</td></tr>'; ?>"); ...

Having trouble making the child process die in NodeJS fork

I'm facing a roadblock here, perhaps it's just a minor issue that I can't seem to solve because of my lack of experience with NodeJS. Currently, I am developing a Bluetooth device that will be controlled by a master application. For prototy ...