How to retain old value in AJAX drop down using laravel - ajax

I am developing application in laravel 9.I have dependent dropdown in my application .
I have used ajax for dependent dropdown. It is working .But When I submit the form it is not showing selected option .It is showing "please select line"
I have used old() .But it is not working.
Here is my code
$("#downtimedatepicker").change(function(){
var downtimedate = $(this).val();
if(downtimedate)
{
$.ajax({
url:"{{ url('downtimeline') }}",
type : "POST",
data: {
downtimedate: downtimedate
},
dataType : "json",
success:function(data)
{
if(data){
$('select[name="line"]').empty();
$.each(data,function(key,value) {
$('select[name="line"]').append('<option value="'+ value +'" {{ old('line') == "'value'" ? 'selected' : '' }}>'+value+'</option>');
});
} else{
$("#line").empty();
}
}
});
}
});
Mycontrollercode
public function getdowntimeline(Request $request){
$downtimedate=$request->downtimedate;
$downtimelines=Downtime::where('date', $downtimedate)->distinct()->pluck('line', 'id')->unique();
return json_encode($downtimelines);
}
It should show selected option when I submit the form

Related

How to show blank if no search results found through Ajax?

I am trying to search similar posts through Ajax while typing in input. And I want to get the blank div if there are no results.
Here is my Ajax code I have tried:
$('#title').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{URL::to('similarpost')}}',
data:{'search':$value},
success:function(data){
if(data != null){
$('.mydiv').html(data);
}
else{
$('.mydiv').html(); //Here I want the div to be blank but it's not working
}
}
});
})
try like this, if you want the div to be blank, put like this $('.mydiv').html('');
$('#title').on('keyup',function(){
let $value = $(this).val();
$.ajax({
type : 'get',
url : '{{URL::to('similarpost')}}',
data:{'search':$value},
success:function(data){
if(data != null){
$('.mydiv').html(data);
}else{
$('.mydiv').html('');
}
}
});
})

jquery autocomplete is now working properly

I am working on jquery autocomplete in mvc platform. Now, my question is that in some textbox data autocomplete is working properly while in some textbox data autocomplete is not working properly.
For more clear, lets see the image of autocomplete task
now as per the image when I write the R word, I am getting the suggestion list of related R word.
now as per the second image I have written the whole word but still suggestion is not display.
Here is my code,
View
<input type="text" id="CustomerName" name="CustomerName" required data-provide="typeahead" class="typeahead search-query form-control autocomplete"
placeholder="Customer Name" />
<script>
$(document).ready(function () {
$.ajax({
url: "/ServiceJob/CustomerSerchAutoComplete",
method: "GET",
dataType: "json",
minLength: 2,
multiple: true,
success: function (data) {
/*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
//autocomplete(document.getElementById("CustomerName"), data.data);
$('#CustomerName').autocomplete({ source: data.data, minLength: 2, multiple: true });
}
});
});
</script>
Controller
[HttpGet]
public IActionResult CustomerSerchAutoComplete()
{
var customers = _Db.Ledger.Where(x => x.LedgerTypeId == (int)LedgerType.Customer).ToList();
var result = (from n in customers
select new
{
kk = n.Name.ToUpper()
}).ToList();
return Json(new { data = result });
}
As you are getting
Uncaught TypeError: Cannot read property 'substr' of undefined
For this error you should check that data is not null.
success: function (data) {
if(data != null)
{
autocomplete(document.getElementById("CustomerName"), data.data);
}
}

ajax success method not showing data in asp.net mvc

I am working on as asp.net application. Its view has a button like this:
<input type="button" id="btnCall" title="Call" value="Call" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" />
and in document.ready, I have this:
$("#btnCall").click(function () {
alert("here");
$.ajax({
type: "POST",
dataType: "text/json",
url: "/account/getGenericPassword",
success: function (data) {
alert("data" + data);
if (data == null || data == "") {
alert("Generic Password is empty. Please enter generic password");
}
else {
//saveCallRecording();
}
}
});
});
and method is like this:
[Authorize]
public JsonResult GetGenericPassword() {
using (IUserProfileManager profilemanager = new ManagerFactory().GetUserProfileManager()) {
UserProfile profile = profilemanager.GetProfile(CurrentUser.Id, CurrentAccount.Id);
return Json(profile.GenericPassword == null ? "" : profile.GenericPassword, JsonRequestBehavior.AllowGet);
}
}
but alert in success is not shown. Please suggest solution.
Try setting the dataType like so:
dataType: "json"
See the valid dataType options at http://api.jquery.com/jQuery.ajax/
try with these
type:'Post',
url:'#Url.Action("actionname","controller")',
datatype:"json",
data:{},
success:function(data){
}

Relevant data form_dropdown codeigniter

I am trying to post value using javascript in form_dropdown('') but it is not posting data on form.
Jquery library is loaded. On alert it display record of dep_selected
JQUERY On view page
function get_subdepartment() {
var dep_selected = $('select[name=txtDept]').val();
$.ajax({
data: {
dep_selected: dep_selected,
},
type: 'POST',
url: 'addVacancy/getSubDept',
success: function(data){ //alert(dep_selected);
console.log(data);
$('.subdepartment').html(data);
}
})
}
Controller Page:
function getSubDept(){
if($this->input->post('txtDept')){
echo "getSubDept > Dept: ".$this->input->post('txtDept');
}
else{
echo "getSubDept > No Return";
}
}
It display "getSubDept > No Return" Please help.
Well it's obvious, that in your controller you are requesting a post value by the key txtDept, but you don't have that when you send data with AJAX. What you have now is:
var dep_selected = $('select[name=txtLocation]').val();
and
data: {
dep_selected: dep_selected,
},
So you should simply change the first line in your controller method to
if($this->input->post('dep_selected')) {
I suggest you to do some reading about POSTing values with AJAX and how to work with JSON.

How to retrieve CheckBox value in Controller - Asp.Net MVC 3 and Jquery

Problem: Check box value display null in controller.cs. but it is working perfectly according to selection of row from jqgrid. But when I select any row and update all the field it will pass to the controller with modified value but only IsEnabled field comes null.
I have Database Field called IsEnabled which has Bit data type.
I have written following code in .cshtml
<input type="checkbox" value='Yes' offval='No' name="IsEnabled" />
I am using following code to bind check box value as per in database
grid.jqGrid('GridToForm', gsr, "#order");
I have save button. When I click on save following code will execute
$("#btnSave").click(function () {
var data = JSON.stringify($('#order').serializeObject());
var href = '#Url.Action("SaveData", "Users")';
var ajaxResponse = $.ajax({
type: "post",
url: href,
dataType: 'json',
data: data,
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.Success == true) {
alert("Success");
}
else {
alert("Error: " + result.Message);
}
}
});
Following code written in Controller.cs
(in FormValue it will show all the updated value correctly except IsEnabled, it will display always null.)
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveData(User FormValue)
{
string message = "";
return Content(message);
}
You must delete this code ( if use jquery ajax )
[AcceptVerbs(HttpVerbs.Post)]
when you use post back and send class use above code
send class with jQuery use this code:
try it:
$.ajax({
type: "post",
url: href,
dataType: 'json',
data: SON.stringify({ FormValue : { ID : $('#controlIdName').val() , Name : $('#ControlName').val() } }),
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.Success == true) {
alert("Success");
}
else {
alert("Error: " + result.Message);
}
}

Resources