www.webdeveloper.com
Results 1 to 9 of 9

Thread: variables blank outside of function

  1. #1
    Join Date
    May 2007
    Posts
    105

    variables blank outside of function

    This should be easy for me, but for the life of me, I can't figure out what I'm doing wrong. Here is my code:
    Code:
    var dateTime = "";
    var lastTime = "";
    var gauges = new Array();
    $.post( 'getData.php', function(data) {
    	var json = jQuery.parseJSON(data);
    	dateTime = json.dateTime;
    	lastTime = json.lastTime;
    	gauges = json.gauges;
    	console.log(dateTime);
    });
    var stringToPrepend = dateTime + '- Comparing Against Percentage Reported at ' + lastTime + '<br />';
    When I log out 'dateTime' in the function, it works perfect, but my 'stringToPrepend' has blank spots where the two variables are supposed to be. Any ideas?
    Thanks
    Mike

  2. #2
    Join Date
    Jan 2004
    Posts
    484
    can I see the code on
    getData.php
    ?
    Natdrip :P
    "water go down the hole" - plucky duck

  3. #3
    Join Date
    May 2007
    Posts
    105
    Sure, but like I said, when I console out one of the values from it, it gives the correct value. So I'm pretty sure that is working fine.

    PHP Code:
    <?php 

        $dateTime 
    $lastTime "";
        
    $gauges = array();

        if ((
    $handle fopen("gauges.csv""r")) !== FALSE) {
            while ((
    $data fgetcsv($handle1000",")) !== FALSE) {
                
    $firstCol trim(strtolower($data[0]));
                if(
    $firstCol == 'datetime') {
                    
    $dateTime $data[1];
                } else if(
    $firstCol == 'lasttime') {
                    
    $lastTime $data[1];
                } else if(
    strpos($firstCol'gauge') !== FALSE) {
                    
    $gauge = array($data[1], $data[2], $data[3], $data[4]);
                    
    $gauges[] = $gauge;
                }
            }
        }
        
    $values = array('dateTime'=>$dateTime'lastTime'=>$lastTime'gauges'=>$gauges);
        echo 
    json_encode($values);

    ?>

  4. #4
    Join Date
    May 2007
    Posts
    105
    Here is something interesting. I added a console out after "var stringToPrepend = dateTime..." and I saw that line got outputted before the line in the post function. Is it possible the code after the function is running before the function returns?

  5. #5
    Join Date
    Aug 2012
    Posts
    13
    you are trying to use the variables you got of the ajax call outside your call, this is not possible. And indeed, it could be possible that the function is still busy when ur creating the variable stringToPrepend.

    EDIT: this would work..

    Code:
    $.post( 'getData.php', function(data) {
    	var json = jQuery.parseJSON(data);
    	dateTime = json.dateTime;
    	lastTime = json.lastTime;
    	gauges = json.gauges;
    
            var stringToPrepend = dateTime + '- Comparing Against Percentage Reported at ' + lastTime + '<br />';
    });
    Last edited by LukieB; 08-30-2012 at 08:26 AM.

  6. #6
    Join Date
    May 2007
    Posts
    105
    Aw, thanks LukieB. I'll need stringToPrepend outside of that function and I'll need gauges outside of that function. How can I access it?
    Last edited by dmikester1; 08-30-2012 at 08:46 AM.

  7. #7
    Join Date
    May 2007
    Posts
    105

    resolved

    I figured it out from this post: http://stackoverflow.com/questions/6...-post-function
    Thanks!
    Mike

  8. #8
    Join Date
    Aug 2012
    Posts
    13
    ah, was just about to post that

    good thing u figured it out urself.

  9. #9
    Join Date
    May 2004
    Location
    Manhattan NY
    Posts
    6,028
    That's exactly what's happening. The last line is running before the vars are populated. They're printing null strings because that's what they're set to.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles