36 users online (2 members and 34 guests)  

Thread: PHP Variable


  Results 1 to 4 of 4

Related

  1. convert a perl variable to javascript variable    Forum: Javascript Forum
    Replies: 0
  2. Problem with variable on windows    Forum: PHP Forum
    Replies: 5
  3. Can I get a variable name as a string?    Forum: PHP Forum
    Replies: 2
  4. Help!! - passing variable    Forum: Website Scripts Forum
    Replies: 0
  5. Replies: 5
  1. #1
    Pongo's Avatar
    New User

    Status
    Offline
    Join Date
    Jan 2012
    Posts
    2

    PHP Variable

    I have a question about accessing a PHP var in JavaScript. Let me show everyone my code...

    <head>
    //This reads a file off the server and makes it an array
    <?php
    $handle = @fopen("testFile.txt", "r");
    if ($handle) {
    $c=0;
    while (($buffer = fgets($handle, 4096)) !== false) {
    $arrQuestion[$c++] = $buffer;
    }
    if (!feof($handle)) {
    echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
    }
    //This test-displaying of the array works
    print_r($arrQuestion);
    ?>

    </head>
    <body>
    <script type="text/javascript">

    //This test does not work. This next line stops the JavaScript.

    var testVar = <?php echo $arrQuestion[0];?>;
    document.write(testVar);

    </script>
    </body>

    I've worked through every iteration of syntax in the JavaScript code that I could think of. None of them work. I think that maybe the problem is that JavaScript is trying to display the var before the PHP can read it. However, I always thought all PHP was executed first. I dunno and I've exhausted all my ideas. If anyone has any solutions, I would be very grateful. Thank you.

  2. #2
    Pongo's Avatar
    New User

    Status
    Offline
    Join Date
    Jan 2012
    Posts
    2

    Re: PHP Variable

    After spending more hours than I would like to admit, I have come to the conclusion that it is not a JavaScript problem, or rather, the solution is not located in JavaScript. I needed to apply chop() in PHP when writing the variable to JavaScript. Hope maybe this can help someone someday.

  3. #3
    grace's Avatar
    New User

    Status
    Offline
    Join Date
    Apr 2009
    Location
    Misery
    Posts
    9

    Re: PHP Variable

    Only a bit of a glance; but isn't that 'maintainance referral input "jargon"' I have NO idea how I came up with that, I hope its helpful.

  4. #4
    016hnoor's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2015
    Posts
    1

    Re: PHP Variable

    I think that maybe the problem is that JavaScript is trying to display the var before the PHP can read it. However, I always thought all PHP was executed first.



    _____________________________________________________________________________________________
    NOOR