function submit_onClick() {
var addValue = jQuery("#billing_address_1").val();
jQuery.ajax({
type: "POST",
url: "http://localhost/wordpress/wp-admin/admin-ajax.php",
data: {'action':'distance_calculation', addValue:addValue}
}).done(function(data) {
alert(data);
});
}
this works fine until I dont place distance_calculation inside any class.
how can i call a function if its inside any class for example, if distance_calculation function is inside any class called "distance" then how to call this in action .
Have the the ajax request like so
function submit_onClick() {
var addValue = jQuery("#billing_address_1").val();
jQuery.ajax({
type: "POST",
url: "http://localhost/wordpress/wp-admin/admin-ajax.php?method=distance_calculation&addValue=addValue"
}).done(function(data) {
alert(data);
});
}
Then in your PHP file add
if(strcmp($_POST["method"],"distance_calculation")){
$class->distance_calculation($_POST["addValue"]);
}
Related
I am new in Codeigniter
I want to access base URL ID into AJAX, within I also pass another id to the controller in Codeigniter. At the controller side, I access two different id one site_url id and another s_id.
So how can I achieve this task?
Site URL
https://www.demoexample.com=?uid=10
Ajax script
<script>
$(document).ready(function(){
$('#member_type').on('change',function(){
var memId = $(this).val();
if(memId!=''){
$.ajax({
url: "<?php echo site_url('index.php/template/vehicle/get_member_type')?>",
method:'POST',
data:'member_id='+memId, // here I want to access site_url id and s_id from drop-down id
success:function(data)
{
$('#mid').html(data);
}
});
}
else{
$('#mid').html('<option value="">Select sttxt_Standard first</option>');
}
});
});
</script>
Controller .php file
function show_vehicle_reg(){
$id=$this->input->post('id'); // access multiple id over here from ajax
}
}
Thanks in advance
ajax:
GET Method
$.ajax({
beforeSend: function () {
},
complete: function () {
},
type: "GET",
url: "<?php echo site_url('controller/cmethod/'.$s_id); ?>",
success: function (data) {
}
});
POST Method
$.ajax({
beforeSend: function () {
},
complete: function () {
},
type: "POST",
url: "<?php echo site_url('controller/cmethod'); ?>",
data: ({member_id: memId}),
success: function (data) {
}
});
I am unable to pass the data to a PHP function using ajax in jquery.
function updateinventory(product_code,user_id,id,outlet_id) {
var qtyid = 'qty_'+outlet_id;
//alert(product_code);
var dataString = {pcode:product_code};
var newqty = $('#qty_'+outlet_id).val();
//alert(newqty);
jQuery.ajax({
url: "<?=base_url()?>inventory/updateInventoryQtyFromProduct",
data: dataString,
type: "POST",
dataType: 'json',
success:function(data){
console.log("success"+data);
},
error:function (error){
console.log(error);
}
});
}
and the PHP function is where I am trying to return the $pcode just to check if data is passing here or not.
public function updateInventoryQtyFromProduct()
{
$pcode=$_POST['pcode'];
// $us_id=$_POST['user_id'];
return $pcode;
}
You have to pass like this :
public function updateInventoryQtyFromProduct()
{
$pcode=$_POST['pcode'];
echo json_encode($pcode);
or
echo json_encode(array('pcode'=>$pcode)); //if array need
}
I am trying to pass an array through post to a php file. Here is my function in jQuery:
function callPHP() {
$.post("php/save.php", {
node: node
},
function (responde) {
console.log(responde);
});
}
And my save.php file has following content:
<?php
echo $_POST['node'];
?>
But I get an error that there's an undefined index 'node'. What is it that I am doing wrong?
try following
//define php info and make ajax call
$.ajax({
url: "php/save.php",
type: "POST",
data: { node: node },
cache: false,
success: function (response) {
}
});
Consider the below example. where info is an array
info = [];
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
data: {info:info},
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
.answer is the class of the element where you want to output your answer.
I setting the global variable in the __construct();
function __construct()
{
parent::__construct();
//variables
$this->galleryID = $this->uri->segment(3);
$this->productID = $this->uri->segment(4);
}
After a selection is made from a dropdown menu I make an ajax request.
$.ajax(
{
type: 'POST',
url: '/beta/checkout/getCoverSizes',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});
And at this point, simply output the global variable
public function getCoverSizes()
{
print_r($this->productID);
}
Currently nothing is $this->productID is returning 0, and I am positive that it is correct as function index() depends on this variable and is rendering the data correctly. The ajax request does not appear to be accessing the global variable $this->productID.
$.ajax(
{
type: 'GET', // use GET here
url: '/beta/checkout/getCoverSizes',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});
You are using $this->uri->segment(3); for galleryID and $this->uri->segment(4); for productID but the url in ajax call doesn't have these parameters you should pass these ids in ajax call in order to get like
$.ajax(
{
type: 'POST',
url: '/beta/checkout/getCoverSizes/1/2',
//url: '/beta/checkout/getCoverSizes/galleryID/productID',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});
And in your class i assume you have define the global variables like
class checkout extends CI_Controller {
public $galleryID;
public $productID;
// your other code
}
in java-script pass the value js to globle ajax
$("#abc").on("change",function(){
var post_data = new FormData();
ajax_request("dashboard/ajax",post_data, response,null);
});
and then in JS
function ajax_request(URL, request_data, response_function, element){
$.ajax({
type: "POST",
datatype:"json",
url: BASE_URL+URL,
data: request_data,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
success: function(result){
response_function(JSON.parse(result), element);
},
error: function() {
response_function(undefined, element);
}
});
}
I'm trying to pass the a variable from JavaScript to PHP using AJAX, but I'm unable to do so. Whenever I try to var_dump($_POST['winner_id']) it returns NULL. I've tried to check the AJAX call with Developer Tools in Chrome and it showed winner_id:0 - which is right.
Here is my code:
JavaScript
function ajaxCall() {
alert("To AJAX: the winnerid is: "+winner_id);
$.ajax
( {
type: "POST",
url: "ajax.php",
data: {winner_id : winner_id},
success: function(response)
{ alert("The winner was passed!")}
}
);
};
ajaxCall();
PHP Code
<?php
session_start();
if(isset($_POST['winner_id']))
{
$winner_id = $_POST['winner_id']."";
var_dump($winner_id);
}
var_dump($_POST['winner_id']);
?>
If I do a var_dump($_POST) in the beginning of the PHP script then it gives me array(0) { }
I'm new to web development and have been trying to figure this out for hours now. Any hints would be much appreciated. Thanks!
Where are you intializing the winner_id.Either you have to pas it as an argument or intitialize it as aglobal variable.
function ajaxCall(winner_id) {
alert("To AJAX: the winnerid is: "+winner_id);
$.ajax
({
type: "POST",
url: "ajax.php",
data: {"winner_id" : winner_id},
success: function(response)
{
alert("The winner was passed!");
}
});
};
ajaxCall(winner_id);
Where did you initiate value to winner_id? like
function ajaxCall() {
var winner_id = '123';
...
or if you initiated winner_id before calling ajaxCall() ,you should call ajaxCall() with parameters like ajaxCall($winnerid), which $winnerid is from your PHP
and then
function ajaxCall(winner_id) {
...
i guess you have to convert your winner_id to a string because php read zero (0) as null
function ajaxCall() {
alert("To AJAX: the winnerid is: "+winner_id);
$.ajax
( {
type: "POST",
url: "ajax.php",
data: {winner_id : winner_id.toString()},
success: function(response)
{ alert("The winner was passed!")},
dataType: "json"
}
);
};
ajaxCall();