0

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

Allende
1,4822 gold badges23 silver badges40 bronze badges
asked Feb 24, 2014 at 22:38
1
  • What version of JQuery are you using? Also, why don't you use the error/success callbacks? Commented Feb 24, 2014 at 22:51

2 Answers 2

1

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 );
 }
});
answered Feb 24, 2014 at 22:52
1
  • 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();"> Commented Feb 24, 2014 at 23:33
0

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 :-)

answered Feb 24, 2014 at 22:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.