...

View Full Version : Resolved getting data back from a php function



Paul Williams
01-18-2012, 07:26 PM
overview:
from my javascript,
i call a function in the php block,
that function successfully retrieves a value from the database,
but when i pass that retrieved value back to the calling function....all is lost somehow.

when i test this code by itself, it works fine and gets the correct number.

<?php
mysql_select_db($database_connFtH, $connFtH);
$result = mysql_query("SELECT status_index FROM cpqc_status ORDER BY row_id DESC");
$row_result = mysql_fetch_assoc($result);
$index = $row_result['status_index'];

echo $row_result['status_index'];
echo $index;
echo "the value of index is ".$index;
?>

however, when i set this same code up as a function to be called upon globally, and to return the value of $index, it all just seems to "not work".

<?php
function runStatus() {
mysql_select_db($database_connFtH, $connFtH);
$result = mysql_query("SELECT status_index FROM cpqc_status ORDER BY row_id DESC");
$row_result = mysql_fetch_assoc($result);
$index = $row_result['status_index'];
return ($index);
} //end function runStatus()
?>

the calling function, with an alert box to check if the value of $index was passed down accurately

<script type="text/javascript">
var x = runStatus();
alert(x);
</script>

all i want to do is call a server-script function that deals with the database and gets me a value out of the database, and pass that value back to the calling function. can't figure out what is wrong. the "return($index)" should send back the value of $index. the call to that function should receive back a value and assign it to "x". and the alert(x) should spit it out with no problem. but......something is wrong and it won't work.

any ideas what is wrong?

it is my understanding that a function inside the php determants can be called upon from anywhere and that function can return a value.(?)

argh!! i think i know why Einstein's hair was always so messy!!

thanks,
Paul Williams

weather today in Kalispell, MT:
7 deg this morning, got a little snow last night, only about 6 or 8 inches, not much.
maybe more later.

nomanic
01-18-2012, 09:54 PM
php is interpreted by the server, server side
javascript is interpreted by the browser, client side

these 2 functions are in totally different worlds at different times, they do no coexist within the browser or server at the same time

if you want to call a php function from javascript you must call the page holding the php function on the server via ajax
the php file then outputs the answer as a text page, which is then interpreted by javascript

keep your php page as-



<?php
mysql_select_db($database_connFtH, $connFtH);
$result = mysql_query("SELECT status_index FROM cpqc_status ORDER BY row_id DESC");
$row_result = mysql_fetch_assoc($result);
$index = $row_result['status_index'];

echo $index;
?>


say its called func.php

then in your javascript have-



var json,iC=false,dsTA=[],dTA=[],cTA=[],rTA=[],ajs=[],qwt=-1,htp=(document.all)?(new ActiveXObject("Microsoft.XMLHTTP")):(new XMLHttpRequest());
l_8=function(r){return r?r.length:0;};
sT=function(n,t){return setTimeout(n?n:"",(t?t:10));};
sC=function(w,r,f){cTA.push(w);rTA.push(r);ajs.push(f);if(qwt!==-1){clearTimeout(qwt);}qwt=sT("cQ();",100);};
cQ=function(w,r){if(!iC&&(l_8(cTA)>0)){w=cTA.shift();r=rTA.shift();iC=true;htp.open('post',w);eval("htp.onreadystatechange="+r);htp.send(null);}qwt=(l_8(cTA)>0)?sT("cQ();",5000):-1;};

//this line where you want to request it
sC("func.php",caller2,[]);

//this function to collect it
caller2=function() {
if (htp.readyState===4){
iC=false;
ft=ajs.shift();
ln=htp.responseText;
alert(ln); //ln now contains value from php file, but remember its a string
}
};

Paul Williams
01-18-2012, 11:39 PM
nomanic,
thanks for your response.
that sure seems like alot of work for just retrieving a value from the database(?).

so let me get this straight.
the index.php file running on the browser can connect to the db,
the index.php file running on the browser can add things to the db,
the index.php file running on the browser can edit things in the db,
the index.php file running on the browser can delete things in the db,

BUT..................
the index.php file running on the browser CANNOT retrieve a value from the db and use that value to do something?
unless you jump through all these extra hoops, right?

thanks,
Paul

nomanic
01-19-2012, 12:21 AM
the php file can do anything to the database on the SERVER
the javascript file can do anything in the BROWSER
trouble is they are in 2 different places, even if both in the same file,
one is run SERVER side, the other client side in the BROWSER

Old Pedant
01-19-2012, 12:51 AM
You misstated the problem.

the index.php file running on the browser

A php file *NEVER* runs "on the browser".

It *ONLY* runs on the server.

The php file *creates* HTML which it then *SENDS* to the browser, and the browser "runs" that HTML (which could include JavaScript). But the browser CAN NOT see or execute PHP code in any way, shape, or fashion.

To see what a PHP page has created in the way of HTML, do this:
-- Bring that PHP page up in your browser
-- Click on the VIEW menu of your browser
-- Click on the SOURCE (or PAGE SOURCE) menu item.

A new window will appear (or it might be Notepad, if you are using MSIE) which will show you the HTML code as the browser sees it (again, possibly including JavaScript code and/or CSS). That is *ALL* the browser sees. If you don't see your code there, then the browser doesn't see it, either.

So, again, it's a mistake to think of a PHP page (or ASP page or JSP page or...) as "running on the browser." Those kinds of pages run only on the server and GENERATE the HTML code that is seen by the browser.

Paul Williams
01-19-2012, 05:32 AM
Nomanic,
Old Pendant,

thank you both.:thumbsup:
now i understand.

at first i wasn't sure, so i went back to my AJAX text and re-read it very slowly and very carefully; and this time i understood what they were saying.

thank you very much, this was confusing but now it makes sense.
will try to set up the code, shouldn't have trouble with it.
fingers crossed!!

Paul
ps. were getting dumped on with a ton of snow,
coming in from Seattle storm. oh yea!!

Old Pedant
01-19-2012, 05:37 AM
Hey, we got it dumped here, first. But not as badly as places like Olympia got hit.

(We're in Snohomish, but at the 500 foot level, so we always get more than the valley floor and/or Seattle.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum