Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Php/ ajax does not return any data
Welcome, Guest. Please login or register.
November 21, 2008, 12:04:14 PM
11306 Posts in 1249 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Serverside Technology  |  PHP  |  Php/ ajax does not return any data « previous next »
Pages: [1]
Author Topic: Php/ ajax does not return any data  (Read 400 times)
thepreacher

Offline Offline

Posts: 77


« on: March 15, 2007, 04:19:28 PM »

I have a get exchange rate function which job is to retrieve an exchange rate from an fxtable. the javascript code is as below:

   
Code
Language: javascript (GeSHi-highlighted)
    var url2 = "getExchrate.php?param1=";
 
   function getExchrate() {
     var transcrncy = document.moneytrans.transcrncy.value;
     var paycrncy = document.moneytrans.paycrncy.value;
     //var trancurcode = transcrncy + paycrncy;
     alert(transcrncy);
     alert(paycrncy);
     http.open("GET", url2 + escape(transcrncy) + "&param2=" + escape(paycrncy), true);
     http.onreadystatechange = handleHttpResponse2;
     http.send(null);
 
   function handleHttpResponse2() {
 
   if (http.readyState == 4) {
 
   //Split the comma delimited response into an array
   var results2 = http.responseText;
 
   alert(results2[0]);
   //alert(results2[1]);
   document.moneytrans.exchrate.value = results2;
     }
   }
   }


and the php code is

Code
Language: php (GeSHi-highlighted)
 
function db_connect() {
 $database_name = 'globalexpat'; // Set this to your Database Name
 $database_username = '*****'; // Set this to your MySQL username
 $database_password = '*****'; // Set this to your MySQL password
 $result = mysql_pconnect('localhost', $database_username, $database_password);
 if (!$result) return false;
 if (!mysql_select_db($database_name)) return false;
 return $result;
 }
 
$conn = db_connect(); // Connect to database
if ($conn) {
 $ltranscur = $_GET['param1'];
 $lpaycur = $_GET['param2'];
 
 //echo $_GET['param1'];
 //echo $_GET['param2'];
 
 //return;
 
 //echo $trecurcode;
 $query = "select midrate from fxrate where transcurcode = '$ltranscur' and recevcurcode = '$lpaycur'";
 $result = mysql_query($query, $conn);
 $count = mysql_num_rows($result);
 if ($count > 0) {
   $transcrncy = mysql_result($result, 0, 'transcurcode');
   $midrate = mysql_result($result, 0, 'midrate');
   $midrate = stripslashes($row['$midrate']);
   }
}
 
if (isset($midrate))
{
 $return_value = $midrate;
}
else
{
 $return_value = "invalid".",".$_GET['param']; // Include Zip for debugging purposes
}
//echo $return_value; // This will become the response value for the XMLHttpRequest object
 

When the function returns, and i display the variable, it is empty. I just want to be sure that the code for the passing of the parameters from javascript to php is correct.
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #1 on: March 15, 2007, 05:12:39 PM »

Send the parameters in the send method...objRequest.send('param1='+escape(transcrncy)+'&param2='+escape(paycrncy));

Code:
<?PHP
/********************** PAGE HEADERS *********************/
// no browser caching of this page !! to be used on all pages
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
/*********************************************************/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Preacher AJAX</title>
<script type="text/javascript">
<!--
var url2 = "http://www.cafesong.com/ert/getExchrate.php";
var objRequest;

function getExchrate() {
var transcrncy = document.moneytrans.transcrncy.value;
var paycrncy = document.moneytrans.paycrncy.value;
//var trancurcode = transcrncy + paycrncy;
//alert(transcrncy);
//alert(paycrncy);


if (window.XMLHttpRequest) { // Mozilla, Safari, ...
objRequest = new XMLHttpRequest();
if (objRequest.overrideMimeType) {
objRequest.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // II
objRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

if (!objRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
objRequest.onreadystatechange = handleHttpResponse2;
objRequest.open("GET", url2, false);
objRequest.send('param1='+escape(transcrncy)+'&param2='+escape(paycrncy));
}
 
function handleHttpResponse2() {
if (objRequest.readyState == 4) {
if (objRequest.status == 200) {
var results2 = objRequest.responseText;
objRequest=null;
var arr=results2.split(',');
                        // arr[0] is param1
// arr[2] is param2
// alert(results2)
document.moneytrans.exchrate.value = arr[2];
}
}
}
//-->
</script>
</head>

<body>
<a href="#" onclick="return getExchrate();">Click Me</a>
<form name="moneytrans">
  <input type="text" name="transcrncy" value="10">
  <input type="text" name="paycrncy" value="121">
  <input type="text" name="exchrate">
</form>
</body>

</html>
[code]

This works, although I'm simply echoing the params and a made up rate.

[code]
<?PHP
$ltranscur = $_GET['param1'];
$lpaycur = $_GET['param2'];

echo $ltranscur.",".$lpaycur.",9.9";
?>

http://www.cafesong.com/ert/preacherAJAX.php[/code][/code]
« Last Edit: March 15, 2007, 05:54:16 PM by rdivilbiss » Logged

Rod
thepreacher

Offline Offline

Posts: 77


« Reply #2 on: March 15, 2007, 06:48:55 PM »

Is the order of the following statements correct:

Code
Language: php (GeSHi-highlighted)
objRequest.onreadystatechange = handleHttpResponse2;
objRequest.open("GET", url2, false);
objRequest.send('param1='+escape(transcrncy)+'&param2='+escape(paycrncy));
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #3 on: March 15, 2007, 06:54:15 PM »

No, I made a boo boo.

here: http://www.cafesong.com/ert/preacherAJAX.php

correct code is in the page.
Logged

Rod
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #4 on: March 15, 2007, 06:58:30 PM »

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Preacher AJAX</title>
<script type="text/javascript">
<!--
var url2 = "http://www.cafesong.com/ert/getExchrate.php";
var objRequest;

function getExchrate() {
var transcrncy = document.moneytrans.transcrncy.value;
var paycrncy = document.moneytrans.paycrncy.value;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
objRequest = new XMLHttpRequest();
if (objRequest.overrideMimeType) {
objRequest.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // II
objRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

if (!objRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
objRequest.onreadystatechange = handleHttpResponse2;
var params = '?param1='+transcrncy+'&param2='+paycrncy;
objRequest.open("GET", url2+params, true);
objRequest.send(null);
}
 
function handleHttpResponse2() {
if (objRequest.readyState == 4) {
if (objRequest.status == 200) {
var results2 = objRequest.responseText;
objRequest=null;
var arr=results2.split(',');
// arr[0] is param1
// arr[1] is param2
// alert(results2)
document.moneytrans.exchrate.value = arr[2];
document.moneytrans.rawResults.value = results2;
}
}
}
//-->
</script>
<style>
<!--
.code        { font-family: monospace; font-size: 10pt; color: #000000;
               border: 1px dashed #000000; padding: 5px; background-color:
               #FFFFCC }
-->
</style>
</head>

<body>
<a href="javascript:void(0);" onclick="getExchrate();">Click Me</a>
<form name="moneytrans">
  <p>Trans Currency <input type="text" name="transcrncy" value="10"></p>
  <p>Pay Currency <input type="text" name="paycrncy" value="121"></p>
  <p>Exchange Rate <input type="text" name="exchrate"></p>
  <p>Raw Results <input type="text" name="rawResults"></p>
</form>

</body>

</html>
Logged

Rod
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #5 on: March 16, 2007, 07:29:57 AM »

thepreacher, while searching in your PHP code for the cause of an empty return to your Ajax call, I noted the following :
1) a function should return a coherent type of answer. Returning FALSE or a [numeric] symbolic connection handle/resource ID is bad practice to me.
2) afterwards, testing ($conn) in stead of testing ($conn===FALSE) is also a bad idea.
3) I know the db connection handle can'tbe zero, but testing as you do makes me feel uncomfortable (for you ;-)
I think you aim for trouble if you learnt to code like that, but again in this particular occurence it will probably work anyhow.
That's my humble opinion of course.

on to the rest of code now :
1) unless your field "midrate from
fxrate" contains urlencoded values, you probably forgot to urldecode() the $_GET['param1'] and 'param2' arguments before querying the DB
2) you forgot to say "echo $returned_value;" so that your PHP code outputs something on the socket for the Ajax blocked call to get ;-)
(unless you showed us incomplete code, of course)

regards
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
thepreacher

Offline Offline

Posts: 77


« Reply #6 on: March 17, 2007, 01:05:15 PM »

thanks to you all, VGR i'll consider your points and go over my code again. In the mean time, I tried to used
Code
Language: php (GeSHi-highlighted)
 echo $_GET['param1'];
 echo $_GET['param2'];
with the idea of displaying the content of the variable just to be sure that they got through okay. I want a feature like alert in javascript where i can acknowledge before the program continues.

Right i don't see anything displayed.

Hope you can help.
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #7 on: March 17, 2007, 01:09:49 PM »

This is the PHP file I'm using with the example HTML page I posted.

Code
Language: php (GeSHi-highlighted)
<?PHP
$ltranscur = $_GET["param1"];
$lpaycur = $_GET["param2"];
$xrate = $ltranscur * $lpaycur;
echo $ltranscur.",".$lpaycur.",".$xrate;
?>

Which is working with:

Code
Language: gml (GeSHi-highlighted)
<?PHP
/********************** PAGE HEADERS *********************/
// no browser caching of this page !! to be used on all pages
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
/*********************************************************/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Preacher AJAX</title>
<script type="text/javascript">
<!--
var url2 = "http://www.cafesong.com/ert/getExchrate.php";
var objRequest;
 
function getExchrate() {
var transcrncy = document.moneytrans.transcrncy.value;
var paycrncy = document.moneytrans.paycrncy.value;
 
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
objRequest = new XMLHttpRequest();
if (objRequest.overrideMimeType) {
objRequest.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // II
objRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
 
if (!objRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
objRequest.onreadystatechange = handleHttpResponse2;
var params = '?param1='+transcrncy+'&param2='+paycrncy;
objRequest.open("GET", url2+params, true);
objRequest.send(null);
}

function handleHttpResponse2() {
if (objRequest.readyState == 4) {
if (objRequest.status == 200) {
var results2 = objRequest.responseText;
objRequest=null;
var arr=results2.split(',');
// arr[0] is param1
// arr[1] is param2
// alert(results2)
document.moneytrans.exchrate.value = arr[2];
document.moneytrans.rawResults.value = results2;
}
}
}
//-->
</script>
<style>
<!--
.code        { font-family: monospace; font-size: 10pt; color: #000000;
              border: 1px dashed #000000; padding: 5px; background-color:
              #FFFFCC }
-->
</style>
</head>
 
<body>
<form name="moneytrans">
 <p>Trans Currency <input type="text" name="transcrncy" value="10"></p>
 <p>Pay Currency <input type="text" name="paycrncy" value="121"></p>
 <p>Exchange Rate <input type="text" name="exchrate"></p>
 <p>Raw Results <input type="text" name="rawResults"></p>
 <p><input type="button" value="Get Exchange Rate" name="b1" onclick="getExchrate();"></p>
</form>
<p>AJAX Code</p>
<div class="code">
&lt;?PHP<br>/********************** PAGE HEADERS *********************/<br>// no browser caching of this page !! to be used on all pages<br>// Date in the past<br>header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");<br>// always modified<br>header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");<br>// HTTP/1.1<br>header("Cache-Control: no-store, no-cache, must-revalidate");<br>header("Cache-Control: post-check=0, pre-check=0", false);<br>// HTTP/1.0<br>header("Pragma: no-cache");<br>/*********************************************************/<br>?&gt;<br>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;<br>&lt;html lang="en"&gt;<br><br>&lt;head&gt;<br>&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;<br>&lt;title&gt;Preacher AJAX&lt;/title&gt;<br>&lt;script type="text/javascript"&gt;<br>&lt;!--<br>var url2 = "http://www.cafesong.com/ert/getExchrate.php";<br>var objRequest;<br><br>function getExchrate() {<br>&nbsp;&nbsp;&nbsp;&nbsp;var transcrncy = document.moneytrans.transcrncy.value;<br>&nbsp;&nbsp;&nbsp;&nbsp;var paycrncy = document.moneytrans.paycrncy.value;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;if (window.XMLHttpRequest) { // Mozilla, Safari, ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRequest = new XMLHttpRequest();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (objRequest.overrideMimeType) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRequest.overrideMimeType('text/xml');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;} <br>&nbsp;&nbsp;&nbsp;&nbsp;else if (window.ActiveXObject) { // II<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRequest = new ActiveXObject("Microsoft.XMLHTTP");<br>&nbsp;&nbsp;&nbsp;&nbsp;} <br><br>&nbsp;&nbsp;&nbsp;&nbsp;if (!objRequest) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('Giving up :( Cannot create an XMLHTTP instance');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;objRequest.onreadystatechange = handleHttpResponse2;<br>&nbsp;&nbsp;&nbsp;&nbsp;var params = '?param1='+transcrncy+'&param2='+paycrncy;<br>&nbsp;&nbsp;&nbsp;&nbsp;objRequest.open("GET", url2+params, true);<br>&nbsp;&nbsp;&nbsp;&nbsp;objRequest.send(null);<br>}<br> <br>function handleHttpResponse2() {<br>&nbsp;&nbsp;&nbsp;&nbsp;if (objRequest.readyState == 4) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (objRequest.status == 200) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var results2 = objRequest.responseText;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRequest=null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var arr=results2.split(',');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// arr[0] is param1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// arr[1] is param2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// alert(results2)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.moneytrans.exchrate.value = arr[2];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.moneytrans.rawResults.value = results2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}&nbsp;&nbsp;&nbsp;&nbsp;<br>//--&gt;<br>&lt;/script&gt;<br>&lt;/head&gt;<br><br>&lt;body&gt;<br>&lt;a href="javascript:void(0);" onclick="getExchrate();"&gt;Click Me&lt;/a&gt;<br>&lt;form name="moneytrans"&gt;<br>  &lt;p&gt;Trans Currency &lt;input type="text" name="transcrncy" value="10"&gt;&lt;/p&gt;<br>  &lt;p&gt;Pay Currency &lt;input type="text" name="paycrncy" value="121"&gt;&lt;/p&gt;<br>  &lt;p&gt;Exchange Rate &lt;input type="text" name="exchrate"&gt;&lt;/p&gt;<br>  &lt;p&gt;Raw Results &lt;input type="text" name="rawResults"&gt;&lt;/p&gt;<br>&lt;/form&gt;<br>&lt;div class="code"&gt;<br><br>&lt;/div&gt;<br>&lt;/body&gt;<br><br>&lt;/html&gt;<br>
</div>
<p>PHP</p>
<div class="code">
&lt;?PHP<br>
$ltranscur = $_GET[&quot;param1&quot;];<br>
$lpaycur = $_GET[&quot;param2&quot;];<br>
$xrate = $ltranscur * $lpaycur;<br>
echo $ltranscur.&quot;,&quot;.$lpaycur.&quot;,&quot;.$xrate;<br>
?&gt;
</div>
</body>
 
</html>

So start with those... then start adding your database lookup.
Logged

Rod
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #8 on: March 17, 2007, 01:59:44 PM »

the preacher, given your calls to the PHP script are done "behind the mantle" by your javascript, echoing stuff just modifies the response sent (or not ;-)
I suggest you first test your PHP script online in your browser, playing with param1 and param2 values to see what is returned
then, go back to your javascript, abd prepare the URI to call in a variable.
alert() this once to check hat will be sent exatly (should match your tests on the PHP script itself)
then perform the call, get the response and immediately alert() it and then stop (exit() or the like)

this should enable you to debug
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: [1]
« previous next »
    Jump to: