Steps for extracting an Ajax value from a form that contains an array value

My form input contains an array of values, from which I need to extract a value for use in Ajax and JavaScript code to populate the next form. However, my current code is not running as expected and no errors are being displayed.

<table>
  <form>
<?php
 for($i=1;$i<=$jml;$i++){?>
   <tr>
   <td>Jenis CI</td>
   <td><select class="form-control" name="jenis2[]"id="jenis2[]" required>
       <?php foreach($relasi as $row) { ?>
       <option value="<?php echo $row->idJenisCI;?>"><?php echo $row->namaJenisCI;?> 
       </option><?php } ?>
       </select>
   </td>
   </tr>
   <tr>
   <td>Nama CI</td>
   <td><select  class="id2 form-control" name="id2[]" required></select></td>
   </tr>
<?php }?>
   <tr align="center">
   <td colspan="2"><input class="tombol" type="submit" name="submit" value="Simpan"></td>
   </tr>

 </form>
</table>

Here is my JavaScript and AJAX code:

<script type="text/javascript">
    $(document).ready(function(){
        $('#jenis2').change(function(){
            var idJenisCI=$(this).val();
            $.ajax({
                url : "<?php echo base_url();?>index.php/C_ProyekAkhir/option",
                method : "POST",
                data : {idJenisCI: idJenisCI},
                async : false,
                dataType : 'json',
                success: function(data){
                    var html = '';
                    var i;
                    for(i=0; i<data.length; i++){
                        html += '<option value="'+data[i].idCI+'-'+data[i].namaCI+'">'+data[i].namaCI+'</option>';
                    }
                    $('.id2').html(html);

                }
            });
        });
    });
 </script>

Answer №1

<script type="text/javascript">
    $(document).ready(function(){
        $('#jenis2').change(function(){
            var idJenisCI=$(this).val();
            $.ajax({
                url : "<?php echo base_url();?>index.php/C_ProyekAkhir/option",
                method : "POST",
                data : {idJenisCI: idJenisCI},
                async : false,
                dataType : 'json',
                success: function(data){
                    var decode_response = JSON.parse(data); // Update - Include this new line for decoding response
                    var html = '';
                    var i;
                    for(i=0; i<data.length; i++){
                        html += '<option value="'+data[i].idCI+'-'+data[i].namaCI+'">'+data[i].namaCI+'</option>';
                    }
                    $('.id2').html(html);

                }
            });
        });
    });
 </script>

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

The process of transferring ViewBag value as a parameter in an AngularJS function

I'm facing an issue where the viewbag value is not being passed as a parameter in ng-init. Can someone guide me on how I can successfully pass the viewbag value as a parameter? angular.js { $scope.detail = function (Id) { ...

What is the method for retrieving a variable from a Q Node promise?

I am currently developing a learning game utilizing Angular, Express, MongoDB, and Node. As part of my learning process, I have been exploring the use of promises to manage asynchronous operations effectively. One particular challenge I am facing involves ...

Experiencing problems with the calling convention in JavaScript

Here is a snapshot of the code provided: If the function testFields at Line:3 returns false, then the program correctly moves to Line:21 and returns the value as false. However, if testFields returns true, the program proceeds to Line:4, but instead of ex ...

Using PHP to output a string within a JavaScript onclick function parameter

I am attempting to display a string in PHP as a string variable within a JavaScript function, but it is not appearing correctly. How can I resolve this issue? <?php $example1 = "example 1"; $example2 = "example 2"; $examp ...

Unable to update to the most recent version of React-Bootstrap

I'm facing an issue while trying to upgrade to the newest version of react-bootstrap. When I run: npm install --save react-bootstrap The following message appears: npm notice created a lockfile as package-lock.json. You should commit this file. npm ...

Max value determined by a variable within a for loop

UPDATE: Revised code provided. In my Node.js JavaScript project, I am creating a script to iterate through a dataset obtained by web scraping. The main goal of this algorithm is: 1) Examine the player table for the presence of the player's name in a ...

What is the process for integrating TypeScript compiling into a JavaScript application?

My project includes a build.js file that is responsible for building the project. It has two main objectives: Compile .ts files and store them in a new directory. Create an asar archive containing the compiled files. Since typescript (or tsc) is availabl ...

Tips for keeping MUI Autocomplete open even when the input field loses focus

I am currently working with a MUI autocomplete feature that includes a list of items and an edit icon next to each one. The edit icon allows the user to change the name of the option by rerendering it as a textfield. However, I have encountered an issue wh ...

Something is seriously wrong with the datetime in fullcalendar JavaScript

I've been diving into a tutorial for creating a calendar scheduler in asp.net MVC5 from this link. One issue I'm facing is the datetime being passed and stored as the min value in the database (1/1/0001 12:00:00 AM), almost like it's null b ...

Highcharts experiencing difficulty in removing labels

How can I remove labels added to highcharts when a button is clicked, as they still remain visible? For reference, please check out the fiddle: https://jsfiddle.net/7pq3po3o/3/ HTML: <script src="https://code.highcharts.com/highcharts.js"></sc ...

What is the best way to invoke a custom hook that returns a value within the useEffect hook?

I have a unique situation where I am using a custom hook that returns an object based on the parameter it receives. I now need to modify this behavior by recreating the object with updated parameters within the useEffect function. The challenge is that I c ...

The upload method in flowjs is not defined

I am a novice when it comes to flow.js and am currently using the ng-flow implementation. I have a specific task in mind, but I'm unsure if it's feasible or not, and if it is possible, how to achieve it. I've created a factory that captures ...

JavaScript: Collection of Pictures

I've recently begun my journey into HTML and JavaScript by working on a basic website that showcases four images using a for loop. However, upon viewing the website in a browser, I noticed that only the names of the images are displayed, not the actua ...

Generate a custom image using a pre-existing background image and text using JavaScript, then save it as a JPEG file in

Managing multiple fanpages on Facebook can be quite a task, especially when it comes to posting images with funny text. I have different backgrounds for each page, which means I have to save each image individually with the text. To streamline this process ...

Tips for simulating a service in Angular unit tests?

My current service subscription is making a promise: getTaskData = async() { line 1 let response = this.getTaskSV.getTaskData().toPromise(); line 2 this.loading = false; } I attempted the following approach: it('should load getTaskData', ...

Implementing the if/shim binding in Knockout.js

In my project, I am looking to develop a unique custom binding that functions similarly to the if binding. However, instead of completely removing the element from view, I want it to be replaced with another element of equal height whenever it needs to be ...

What does the buffer.copy() method do in Node.js?

Can someone please discuss the purpose and functionality of buffer.copy() in a Node.js application? Provide a real-time example for better understanding. Additionally, I am curious about the differences between the copy and slice methods in Node.js. How ...

jQuery dynamically updating calculations on a single row consecutively

Currently, I am in the process of developing a small table to calculate potential winnings from betting on a rubber duck race with specific odds for each duck. I have managed to get most of it working but have encountered an issue... Upon loading the pag ...

Chrome is experiencing a problem with anchor tags that have an href attribute set to a "blob:" URL and using a target of "_blank"

My current project involves developing a website feature that allows users to download a PDF version of a page. To achieve this, the generated HTML is rendered into a PDF on the server, which is then returned as a Base64-encoded PDF. This data is converted ...

Tips for loading face-api.js shard models in a cloud-based setting with AWS Amplify?

Currently, I am exploring the functionality of the face-api.js library for emotion detection on a live webcam stream. While it works flawlessly on my local setup, I encounter an error when attempting to deploy it on AWS Amplify. The error message reads: Un ...