i just try to use javascript for build mobile apps. This apps required connection to sql server. I already create the javascript and php file, but whenever i try to call the function it gave me no results. this is the javascript function:
function getvoucher()
{
var result = jQuery.ajax({
type: "GET",
url: aurl,
data: "tag=getvoucher"
async: false
}).responseText;
if(result)
{
var obj = jQuery.parseJSON(result);
if(obj.success == 1)
{
var voucher=obj.name;
document.write(voucher);
}
}
}
this is my phpscript:
<?php
include("../includes/config.php");
if(isset($_REQUEST['tag']) && ($_REQUEST['tag']=='getvoucher'))
{
$success=0;
$query=mysql_query("select code from tbl_voucher")or die ("query issue");
#if(mysql_num_rows($query) > 0)
# {
if($query)
{
$success=1;
$code=$row['code'];
}
# }
$jsondata = array('tag'=>"getvoucher",'success'=>$success, 'code'=>$code);
echo json_encode($jsondata);
if($query){mysql_free_result($query);}
mysql_close($con);
die;
}
>?
The php and connection to sql is working because i already success in other script. And the tbl_voucher and column code is already exist.
Thanks
-
What version of JQuery are you using? Also, why don't you use the error/success callbacks?Ben A. Hilleli– Ben A. Hilleli2014年02月24日 22:51:20 +00:00Commented Feb 24, 2014 at 22:51
2 Answers 2
Perhaps the last die
in your php script might be messing up. Loose it.
$jsondata = array( 'tag '=> 'getvoucher','success' => $success, 'code' => $code);
echo json_encode( $jsondata );
if( $query ){ mysql_free_result( $query ); }
mysql_close( $con );
die;
By the way, you are missing a coma here
data: "tag=getvoucher"
Also, your ajax call seems bit weired.
var result = jQuery.ajax({
type: "GET",
url: aurl,
data: "tag=getvoucher"
async: false
}).responseText;
if ( result )
{
var obj = jQuery.parseJSON( result );
if(obj.success == 1)
{
var voucher=obj.name;
document.write( voucher );
}
}
Instead, try this
$.ajax({
type: "GET",
url: aurl,
data: "tag=getvoucher",
async: false,
success: function( object ) {
document.write( object.name );
}
});
-
Hi, after fixing couple of things now i get "undefined" in my screen. btw i load the function with this: <body background="bg_html.jpg" onLoad="getvoucher();">user3231429– user32314292014年02月24日 23:33:11 +00:00Commented Feb 24, 2014 at 23:33
If you use google chrome and run the webpage/script, press F12 and look at network=>XHR. Then you can look at what is submitted and you can even open the result-page.
This way you can see if your parameters are passed and you can see what gets returned. If something is not passed, you need to fix it on the sender side. If it is sent and return data is sent, it means reciever side is wrong :-)