Variable data How to send thourgh ajax - php

When I sent the variable data through ajax loading_add.php page an error
is displayed:
Uncaught ReferenceError: profit is not defined.
What I tried so far is attached below.
var profit = (
Number($("#pro_price").val() - retail_price)
);
$.ajax({
type: "POST",
url: '../php/product/loading_add.php',
dataType: 'JSON',
data: {
profit: profit
};

In you php page just define variable as:
#$_POST['profit'];
you will not get undefined error again, hope this trick will help you :)

Your question is not clear.
But I share the valid way to send data via AJAX and retrieve the data in PHP Server Side..
var profit = (
Number($("#pro_price").val() - retail_price)
);
console.log(profit); // see in console log if `profit` is a valid variable or not
$.ajax({
type: "POST",
url: '../php/product/loading_add.php',
dataType: 'JSON',
data: {
profit: profit
}
}).done(function(result){
//do something after done AJAX
});
In server side, you must call variable $_POST['profit'] to retrieve the profit value.

Related

How to get and store Twitter share counts in database?

I want to get twitter counts of my website and article pages. After getting them I want to store them in database. I tried getting counts and posting them on my query.php using ajax but its showing me Cross Origin Block Error. Is there any way to do this? or am I missing something?
$.ajax({
url: "https://cdn.api.twitter.com/1/urls/count.json?url=<?php the_permalink(); ?>",
crossDomain: true,
crossOrigin: true,
success: function(data) {
var count = JSON.parse(data);
$("#dd").html(count);
$.ajax
({
type: "POST",
url: "post.php",
data: { stats: count, paralink:'<?php echo $rows['link_id'];?>', social:'2' },
success: function(data) {
},
dataType: 'json'
});
}
});
It would probably be more efficient / faster if you just made one ajax call to your server and a request to twitter directly from your php script.
And it would solve your problem as well:
// you should validate the input, but for simplicity:
$link = $_POST['paralink'];
// ^^^^^^^^ I am not sure if this is the page you want, you need to check that.
// get twitter json
$json = file_get_contents('https://cdn.api.twitter.com/1/urls/count.json?url=' . $link);
Now you just need the inner ajax call.

Getting data value via AJAX call

I wanted to get the form value that have been serialized and send it via AJAX call
var searchData = $("#SearchForm").serialize();
$.ajax({
type: "POST",
url: "search.php",
data: {
thedata : searchData,
page : "listPage="+pageNumber
},
success: function(searchResult){
$("#search-section").ajaxComplete(function() {
$(this).html(searchResult);
});
}
});
What should I do if I wanted to access data in the server side, usually if I only 1 data to be sent, I never define the data in .ajax, like...
...
data: searchData,
...
And I can access the serialized data in server side just like...
$_POST['inputValue1'];
$_POST['inputValue2'];
etc...
But how I should do it if I have 2 or more data to send via .ajax, since it must be defined first.
Simply you can use as below
data: searchData+"&listPage="+pageNumber

Passing Space Issue In Codeigniter Using Ajax

I have a codeigniter application.In that i call the controller function using ajax/jquery.But when i passing variable with space value it becomes wrong loading of another view page.
This is my view page code
var cnt=$("#search_agency_res").serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url();?>index.php/addAgency/agency_map_result/"+region+"/"+country+"/"+agency,
data: cnt,
success: function(valmsgnew){
$("#map").css("display","block");
$("#map").html(valmsgnew).show();
}
});
search_agency_res is the name of form.Here the variable region must have values in all time but country,agency may have ""(space).How can i handle this?
The issue is only occuring when country,agency have sapce or null value.If it have any valid value except space then everything is perfect.
Try passing data as post:
var cnt=$("#search_agency_res").serialize();
$.ajax({
type: "POST",
url: "index.php/addAgency/agency_map_result,
data: "cnt="+cnt+"&rregion="+region+"&country="+country+"&agency="+agency,
success: function(valmsgnew){
$("#map").css("display","block");
$("#map").html(valmsgnew).show();
}
});
Hope it helps

sending increment value from jquery to php file with for loop help

I have this for loop in jquery
$(document).ready(function() {
for(i=0; i<counter; i++)
{
dataCounter = i;
$.ajax({
url: 'file.php',
dataType: 'json',
data: dataCounter,
error: function(){
alert('Error loading XML document');
},
success: function(data){
$("#contents").html(data);
}
});
}
});
And then I want to bring in my dataCounter into the file.php as a variable and have it change each time so I can get different records in mysql in file.php, am I doing this right? How would the php portion look like? I know how pass variables to a php file with this method if I had a form, but I don't have a get or a post form to work with. Also, my variables are going to change.
Can someone help me? Thank you!
While I don't recommend running an ajax query inside of a loop, I am wiling to explain the data option for $.ajax(). Ideally, you pass an object as the data option, and it is translated by jQuery into a query string where each object property name is a key and its value is the value:
data: {
count: dataCounter
}
becomes
?count=1
in the query string of the ajax request if datacounter is equal to 1.
In PHP you would access it as $_GET['count'].
data needs to be a key-value pair, not just a value as you have it here. Try something like: (not tested)
$.ajax({
url: 'file.php',
dataType: 'json',
data: ({dataCounter : dataCounter}),
error: function(){
alert('Error loading XML document');
},
success: function(data){
$("#contents").html(data);
}
});

JQuery Ajax post data not arriving

my Ajax post data is not arriving - any clues please.
The data is a serilaized form that "alerts" correctly with all the data using this
$(document).ready(function() {
var serial = $('#frm_basket').serialize();
alert(serial);
$.ajax({
url: "basket-calc.php",
type: "post",
data: serial,
success: function(){
("#basketTotal").load('basket-calc.php');
}
});
});
The alert gives me a string like product=p1&qty=1&product=p2&qty=2
But when I try to php echo out the results on basket-calc.php I get an "empty" array
basket-calc.php:
$test = $_POST;
print_r($test);
You can debug your request with firebug to make sure what is happening.
Also try setting the post type to GET:
type: "GET",
to see if it makes any difference.
try:
$(document).ready(function() {
var serial = $('#frm_basket').serialize();
alert(serial);
$.ajax({
url: "basket-calc.php",
type: "post",
data: serial,
success: function(result){
("#basketTotal").html(result);
}
});
});
Also note following points:
make sure basket-calc.php is not returning 404
try sending blank data and echo your response
once you get sample string from server, just attach the real data
hope this helps
If your htaccess is stripping the .php from the , the POST gets converted to GET. I think.
Try and remove .php from your url.

Categories