Checked several topics here on stack overflow and the jquery documentation but still getting stuck.
I basically want this to post (with ajax/jquery) to update a div's content.
function loadSubCats(value)
{
$.post("load_sub_cats.php",
data: { "catid": value },
type: "POST",
contentType: "application/x-www-form-urlencoded",
function(data) {
$('#sub_categories').html("Data Loaded: " + data);
});
}
But im getting no responses what so ever. The page contains the sub_categories div, and this is called onclick for a form event.
<select name='cselect3' onChange='loadSubCats(this.value)' class='e1'>
So basically, it should past the value of the field (cselect3) and load_sub_cats.php should just echo $_POST[catid]; thats it. But im getting zero activity on it so it think its something simple i've screwed up.
you have a problem with $.post syntax. Here's how it should be:
function loadSubCats(value)
{
$.post("load_sub_cats.php",{ "catid": value },
function(data) {
$('#sub_categories').html("Data Loaded: " + data);
});
}
as simple as it is.
function loadSubCats(value)
{
$.post("load_sub_cats.php",
{ catid: value },
function(data) {
$('#sub_categories').html("Data Loaded: " + data); }
);
}
You send catid not "catid"
You're mixing the syntax of $.post and $.ajax which are different. If you want to use the object syntax then call $.ajax, because $.post takes its arguments as a simple un-named list.
function loadSubCats(value)
{
$.ajax({
url : "load_sub_cats.php",
data: { "catid": value },
type: "POST",
contentType: "application/x-www-form-urlencoded",
success: function(data) {
$('#sub_categories').html("Data Loaded: " + data);
}
});
}
You could also directly attach the change event instead, of inline and do away with the loadSubCats() function:
$(document).ready(function(){
$('select[name=cselect3]').change(function(){
$.ajax({
url : "load_sub_cats.php",
data: { "catid": $(this).val() },
type: "POST",
contentType: "application/x-www-form-urlencoded",
success: function(data) {
$('#sub_categories').html("Data Loaded: " + data);
}
});
});
});
two things:
you are using type in post which is not required.
Even after removing it is not working just try to alert it-
function loadSubCats(value)
{
$.post("load_sub_cats.php",
data: { "catid": value },
type: "POST",
contentType: "application/x-www-form-urlencoded",
function(data) {
alert(data)
$('#sub_categories').html("Data Loaded: " + data);
});
}
By alert you will come to know -
1) if there is any php error it will alert that,
2) If php is working fine that it will alert with your values.
updating my post as per latest comments:
Try to use like below:
$(document).ready(function() {
$('.e1').change(function() {
$.post("load_sub_cats.php",
data: { "catid": value },
type: "POST",
contentType: "application/x-www-form-urlencoded",
function(data) {
alert(data)
$('#sub_categories').html("Data Loaded: " + data);
});
})
});
Related
I'm trying to send some data from PHP back to AJAX. I found some examples but it doesn't seem to work. The result of console log is: "test: success". How can I get the data?
$.ajax({
url: "assets/psv.php",
method: "POST",
dataType: "HTML",
success: function(results, test){
console.log("test:" + test);
},
error : function (e) {
console.log("error " + e);
}
});
PHP
$test= "pgv";
echo $test;
Try:
$.ajax({
url: "assets/psv.php",
method: "GET",
success: function(data){
console.log("test:" + data);
},
error : function (e) {
console.log("error " + e);
}
});
or something like this:
$.get( "assets/psv.php", function( data ) {
alert( "Data Loaded: " + data );
});
The first variable in the success callback contains data received from the page you called.
PHP
header('Content-Type: application/json');
$test= "pgv";
echo json_encode($test);
JS
$.ajax({
url: "assets/psv.php",
method: "GET",
dataType: "json",
success: function(response, status){
console.log("test", response.data); //CHANGE THIS!!
},
error : function (e) {
console.log("error " + e);
}
});
data will contain
{
headers: "...",
data: "pgv",
....
}
I post data to sa.php via AJAX and on it I use $_POST['data'] to grab the post and echo it. Now when I hit F12 to see the transfer it shows success. The $_POST['data'] shows up on the sa.php but when you look at the screen no text shows.
retrieve code:
Now here is my AJAX code:
$.ajax({
type: "POST",
data: {"data": data},
dataType: "text",
success: function(data){
console.log(data);
},
statusCode: {
404: function() {
alert( "page not found" );
}
},
complete: function (data) {
console.info(data);
}
});
});
I'm trying to echo the $_POST['data'] but it won't display in html.
As most comments point out you are not updating the HTML but rather just logging to the console....try this... create a div with an id "status"
In your page
<div id="status"></div>
and update your javascript
$.ajax({
type: "POST",
data: {"data": data},
dataType: "text",
success: function(data){
$('#status').html(data); //puts the response in a div with id status
console.log(data);
},
statusCode: {
404: function() {
alert( "page not found" );
}
},
complete: function (data) {
console.info(data);
}
});
});
I presume that you would have a container div for where you want to echo your $_POST['data']? If you do, you can use that div as a selector using jQuery, where you can insert your AJAX data into it. To do that you add to your AJAX success function, something like the following:
success: function(data) {
$('#containerDiv').html(data);
}
Note: .html overwrites whatever is in your selector
Have a look at the first answer of this question, i think this might help achieve what you need:
https://stackoverflow.com/a/2972645/5525510
i have to pass header information while posting a url and want to pass raw data in the url can any on help me on this
i have tried
function test(){
alert("test");
$.ajax({
type:"POST",
beforeSend: function (request)
{
request.setRequestHeader("X-APIKEY", "y5q9q1at8u-1uf4bao2yq-bsjdj3gh1g-u9ymh1t2f8-tt85pn4r50");
},
url: "https://backoffice.hostcontrol.com/api/v1/domain-is",
data: {"domain": "mahrosh.com"},
processData: false,
success: function(msg) {
alert(msg);
$("#results").append("The result =" + StringifyPretty(msg));
}
});
}
its not returning me results
You can add header variables as per given in below described code
function test(){
alert("test");
$.ajax({
type:"POST",
headers: { 'X-APIKEY': 'y5q9q1at8u-1uf4bao2yq-bsjdj3gh1g-u9ymh1t2f8-tt85pn4r50'},
url: "https://backoffice.hostcontrol.com/api/v1/domain-is",
data: {"domain": "mahrosh.com"},
processData: false,
success: function(data) {
alert(JSON.stringify(data));
},
error: function(data){
alert(JSON.stringify(data));
}
});
}
If server gives error in response than you have idea about it so i added the error section as well ...
I have the followin PHP file (a function of it):
public function get_hotels(){
$hoteles = new HotelModel();
$query = "SELECT * FROM hotel";
$hoteles = $hoteles->execute_query($query);
echo json_encode($hoteles);
}
And this is my jQuery script:
$.ajax({
type: "POST",
url: "index.php?controller=ExcursionTypes&action=get_hotels",
dataType:"json",
success: function(response){
alert(typeof (response[0].hotel_name));
//$("#pickups_fields").html(response[0].hotel_name);
},
error:function(response){
alert("ERROR");
}
});
Firebug throws me this JSON:
[{"id_hotel":"1","hotel_name":"Apt.Playa del Ingles", "hotel_phone":"928762629",
"hotel_corporation_id":"1","hotel_state_id":"1"},
{"id_hotel":"2","hotel_name":"LZ",
"hotel_phone":"928762629","hotel_corporation_id":"1",
"hotel_state_id":"2"}]
I want to read both hotel_name fields and I can't.
I'm sure you're giving me the solution or a link to solve it.
Although I'm looking for it too.
Javascript is case sensitive, so you should write dataType, not datatype.
JSON that you get as a response is correct and response[0].hotel_name would work, but because you mistyped dataType, the response is not parsed as a JSON and therefore you can't access it the way you did.
Try iterating over the objects in response, like this:
$.ajax({
type: "POST",
url: "index.php?controller=ExcursionTypes&action=get_hotels",
dataType:"json",
success: function(response){
$.each(response, function(i, hotel) {
alert(typeof (hotel.hotel_name));
//$("#pickups_fields").html(hotel.hotel_name);
});
},
error:function(response){
alert("ERROR");
}
});
$.ajax({
type: "POST",
url: "index.php?controller=ExcursionTypes&action=get_hotels",
dataType:"json",
success: function(response){
for(key in response) {
alert(typeof (response.hotel_name[key]));
});
},
error:function(response){
alert("ERROR");
}
});
Hi everyone I'm trying to incorporate jQuery AJAX on my multi-step form so that it updates a certain div to the one on the process.php page but it keeps loading the results on a new page. How can I get it to update the div without refreshing the page?
This is the jQuery code I'm using:
$.ajax({
url: 'process.php',
data: $('form[name="booking"]').serialize(),
dataType: 'html',
type: 'POST',
success: function(data) {
// this is the bit that needs to update the div
$('#last-step').fadeOut(300, function() { $('#result').html(data).fadeIn(300);
},
complete: function (data) {
// your code here
},
error: function (url, XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});
This is the code for my multistep form: http://jsfiddle.net/xSkgH/47/.
Many thanks in the advance
I dont see a div called result in your Markup. So probably you need to show your result in the last div you are showing. And you are missing }) also. the below code should work,
$.ajax({
url: 'process.php',
data: $('form[name="booking"]').serialize(),
dataType: 'html',
type: 'POST',
success: function(data) {
// this is the bit that needs to update the div
$('#last-step').fadeOut(300, function() {
$('#last-step').html(data).fadeIn(300);
});
},
complete: function (data) {
// your code here
},
error: function (url, XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});