Select2 remote source on dropdown field - php

I have a city field, where on inlinedit call it will load ajax data on dropdown list. Please check my code & let me know where am I wrong. I read the select2 documentation
<script type="text/javascript">
jQuery(function($) {
$('#city_id').editable({
type: 'select2',
name: 'otmp_tx_user_details:city_id',
pk:"userdetailid:<?php if($student_info->userdetailid) echo $student_info->userdetailid; else echo "0";?>",
ajax: {
url: "<?php echo site_url()?>students/get_city_by_country",
dataType: 'json',
data: function () {
return;
},
results: function (data) {
return {results: data};
}
},
url: "<?php echo site_url();?>students/inlineedit",
success: function(data) {
}
});
});
</script>
Here is my ajax data from PHP file:
$array = array(
array("id"=>1,text=>"Dhaka"),
array("id"=>2,text=>"Pabna")
);
echo json_encode($array);
Please help me to solve my problem.

Try wrapping your Ajax object inside a select2 object, like so:
jQuery(function($) {
$('#city_id').editable({
type: 'select2',
name: 'otmp_tx_user_details:city_id',
pk:"userdetailid:<?php if($student_info->userdetailid) echo $student_info->userdetailid; else echo "0";?>",
select2: {
ajax: {
url: "<?php echo site_url()?>students/get_city_by_country",
dataType: 'json',
data: function () {
return;
},
results: function (data) {
return {results: data};
}
}
},
url: "<?php echo site_url();?>students/inlineedit",
success: function(data) {
}
});
});

Related

Function doesn't echo variable

I'm currently learning basic php and jQuery.
I've created script which is getting url on mouse hover, and sends it to php.
The problem is, if I want to pass this data to php variable, it seems like it doesn't work because it echos only "'This is our JS Variable :'"
Script:
<script type="text/javascript">
var hrefValue;
jQuery(document).ready(function($) {
$('#bio-box').find('a').mouseover(function() {
hrefValue = ($(this).attr('href'))
console.log(hrefValue)
});
$.ajax({
url: 'jakubtrz-portfolio/wp-admin/admin-ajax.php',
data: {
'action': 'php_tutorial',
'php_test': hrefValue
},
success: function(data){
console.log("happy")
}
});
});
</script>
functions.php:
function our_tutorial(){
if(isset($_REQUEST)){
$testing = $_REQUEST['php_test'];
echo 'This is our JS Variable :'.$testing;
global $wpdb;
$wpdb->insert(
$wpdb->prefix.'lms_enroll',
[
'ID' => $testing
]
);
}
die();
}
add_action('wp_ajax_php_tutorial', 'our_tutorial');
Solution:
$.ajax({
url: 'jakubtrz-portfolio/wp-admin/admin-ajax.php',
type: 'post', // define type
data: {
'action': 'php_tutorial',
'php_test': hrefValue
},
success: function(data){
console.log("happy")
}
});
functions.php:
// post to et value
$test = $_POST['php_test'];
The problem was - when page loaded, value of a variable was empty. So solution is to call ajax in the moment of mouseover.
var hrefValue;
jQuery(document).ready(function($) {
$('#bio-box').find('a').mouseover(function() {
hrefValue = ($(this).attr('href'))
//console.log(hrefValue)
$.ajax({
url: frontendajax.ajaxurl,
type: 'POST',
data: {
'action': 'image',
'php_test': hrefValue
},
success: function(data){
$('#featured-image').html(data);
//console.log(data);
}
});
});
});

Wordpress ajax request not working properly

I am new to wordpress, i make a ajax request on click button and it print the data, but ajax is not giving me any response. Please help me to find out the error.
Here is my code
add_action("wp_ajax_delivery_options", "delivery_options");
add_action("wp_ajax_nopriv_delivery_options", "delivery_options");
function delivery_options()
{
echo json_encode(array('type' => 'success'));
wp_die();
}
wp_enqueue_script("my-ajax-handle", get_stylesheet_directory_uri() . "/js/custom.js", array('jquery'));
wp_localize_script('my-ajax-handle', 'the_ajax_script', array('ajaxurl' => admin_url('admin-ajax.php')));
Ajax
(function($) {
$(document).ready(function() {
$('#delivery_option button').on('click', function(e) {
e.preventDefault();
var data = e.currentTarget.id;
$.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: { delivery_option: data },
success: function(response) {
console.log(response);
}
});
});
});
})(jQuery);
Any solution appreciated!
you have to pass "callback function name" in data: { action: 'delivery_options', delivery_option: data },
(function($) {
$(document).ready(function() {
$('#delivery_option button').on('click', function(e) {
e.preventDefault();
var data = e.currentTarget.id;
$.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: { action: 'delivery_options', delivery_option: data },
success: function(response) {
console.log(response);
}
});
});
});})(jQuery);

Json encode error in codeigniter

I tried to load a view in my codeigniter controller and also i pass a status to my ajax function. In the ajax function i wrote a sweetalert popup in the sucess function when my status is 1. now my actual problem is when i load a view in the controller the status is not passing to my ajax function.can anyone figureout what the problem is any help is appreciable.
controller
$query = $this->package_view_model->enquiry_history();
if (isset($query))
{
$status = 1;
$this->load->view('packages/enquiry');
}
echo json_encode (array("status"=>$status));
ajax function
function sendMail(package_url)
{
var formData = $("#enqform").serialize();
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url() ?>tour-package/send-mail',
data: formData,
dataType: 'json',
success: function(data){
if(data.status == 1){
swal({
title:'Thankyou for your interest!!!',
text: "Our excecutives will contact you soon.",
html: true,
type: "success",
showCancelButton: false,
showConfirmButton:false
});
window.setTimeout(function() {
window.location.href = '<?php echo base_url() ?>tour-packages';
}, 1000000);
}
}
});
return false;
e.preventDefault();
}
Hope this will help you :
$query = $this->package_view_model->enquiry_history();
$status = 1;
if (! empty($query))
{
$this->load->view('packages/enquiry');
}
echo json_encode(array("status" => $status));
exit;
I think there is a problem with the ending slash in the baseurl. So, change the way you're generating the url.
function sendMail(package_url)
{
var formData = $("#enqform").serialize();
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url("tour-package/send-mail") ?>',
data: formData,
dataType: 'json',
success: function(data){
if(data.status == 1){
swal({
title:'Thankyou for your interest!!!',
text: "Our excecutives will contact you soon.",
html: true,
type: "success",
showCancelButton: false,
showConfirmButton:false
});
window.setTimeout(function() {
window.location.href = '<?php echo base_url() ?>tour-packages';
}, 1000000);
}
}
});
return false;
e.preventDefault();
}

How to insert data into db using checkbox using ajax and php without form

I have a checkbox when checked on it i want to enter the value of the checkbox into the database.Please help me to find the Ajax and php code for this.
I tried this
$(document).on('click', "input[type='checkbox']", function() {
var checkbox = $(this);
var checked = checkbox.attr('checked');
$.ajax({
url:"<?php echo base_url("contact-details-availability"); ?>",
type: 'post',
data: {
action: 'checkbox-select',
id: checkbox.attr('contact_avl'),
checked: checked
},
success: function(data) {
//alert(data);
},
error: function(data) {
// alert(data);
// Revert
checkbox.attr('checked', !checked);
}
});
});
and
public function add_contact_details_availability()
{
if($_POST['action'] == 'checkbox-select') {
$checkbox = $_POST['id'];
$checked = $_POST['checked'];
// Your MySQL code here
echo 'Updated';
}
echo 'Code ran';
}
But it won't works
Here is the working JQuery
<script>
$(document).on("change", "input[name='chk']", function () {
var checkbox = $(this);
var checked = checkbox.prop('checked');
$.ajax({
url:"<?php echo base_url("contact-details-availability"); ?>",
type: 'post',
data: {
action: 'checkbox-select',
id: checkbox.data('contact_avl'),
checked: checked
},
success: function(data) {
//alert(data);
},
error: function(data) {
// alert(data);
// Revert
checkbox.attr('checked', !checked);
}
});
});
</script>
HTML
<input type="checkbox" name="chk" id="chk" data-contact_avl="val" value="1">check
I am saying it as working because all the three values are passing.
Changes done to fix are:
1] changed var checked = checkbox.attr('checked'); to var checked = checkbox.prop('checked');
2] changed id: checkbox.attr('contact_avl') to id: checkbox.data('contact_avl')
3] changed onclick to onchange
try this:
var id = checkbox.attr('contact_avl'),
$.ajax({
url:"<?php echo base_url("contact-details-availability"); ?>",
type: 'post',
data: {
action: 'checkbox-select',
id: id,
checked: 'checked'
},
success: function(data) {
//alert(data);
},
error: function(data) {
// alert(data);
// Revert
checkbox.attr('checked', !checked);
}
});

How to retrieve textbox value in a controller which is supplied with ajax?

I want to pass value of text box to my controller, i can pass that but i cant retrieve it at controller. Help me to identify my mistake.
Form.php (dept_name is a id of my textbox)
<script>
$(document).ready(function() {
$("#submitbutton").click(function(e) {
var deptname=$('#dept_name').val();
$.ajax({
url: 'http://localhost/finalProjectWork/admin_department_controller/adddept/',
type: 'POST',
method: 'POST',
headers: {'Cache-Control': 'no-cache'},
data: {name:deptname},
contentType: false,
processData: false,
success: function(test) {
alert(test);
},
error: function() {
alert("Already Exists");
}
});
e.preventDefault();
});
});</script>
controller :
public function adddept()
{
$deptname=$this->input->post('name');
$this->load->model('admin_department_model');
$this->admin_department_model->insertdept($deptname);
echo "Successfully inserted";
}
Try this
<script>
$(document).ready(function() {
$("#submitbutton").click(function(e) {
var deptname=$('#dept_name').val();
$.ajax({
url: 'http://localhost/finalProjectWork/admin_department_controller/adddept/',
type: 'POST',
method: 'POST',
headers: {'Cache-Control': 'no-cache'},
data: {name:deptname},
success: function(test) {
alert(test);
},
error: function() {
alert("Already Exists");
}
});
e.preventDefault();
});
});</script>

Categories