How to get ajax request in php? - php

$.ajax({
type: "POST",
url: "Check_Country.php",
data: "{Country_name: "+Country_name+"}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(html){
$('select#Section').empty();
$('select#Section').append('<option>Select</option>');
$('select#Section').append(html);
}
});
How do you detect Country_name in Check_Country.php file?
I am using
$_POST['Country_name'];
in php file but, the console says that it cannot find Country_name.

Try to use
data: "{Country_name: "+Country_name"}"
instead of
data: "{Country_name: "+Country_name+"}"
i.e. remove the trailing + sign.

In your code only one syntax is wrong
you have to change this code :-
data: "{Country_name: "+Country_name+"}",
into
data: {"Country_name": Country_name}
or
data: "Country_name="+Country_name,
please try this:-

Related

Opencart - Way to call ajax request in Admin Panel

I want to fetch some data using ajax request in Opencart Admin panel. I have simply created a controller which only returns data in json format. Using below code:
$.ajax({
type: 'post',
url: 'index.php?route=common/ajaxdata/functionName&token=<?php echo $token; ?>',
data: data,
dataType: 'json',
success: function(json) {
if (json['success']) {
alert(json['success']);
}
}
});
Can anybody please let know whether this is correct or any other built in functionality available in Opencart for ajax requests? So that I can proceed and implement this in my rest of the pages.
Thanks
$.ajax({
type: "POST",
url: "http://your url write hare",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
"propertytypename": property,
"propertytypecreatedby": property_name
}),
success: alert("success"),
error: alert("Fail")
});

Sending JSON data using ajax issue

I have the following code:
var arr = {City:'Moscow', Age:25};
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data: JSON.stringify(arr),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});
The result is null. In the PHP side I have:
echo json_encode($_POST);
and
print_r($_POST);
But both are giving empty results (checked Firebug also).
You can also set the dataType in Ajax to specify the content type as follows.
var city='city name';
var age=10;
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data:"City="+city+"&Age="+age,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});
and in cities/index.php you can get this data by follows
if($_POST){
$city=$_POST['City'];
$age=$_POST['Age'];
// and do with $city and $age what you want.
//for return anything thing to `json` use follows we pass $age back to ajax
echo json_encode($age);
}
I guess you don't need to stringyfy the data because data should be PlainObject or String but in your case you can simply write like below
var arr = {City:'Moscow', Age:25};
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data: arr,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});
as documented in jquery official site https://api.jquery.com/jQuery.ajax/
data
Type: PlainObject or String
Data to be sent to the server. It is converted to a query string, if
not already a string. It's appended to the url for GET-requests. See
processData option to prevent this automatic processing. Object must
be Key/Value pairs. If value is an Array, jQuery serializes multiple
values with same key based on the value of the traditional setting
(described below).
The data option passed to $.ajax() must be either a simple object, that jQuery will transform into a string of the formatkey1=value1&key2=value2.. OR it should be a string of the form key1=value1&key2=value2...
In your case, it can be solved by passing the object itself and letting jQuery do the query string formatting:
$.ajax({
...
data: arr,
...
});
try this
var arr = {City:'Moscow', Age:25};
$.ajax({
url: "<? echo $this->createUrl('cities/index');?>",
type: "POST",
data: arr,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(data){
alert(data);
}
});

Passing jquery array to PHP with AJAX

having a bit of an issue with sending a jQuery array to a PHP file. I've looked at similar questions on here, but mine has other elements to the data variable being sent. Here's the code:
var data = 'type='+e+'&offset=' + all_dates_offset + '&filters=' + filters;
$.ajax({
url: "pos_jobs.php",
type: "POST",
cache: false,
data:data,
dataType:"json",
success: function(html){
//Do Something
}
});
For the data, 'e' and 'all_dates_offset' are standard variables, whereas 'filters' is an array. On the PHP side of things, I was hoping I could just use something like $_POST['filters'][0], but that is returning a null value.
Any ideas?
Thanks.
$.ajax({
url: "pos_jobs.php",
type: "POST",
data: {type: e, offset: all_dates_offset, filters: filters},
dataType:"json"
}).done(function(data) {
//do something
});
use below
$.ajax({
url: "pos_jobs.php",
type: "POST",
cache: false,
data:{'type':e,'offset':all_dates_offset,'filters':filters},
dataType:"json",
success: function(html){
//Do Something
}
});

Send and receive data in same ajax request with jquery

What is the best way to send data and receive a response dependent on that data?
Consider the PHP file used for the request:
$test = $_POST['test'];
echo json_encode($test);
I have tried unsucessfully to achieve this with:
$.ajax({
type: "POST",
dataType: "json",
data: '{test : worked}',
url: 'ajax/getDude.php',
success: function(response) {
alert(response);
}
});
Lose the quotes to pass the object:
$.ajax({
type: "POST",
dataType: "json",
data: {test : worked},
url: 'ajax/getDude.php',
success: function(data) {
alert(data);
}
});
Instead of this
data: '{test : worked}'
try
data: {"test" : worked} // Worked being your data you want to pass..
data: {"test" : "worked"} // Else enclose worked in quotes
The problem appears to be that you're submitting a string rather than a json object - change data: '{test : worked}' to data: {test : 'worked'}

Jquery - Send variables to a controller Action via GET in AJAX

All,
I want to send a variable "itemId" via GET to a controller action through AJAX. In the Controller Action, I should be able to retrieve the value using $_GET["itemId"];
Can I send the querystring with "data" tag instead of appending it to the "url"?
I have the following code:
$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: itemId,
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});
How can I do this?
data: {itemId: itemId},
$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: ({itemId: itemId}),<------change it to this
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});
Make itemId a JavaScript object before making the AJAX request. For example:
var itemId = {'itemId': 1000};
data: {itemId: "you info"},
or
data: "itemId=you info",

Categories