Jump to content

Variable conversion PHP to Javascript


NorKayak

Recommended Posts

Hi;

I'm a beginner and I'm getting a big headache figuring how to convert php variables into Javascript variables.
I want to use the content of $theName as the value for an input tag. $_SESSION['$Sess_Name'] is loaded with the right data, I verified it. But my function InitField returns always the word NULL.
Why is that? Where is the problem?

Thank

 

Here is my code:

 

function InitField() {

    <?php
        $theName = $_SESSION['$Sess_Name'];
    ?>
    var tempoVar = "<?php echo json_encode($theName); ?>";
    var myInput = document.getElementById('theSurname');
    myInput.value = tempoVar;
}
 
 
Link to comment
Share on other sites

 

It looks like json_encode() adds the quotes for you. Try changing this:

var tempoVar = "<?php echo json_encode($theName); ?>";
 
To this:
var tempoVar = <?php echo json_encode($theName); ?>;

 

Ok, I tried. Still not working. The diference is before I had the output "NULL" and now nothing...

Link to comment
Share on other sites

When viewing the page in your browser, does the correct name appear in the source code?

 

Are you getting any JavaScript errors? In case you're not aware, you can right-click the page in a browser like Chrome. Then click the Inspect Element option. The errors would be displayed in the Console tab.

Thanks I didn't think about that... I checked and the codes reads: var tempoVar = null;

It seems then that the $_SESSION variable is not recognized in the function. Could that have something to do with declaring the variable global? So I tried this:

 
<?php
$theName = global $_SESSION['$Sess_Name'];
?>
 
But the browser doesn't accept it...
Link to comment
Share on other sites

You mean I should remove the $ sign?

 

That depends. If your SESSION variable is named with a dollar sign, it should stay. I was just checking to see if the dollar sign was put there on purpose.

 

 

<?php

ob_start();
    session_start();
 
?>

 

Did you turn off the output buffer after the session_start() call? That way you can see if PHP is throwing errors.

 

If no errors are showing up, do you know if errors are being hidden by the server? Note that you can add the following lines at the top of your script...probably after your session_start() call:

//REPORT ALL PHP ERRORS
error_reporting(E_ALL);
ini_set('display_errors', 1);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.