ajax modal form editing

Encountered an issue with editing a form using modal ajax, where the edit form pops up but the data remains empty. The code snippet for my controller:

public function edit()
{
$id=$this->uri->segment(3);

$data=array(
'project' => $this->m_inputactivity->get_project(),
'actype' => $this->m_inputactivity->get_actype(),
'detail' => $this->m_inputactivity->per_id($id),
'
);


$this->output
                ->set_content_type('application/json')
                ->set_output(json_encode($data));
}

For the view section:

<script type="text/javascript>
function edit(id)
{

    $.ajax({
        url : "<?php echo site_url('input_activity/edit/')?>" + id,
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {



            $("[name='actype']").val(data.actype);
            $("[name='activity_name']").val(data.activity_name);
$("[name='project']").val(data.project);
            $("[name='portion']").val(data.portion);
            $('#modal_form').modal('show'); // show bootstrap modal when complete loaded


        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error get data from ajax');
        }
    });
}

function reload_table()
{
    table.ajax.reload(null,false); //reload datatable ajax 
}
</script>
<a href="javascript:void(0)" title="Edit" onclick="edit('<?php echo $row->activity_detail_id;?>')"  class="btn btn-outline btn-circle btn-sm purple" >
<i class="fa fa-edit"></i> Edit </a>
In the modal view:

<! -- begin pop update activity -->
<div class="modal fade left" id="modal_form"> 
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="pull-left no-margin">Edit Activity</h3>
<button type="button" class="close" data-dismiss="modal" title="Close"><span class="glyphicon glyphicon-remove"></span>
</button>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" method="post" action="<?php echo base_url(); ?>input_activity/update">
<div class="form-group">
<label for="name" class="col-sm-3 control-label">Category:</label>
<div class="col-sm-9">
<?php $attributes = 'class = "form-control" id = "actype"';
echo form_dropdown('actype',$actype,set_value('actype',$detail[0]->activity_type),$attributes);?>
</div>
</div>
<div class="form-group">
<label for="activity" class="col-sm-3 control-label">Activity Details: </label>
<div class="col-sm-9">
<textarea class="form-control" rows="3" name="activity_name" required><?php echo $detail[0]->activity_name;?></textarea>
</div>
</div>
<div class="form-group">
<label for="project" class="col-sm-3 control-label">Allocation:</label>
<div class="col-sm-9">
<?php $attributes = 'class = "form-control" id = "project"'; 
echo form_dropdown('project',$project,set_value('project',$detail[0]->project_id),$attributes);?>
</div>
</div>
<div class="form-group">
<label for="portion" class="col-sm-3 control-label">Weight:</label>
<div class="col-sm-4">
<input class="form-control" name="portion" value="<?php echo $detail[0]->portion;?>" required>
<input name="activity_detail_id" type="hidden" id="activity_detail_id" value="<?php echo $detail[0]->activity_detail_id;?>">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-5">
<button type="submit" class="btn green">
<i class="fa fa-save"></i> Update</button>
<button type="button" class="btn default" data-dismiss="modal">Cancel</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</div>

Issue arises when clicking the edit button and the popup data remains empty.

https://i.sstatic.net/wtqWg.jpg

Any guidance on creating the modal ajax correctly?

Answer №1

If the controller is returning accurate data, then the issue may lie with the selectors for the form inputs.

Try using these selectors to assign the values:

$("input[name='actype']").val(data.actype);
$("input[name='activity_name']").val(data.activity_name);
$("input[name='project']").val(data.project);
$("input[name='portion']").val(data.portion);

It is assumed that the modal html is located in the same file as the javascript you provided, although the view html is not shown. Is this correct?

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

Is the NodeJS rmdir function experiencing issues specific to LINUX?

Currently, I have a NodeJS script section that runs on my Windows 10 machine with the latest Node version and another version on my CentOS8 Linux webserver. The code executes as expected on Windows but throws an error when running on Linux at this specific ...

What is the best way to retrieve data from an HTML form and transfer it to my controller class in .NET Core 6?

Currently, I am working on a project using .NET Core 6 where the goal is to extract text from a form and send it via email to a selected professor. Now, I am facing the challenge of retrieving specific information from my HTML form (specifically the text i ...

error encountered during module import in Ember.js

I am struggling with removing diacritics from a string in my Ember.js app. After discovering the 'fold-to-ascii' plugin on GitHub, I added it to my project using npm. Although the plugin is visible under the node_modules folder, I'm unsure o ...

jQuery is used to make an object fade out once another object has been moved into place

As I delve into the world of jQuery, I've decided to create a simple Super Mario imitation. The concept is to control Mario using arrow keys and fade out mushrooms once Mario reaches them. However, my attempts at achieving this result have not been su ...

What is the best way to fetch the title property from my Campaign Contract for displaying it in the render method?

I'm currently working on a unique crowdfunding DApp that requires constant access to contract variables through function calls for retrieval purposes. The getDeployedCampaigns function is responsible for returning an array of deployed campaign addres ...

Having issues with the custom listing feature in Jquery UI auto complete not functioning as expected

I am facing an issue with my implementation of jquery UI autocomplete. The autocomplete functionality is not working as expected, despite the absence of any JavaScript errors. Below is the JSON data that I am working with: {"d":"[{\"label\":&bs ...

The AJAX request is failing to send the most recent data for processing on the server side

Recently, I created a server-side processing script for datatables v1.10.0 that has been giving me some trouble. The server needs the product id to fetch records from the database, which it gets from a select2 plugin based selector selection. However, I ha ...

jQuery fails to modify HTML according to its intended purpose

I've been struggling to update a price using JQuery. Even though the code seems fine when I check the console, the HTML doesn't reflect the changes. Additionally, when I try to log console.log(newPrc), it gives an error saying "newPrc" is not def ...

Invoke jQuery within a nested context once more in the jQuery method sequence

Is it possible to do something like this? jQuery(selector).jQuery(subselector).command(....) Here is the current code snippet: $(' .species') .filter(function () { return !$(this).data('hidden_code_ready'); } .wrapInner('<spa ...

What is the best way to utilize RxJs with jQuery's ajax function?

Here is how I've set it up: Rx.Observable.fromPromise($.ajax({ url: url + postal_value + '&types=(regions)&location=51.509865,-0.118092&key=' + key, type: "GET", datatype: "json" })); However, the aj ...

Is it possible to make changes to local storage data without impacting the rest of the data set?

I am looking for a way to modify specific data in the local storage without affecting any other stored information. However, I have encountered an issue where editing values works correctly for the first three attempts, but on the fourth try, it seems to ...

Which is the better option for setting a title: using .prop or .attr?

I came across a comment that mentioned The suggestion was to utilize the .prop() method instead of .attr() when setting the "title" property in jQuery versions 1.6 or newer. Could someone provide an explanation for this recommendation? ...

A guide to retrieving all image URLs when a checkbox is selected using Javascript

My goal is to extract only image URLs from the concatenated values of price and picture URL. However, when I check different images using checkboxes, it always displays the URL of the first selected image. When I try to split the value, all the prices and ...

Issue with RegisterClientScriptCode function following a partial postback

In a SharePoint website, the code below is contained within a user control: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "jquery144", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Script ...

What could be causing my Discord bot to remain offline even when I run the command node main.js?

After successfully installing node.js, I proceeded to type the command 'npm init' in the command prompt and then installed discord.js. However, upon installation of discord.js, a 'node_modules' folder was not added inside the project, w ...

Clicking on a date in Vue.js Fullcalendar

My goal is to retrieve a date value from the onDateClick function of fullCalendar using vue.js and then pass this data to a prop that can be stored in my backend via Laravel. However, I am encountering various undefined errors no matter how I approach th ...

Ensure that all links are opened in a new tab

When including _blank in the a href URL, my website contains various elements like iframes and ads from Adsense, Taboola,, etc. Currently, when a user clicks on an iframe or ad, it opens in the same window. Is there a way to ensure that all URLs (includin ...

Create copies of Div elements for them to revert back to their original state following

It seems like a simple task, but I'm struggling to figure out how to do it. On a single page, I have multiple divs: <div id="playerid-1"><a href="javascript:loadplayer(1);">[LOAD PLAYER]</a></div> <div id="playerid-2">& ...

Understanding the rationale for rendering Views with vuex dependencies in vueJS

I'm facing an issue with my API call in App.vue that populates the vuex store's state. The Home.vue component displays images based on the store's state, but console errors are thrown before the state is populated. I'm unsure about the ...

At times, the Angular Modal Dropdown may unexpectedly open in an upward direction

Dealing with an AngularJS modal that contains a dropdown menu. The menu list is quite extensive. Most of the time, around 70%, the menu drops down in the lower direction which is fine. However, approximately 30% of the time, the dropdown menu appears in ...