Auto fill multiple text fields using Ajax and jQuery - php

I have a form, which I have divided in to there parts using tabs, in my second tab I'm using a select option and then try to auto fill 2 text box's and one text area using Ajax ,but although console log shows that ajax return the object but it doesn't fill the required fields.
tab
<div class="tab-pane fade " id="package_details">
select option
<select name="package" id="packageid" >
<option selected > Select a Package </option>
#foreach($package as $c)
<option value="{{$c->id}}">{{$c->tour_name}}</option>
#endforeach
</select>
Text fields i need auto filling
<input type="number" name="no_of_days" id="no_of_days" class="form-control"
placeholder="Days" >
<input type="number" name="cost" id="cost" class="form-control"
placeholder="Price" >
<textarea rows="4" cols="50" class="form-control" id="description" name="description" placeholder=" Package Details" ></textarea>
my Ajax code
$(document).on('change', '#packageid', function(e) {
e.preventDefault();
var pkid = $(this).val();
$.ajax({
type:'POST',
url: "{{ route('package.tour') }}",
dataType: "json",
data:{
'_token':$('input[name=_token]').val(),
'selectedid': pkid
},
success: function(data){
// console.log(data);
$('#description').val(data.description);
$('#no_of_days').val(data.no_of_days);
$('#cost').val(data.cost);
}
});
});
Function in my controller
public function getPackage(Request $request)
{
$data = packages::where('id', $request->selectedid)->get();
return response()->json($data);
}
what is wrong with my Ajax / JQuery code ?

check using like this in ajax response :
$("textarea#description").val(data.description);
$("input#no_of_days").val(data.no_of_days);
$("input#cost").val(data.cost);

I finally figured out ,since response is an array i had to use $("#description").val(data[0].description); and it works !

Related

ajax output in textbox is not available in post method of submit

I have a select dropdown, from that I'm calling an ajax function onchange event. Posting the result in a text input. This much is working perfectly. But when I submit the form the text input value is not submitting.
This is the ajax code. teachername dowpdown and teachercode text input
$("#teachername").change(function(){
var name = $(this).val();
var dataString = "name="+name;
$.ajax({
type: "POST",
url: "get-teachercode.php",
data: dataString,
success: function(result){
$("#teachercode").val(result);
}
});
});
this is the form
<form action="insert-assign-classes.php" method="post">
<select class="form-control" name="teachername" id="teachername" required>
</select>
<input type="text" disabled style="background-color:#fff;" class="form-control" name="teachercode" id="teachercode" required>
getting the value in insert-assign-classes.php file.
$teachercode= $_POST['teachercode'];//not working

Populating data in a text box on selection of dropdown using ajax

I am trying to fetch a row on a selection of a particular ID, now when the user selects any value of a drop-down the respective values have to come into text boxes, I am using ajax for the same,I am getting output from my PHP file But they are in array form like ("rahul", "9594233709"). When I am trying to put them in there respective fields I am not able to do it.
Here is my complete code. I am trying to fetch a row on a selection of a particular ID, now when the user selects any value of a drop-down the respective values have to come into text boxes, I am using ajax for the same,I am getting output from my PHP file But they are in array form like ("rahul", "9594233709").
When I am trying to put them in their respective fields I am not able to do it.here is my complete code. I am expecting That location has to move in the location column and mobile has to be in Mobile column, Array is coming perfectly in the front end. For example: ["9594233705", "Gorakhpur"]
<script >
$(document).ready(function(){
$('#reg_number').change(function(){
var reg_number = $(this).val();
$.ajax
({
type: "POST",
url: "details.php",
data: {post_id:reg_number},
dataType: "text",
cache: false,
success:function(data)
{
$('#u_mobile1').val(data);
$('#u_location1').val(data);
}
});
});
});
</script>
if($_POST['post_id'])
{
$id=$_POST['post_id'];
$sql="SELECT u_mobile,u_location FROM system_users WHERE u_userid=".$id;
$result = mysqli_query($con,$sql);
$data = array();
while($row = mysqli_fetch_array($result))
{
$u_mobile = $row['u_mobile'];
$u_location = $row['u_location'];
$data[0]=$u_mobile ;
$data[1]= $u_location ;
// $data[] = array('u_mobile' => $u_mobile, 'u_location' => $u_location);
} echo json_encode($data);
}
<div class="col-md-6">
<div class="form-group form-group-default required">
<label>Company Mobile Number:</label>
<input type="number" class="form-control" required="" id="u_mobile1" name="u_mobile1" value="" >
</div>
</div>
<div class="col-md-6">
<div class="form-group form-group-default required">
<label class="">Location</label>
<input type="number" class="form-control" required="" id="u_location1" name="u_location1" value="">
</select>
</div>
</div>
if i didn't get it wrong, you want to parse response Json data and add to inputs.
If this is the case you can use javascript JSON.parse(); function. Because you getting data as a json list.
$.ajax
({
type: "POST",
url: "details.php",
data: {post_id:reg_number},
dataType: "text",
cache: false,
success:function(data)
{
var resp = JSON.parse(data);
document.getElementById("u_mobile").value=resp.u_mobile;
document.getElementById("u_location").value=resp.u_location;
}
});

How to pass a value from input-text through Jquery Ajax to php function while I type? Wordpress

I am using Wordpress, and here is my html code:
<input class="form-control text-right" name="majorhead" required="" type="number" id="majorhead"/>
<div class="result_majorhead"></div>
JS code:
<script>
jQuery("#majorhead").on("keyup",function(){
jQuery.get(ajaxurl,{'action': 'majorhead'},
function (msg) { jQuery(".result_majorhead").html(msg);
});
});
</script>
Here is my code in function.php
add_action('wp_ajax_nopriv_majorhead', 'majorhead_function');
add_action('wp_ajax_majorhead', 'majorhead_function');
function majorhead_function(){
echo 'hello';
exit();
}
For now I am able to display a simple message "Hello" in the same web page whatever I type in the input box. Now I want to display the exact value from the input-text in the display message while typing.
for example: if I type 1, it should display 1 and If I type another another 2 then it should display 12.
What you need to do in your Javascript is:
Add "change" event so that you could use the arrows to control the "number" input.
Get the value of the input.
Add the value to your GET parameters.
jQuery("#majorhead").on("keyup change",function(){
var majorhead_value = $(this).val();
jQuery.get(ajaxurl,{'action': 'majorhead','majorhead_value': majorhead_value},
function (msg) { jQuery(".result_majorhead").html(msg);
});
});
In your PHP script you should display the GET value:
function majorhead_function(){
echo $_GET['majorhead_value'];
exit();
}
If you have multiple inputs you may process them like this:
jQuery("#text1, #text2, #text3").on("keyup change",function(){
var majorhead_value = parseInt(0+$("#text1").val());
majorhead_value += parseInt(0+$("#text2").val());
majorhead_value += parseInt(0+$("#text3").val());
jQuery.get(ajaxurl,{'action': 'majorhead','majorhead_value': majorhead_value},
function (msg) { jQuery(".result_majorhead").html(msg);
});
});
I assume that your HTML will look like this:
<input class="form-control text-right" name="text1" required="" type="number" id="text1"/>
<input class="form-control text-right" name="text2" required="" type="number" id="text2"/>
<input class="form-control text-right" name="text3" required="" type="number" id="text3"/>
<div class="result_majorhead"></div>
HTML CODE
<input class="form-control" placeholder="Search Service" id="search_service" name="service" type="text" autocomplete="off" value="">
Here is my Ajax Function in function.php
function search_service() {
$search_ser = $_POST['search_ser'];
echo $search_ser;
die();
}
add_action('wp_ajax_search_service', 'search_service');
add_action('wp_ajax_nopriv_search_service', 'search_service');
JS Code:
jQuery( "#search_service" ).keyup( function() {
var search_ser = jQuery(this).val();
var ajax_url = jQuery('#ajax_url_input').val();
jQuery.ajax({
type:'POST',
url: ajax_url,
data: {
action: 'search_service',
search_ser: search_ser,
},
beforeSend: function(){
},
success:function(data){
jQuery("._services_list").html(data);
}
});
});

Ajax autocomplete input field unfounded data aren't sending

I have a problem. I used Ajax autocomplete for input field and fetch my listed data form database. Its worked nicely but I want when not found data then input field not send any data to sql.
<input type="text" name="country" id="country" class="form-control" placeholder="Enter Country Name" />
<div id="countryList"></div>
Ajax
$(document).ready(function(){
$('#country').keyup(function(){
var query = $(this).val();
if(query != '')
{
$.ajax({
url:"search.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#countryList').fadeIn();
$('#countryList').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#country').val($(this).text());
$('#countryList').fadeOut();
});
$(document).on('click', 'body', function(){
$('#countryList').fadeOut();
});
});
You can add the required parameter in HTML to make a field required. This way, you can't submit the form before you haven't filled in something in this field.
<input required
type="text" name="country"
id="country" class="form-control"
placeholder="Enter Country Name">

php - Laravel : how to Load Ajax data after selected changes from dropdown?

I want to load some ajax data after selected a value from dropdown in textbox.
Eg: After selecting a teacher from dropdown, teachers remaining credit and credit_taken value should be load. How do I do it with ajax?
NB: Here teacher value has selected from another dropdown selecting in Ajax
<script>
$('#teacher').on('change',function(e){
var teach_id = $('#teacher option:selected').attr('value');
var info=$.get("{{url('ajax-teach')}}",{teach_id:teach_id});
info.done(function(data){
$.each(data,function(index,subcatObj){
/// Here will be the loaded code ///
});
});
info.fail(function(){
alert('ok');
});
});
</script>
Here is my controller:
Route::get('ajax-teach',function(Request $request){
$teach_id = $request::input(['teach_id']);
$teachers=\App\Teacher::where('teacher_id','=',$teach_id)->get();
return Response::json($teachers);
});
Here is the view Page:
<div class="form-group">
<label for="">Teacher</label>
<select class="form-control input-sm" required name="teacher_id" id="teacher" >
<option value=""></option>
</select>
</div>
<div class="form-group">
<label>Credit to be Taken</label>
<input type="text" name="credit_taken" id="credit_taken" class="form-control" required placeholder="Credit to be Taken">
</div>
<div class="form-group">
<label>Remaining Credit</label>
<input type="text" name="remaining_credit" class="form-control" required placeholder="Remaining Credit">
</div>
In your routes (routes/web.php)
Route::get( '/ajaxteach', array(
'as' => 'ajaxteach',
'uses' => 'TeachController#get_teach'
) );
In your controller (app/Http/Controllers/TeachController.php)
class TeachController extends Controller
{
public function get_teach(Illuminate\Http\Request $request)
{
$teach_id = $request->get('teach_id');
$teachers=\App\Teacher::where('teacher_id','=',$dept_id)->get();
return response()->json(['response' => $teachers]);
}
}
Then in your AJAX script you can use at least one of the following ways
use absolute url in javascript code
var base_url = 'http://localhost/laravel'
$.ajax({
type: "GET",
url : base_url+"/ajaxteach",
data : dataString,
success : function(data){
console.log(data);
}
use url() with route
$.ajax({
type: "POST",
url : "{{url('ajaxteach')}}",
data : dataString,
success : function(data){
console.log(data);
}

Categories