Jump to content

read php variable from javascript


khadra4ever

Recommended Posts

Hi everybody!

I am new to php, I'm trying to read a php variable using javascript function

my code is as follow:

 

<?php

  $php_var = 100;

?>

 

<SCRIPT type="text/javascript">

  var js_var = "<?php =$php_var; ?>";

  function display_var() {

      alert(js_var);

  }

</SCRIPT>

<body onload="display_var()">

      ..........

    ..........

</body>

 

the problem is that the line

    alert(js_var);

gives me "<?php =$php_var; ?>"  instead of 100.

I don't know what I'm missing.

any help would be greatly appreciated.

Thanks in advance.

 

Link to comment
Share on other sites

trying putting the javascript inside <?php ?> tags, and echoing it out to the page. Then you should be able to print the variable. The problem is that one is client side, and the other is server side. I'm not sure if this would work as expected, but it's worth a try.

 

Denno

Link to comment
Share on other sites

lol, you're welcome... I told you what you should have done so you could learn for yourself, QuickOldCar merely gave you the code...

 

Anyway for your next question, can't do it. You need to use AJAX. Again, that reason is to do with where the code is executed.

Link to comment
Share on other sites

I think the only problem with the original code was

 

var js_var = "<?php =$php_var; ?>";

 

should be

 

var js_var = "<?=$php_var; ?>";

 

The quick tag syntax was incorrect... not sure it needed the whole script echo'd, but if it works :)

 

 

Link to comment
Share on other sites

lol, you're welcome... I told you what you should have done so you could learn for yourself, QuickOldCar merely gave you the code...

 

Anyway for your next question, can't do it. You need to use AJAX. Again, that reason is to do with where the code is executed.

 

Couldn't you just set a cookie?

Link to comment
Share on other sites

what about people who have cookies disabled, and whom are scared of cookies (yes I know people who know very little about computer and think that all cookies are viruses).

 

Using AJAX should mean no one would have problems, no matter their security settings.

 

Denno

Link to comment
Share on other sites

Thank you guys for your replies

actually i don't know how to use cookies but i used text file instead

i don't know if it's a good idea but it worked for me

what i did is i had php to write data from database and write the results in a temporary text file

then javascript reads that data into array, so it can populate a combobox.

 

once again thank you guys!

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.