Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post

    getting data back from a php function

    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.
    Last edited by Paul Williams; 01-19-2012 at 05:33 AM. Reason: resolved

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    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-

    Code:
    <?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-

    Code:
    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
        }
    };
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • Users who have thanked nomanic for this post:

    Paul Williams (01-18-2012)

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    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

  • #4
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    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
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • Users who have thanked nomanic for this post:

    Paul Williams (01-19-2012)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,519
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Paul Williams (01-19-2012)

  • #6
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post

    Smile

    Nomanic,
    Old Pendant,

    thank you both.
    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!!

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,519
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •