-5

I have a small issue. I can't seam to pass a variable from PHP to JS.

Here is my JS:

var eName = '<?php echo $eName; ?>';

I know that $eName has a value. However in the JS section, I get nothing. no data seams to be present in the variable. when I echo in the PHP section, I get data.

Can anyone help?

Also tried:

var eName = '<?php echo json_encode($eName); ?>';

This give me a null result

Thanks for your help

Here is how I get my PHP Variable populated:

$sql="SELECT * FROM league WHERE id='". $lID ."' LIMIT 1";
    $result = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($result);
    if($numrows < 1){
        echo "league does not exist";
        exit();
    }
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $eName = $row["name"];
  • 1
    Sounds like a scoping issue in PHP. Are you sure the variable is defined in the global PHP scope? – Sirko Dec 01 '14 at 16:44
  • Can you please post your `$eName` echo result in PHP? – Sithu Dec 01 '14 at 16:44
  • Works fine for me! Also should work for you! Please show us more code – Rizier123 Dec 01 '14 at 16:44
  • 6
    Did you put your PHP code in a .js file? – TimWolla Dec 01 '14 at 16:44
  • $eName is not an array. I get a single name from my database. such as: "VIP", "8 Ball". Not sure I understand the global PHP scope question, I'll read on it. – Richard Moreau Dec 01 '14 at 16:45
  • Actually, it should be just `var eName = ;`, without any quotes; that's the safest way, unless `$eName` contains non-serializable data. Could you show what's put in the source code as a result? – raina77ow Dec 01 '14 at 16:45
  • You are pushing the PHP code in the JS File. JS cannot interpret the php code. You need to use JS ajax to load the data as a web service ot some other way around – Akhilesh Sharma Dec 01 '14 at 16:47
  • Raina77ow, I tried it already. The alert returns a NULL value. – Richard Moreau Dec 01 '14 at 16:48
  • 2
    I didn't mention any alert, did I? Check the source code directly. If it contains something like `var eName = null`, it means that `$eName` variable doesn't have any value when you echo it, for whatever reason there is - typo, scope issues etc. – raina77ow Dec 01 '14 at 16:49
  • I am using ajax. If I echo in PHP I see the value in my ajax.responseText – Richard Moreau Dec 01 '14 at 16:50
  • 1
    Wait, you are returning JavaScript in an Ajax response? – Felix Kling Dec 01 '14 at 16:50
  • all my code is in the php file directly – Richard Moreau Dec 01 '14 at 16:50
  • Felix, no i'm returning the PHP response to ajax – Richard Moreau Dec 01 '14 at 16:51
  • 1
    And the PHP response is what exactly? I don't understand how an Ajax response is related to `var eName = '';`. Please **[edit]** your question and provide more (complete) information. – Felix Kling Dec 01 '14 at 16:52
  • I just used the ajax response to make sure the variable was not empty in my php section. I tried to make the variables global, same thing. – Richard Moreau Dec 01 '14 at 16:58
  • Can you show the code that assigns a value to this variable? Is that code before or after section with JS (i.e., `var eName = ...`)? – raina77ow Dec 01 '14 at 17:06
  • The code is before. It is at the top of my file – Richard Moreau Dec 01 '14 at 17:08
  • I read and followed the instruction in this post: down vote favorite This question may already have an answer here: How to pass variables and data from PHP to JavaScript? This is how I got the code i'm using. Still it is still not working. This is why I am asking for help. – Richard Moreau Dec 01 '14 at 17:09

1 Answers1

0

According to your edited code, I guess your variable $eName would be empty for the last record of the while loop.

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $eName = $row["name"];
    // if the last record has the empty value for the field `name`, 
    // it will overwrite the previous value and
    // you will get `$eName` of no value out of the loop later
}

And moreover, make sure you are not using <?php ?> in the .js file which is not parsed by Javascript.

Sithu
  • 4,752
  • 9
  • 64
  • 110