problem with ajax call of json and php - php

i have one AJAX function getting results from php file as bellow
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
now i want to use the value of g_foo but i can't i try using console.log but i am facing strange problem. like
if i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
console.log(g_foo);
}
});
now i can see the value return from php file
if now i use function like that
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
now i got error undefined g_foo;
thanks

As ajax is asynchronous, there's no way of making sure that g_foo is available as soon as you call the $.ajax() request, hence it's not available outside that callback.
You could however, specify async:false to that .ajax() call, but it will most likely block the browser, attempting to wait for the request, something I don't think you want to happen.

To use it outside you have two ways:
1)make the call syncronous like this (this is not a best practice as the browser has to wait for the call to continue the flow):
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json',
async: false,
success: function(json)
{
g_foo = json.foo;
}
});
console.log(g_foo);
2) call a function on success and continue the flow from there using whatever data has been returned from the function (this is the best practice)
$.ajax({
type: "POST",
url: "GeteT.php",
cache:false,
data:"id="+ encodeURIComponent(1),
dataType:'json'
success: function(json)
{
g_foo = json.foo;
yourfunction(g_foo);
}
});
function yourfunction(g_foo){
//here g_foo is set
}

Related

$.ajax and method post not work correctly

I have a simple code-form like this:
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
When on PHP server page, I try to print $_POST["action"] yet this field is blank. Why does it work fine if I use $.post(url,data,function)?
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
method: "login"
},
success: function (data) {
console.log(data);
}
});
login is a php function
You may use method instead of type.
$.ajax({
url: "http://myurl/test.php",
method: "POST", // <-- Use method, not type
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
Problem is solved.....
There is a problem with the callback on success event...and yesterday I focused my attention only on the request php in the developers tool....when I used Curl (with the right request) all work correctly and I forgot to check client code in success callback....
Never use post method with developers console,it's very easy to lose more time :=)

jQuery is not sending data to codeigniter

I am trying to get ajax working with a codeigniter installation.
This is my PHP function:
function test() {
return print_r("hey");
}
This is the JS:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
success: function(data) {
alert(data);
}
});
This works perfectly but, as soon as I add data it doesn't work.
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: {bar:"foo"},
success: function(data) {
alert(data);
}
});
Thanks in advance!
please check the following
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "&bar=foo&isAjax="+true,
success: function(data) {
alert(data);
}
});
And controller...
function test() {
if($this->input->post('isAjax')){
return print_r("hey");
}
else{
//do another thing
}
}
And another thing if you want to add data in json format then you have to add another property in your $.ajax object that is datatype: "json"
Use following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
dataType: 'json',
data: {'bar':'foo'},
success: function(data) {
alert(data);
}
});
Or you can use shorthand version:
$.post("http://localhost/code/test", {'bar':'foo'}, function(data) {
alert(data);
});
And your php code should be:
function test() {
echo "hey";
}
Try the following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "bar=foo&name=cyberbob",
success: function(data) {
alert(data);
}
});

Waiting for AJAX request, then finishing the jQuery

I have a piece of jQuery code:
var newData = checkCP(somedata);
if(newData=="hi"){
alert("Welcom");
}
function checkCP(jsData){
$.ajax({
type: "POST",
url: "Process.php",
data: jsData,
dataType: "json",
success: function(data){
if(data.match==1)
return "hi";
else
return "bye";
}
});
}
I don't know why the welcome alert never shows up. I checked everything; the PHP file returns 1, but apparently before it waits on the AJAX response it passes the
if(new=="hi"){
alert("Welcom");
}
Is there any way to wait for the AJAX response, then read the rest of codes in jQuery?
Yes, you can set the 'async' option in $.ajax() to false.
$.ajax({
type: "POST",
async: false,
url: "Process.php",
data: jsData,
dataType: "json",
success: function(data){
if(data.match==1)
return "hi";
else
return "bye";
}
Firstly, please don't use 'new' as a variable name. 'new' already means something.
Now onto the actual question...
when you call checkCP jquery does the ajax post successfully and automatically waits for a response before execuiting the success function. the thing is that the success function is a stand-alone function and returning a value from that does not return anything from checkCP. checkCP is returning null.
As proof try sticking an alert in your success function.
Something like this should do the trick:
function deal_with_checkCP_result(result)
{
if(result=="hi"){
alert("Welcom");
}
}
function checkCP(jsData,callback){
$.ajax({
type: "POST",
url: "Process.php",
data: jsData,
dataType: "json",
success: function(data){
if(data.match==1)
callback( "hi");
else
callback( "bye");
}
});
}
Pass deal_with_checkCP_result as callback

ajax calling of php function from a php page

This is my jQuery code:
$.ajax({
type: "POST",
url: "process.php",
success: function(msg){
}
});
In process.php page I have more than one function. One function is sendmail().
How can I call this function through ajax? I wrote the code as:
url: "process.php/sendmail",
but nothing happens.
this is your script file
$.ajax({
url: "process.php",
type: "POST",
data: "functionName=sendmail",
cache: true,
success: function(response){
}
and this is your process.php file
<?php
if(isset($_POST))
{
if($_POST["functionName"] == "sendmail")
{
sendmail();
}
}
?>
With Ajax call pass some data to process.php.
$.ajax({
type: "POST",
url: "process.php",
data: {method: 'one'},
success: function(msg){
}
});
In php page,
if(isset($_POST["method"])) {
$method = $_POST["method"];
if($method=="one") {
//call methods/functions here
}
}
May below code helpful to you..
$.ajax({
type: "POST",
url: "process.php",
data: {action: 'sendmail'},
success: function(msg){
}
});
Try this..
Thanks.
There is no automatic means of telling PHP to call your functions, but you can accomplish this with a simple check. Since you are trying URLs like "process.php/sendmail" you can test on the PHP variable $_SERVER['PATH_INFO']
if ('sendmail' == $_SERVER['PATH_INFO']) {
sendmail();
}
Try using if else and process.php?fun=sendmail
if($_GET['fun'] == "Sendmail")
sendmail()
else
// something else
to call send mail function pass a parameter to process.php file, and detect that parameter like:
$.ajax({
type: "POST",
url: "process.php",
type: "POST",
data: {sendMail: "1"},
success: function(msg){
}
});
now in process.php USE:
$_REQUEST['sendMail']
to detect the value..

JS jquery and ajax problem

Hi i have problem with some things
Here is the code
function get_char_val(merk) {
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){return(data);}
});
}
alert(get_char_val("str"));
when alert comes out it's output the undefined please help fast i'm making this two days xC
get_char_val does not return anything. The success callback needs to have alert(data) in it to return the data from AJAX.
$.ajax is asynchronous - meaning it doesn't happen in order with other code, that is why the callback exists.
Your return; statement will return the value to the anonymous function you pass into the success handler. You cannot return a value like this, you need to invoke another callback instead.
function get_char_val(merk, cb) {
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){cb.apply(this, data);}
});
}
get_char_val("str", function(data) {
alert(data);
});
You can either set the async:false config parameter in the ajax call options or use the call back.
Using Async:false -
function get_char_val(merk)
{
var returnValue = null;
$.ajax
(
{
type: "POST",
async:false,
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){returnValue = data;}
}
);
return returnValue;
}
alert(get_char_val("str"));
P.S: Note that making ajax calls synchronous is not advisable.
Since you do an asynchronous ajax call, the response of the server is handled in the "success" function. The return in your success function is useless, you should use the "data" parameter directly in the function
This should work. Use it like this.
function get_char_val(merk) {
var myReturnData= "";
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){myReturnData = data;}
});
return myReturnData;
}
alert(get_char_val("str"));
Just understand the problem with your piece of code. Use the return statement under the right function.

Categories