Jump to content

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
https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/
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

<?php
   $php_var = 100;

echo "
<SCRIPT type='text/javascript'>
   var js_var = $php_var;
   function display_var() {
      alert(js_var);
   }
</SCRIPT>
<body onload='display_var()'>
      ..........
     ..........
</body>";
?>

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.

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 :)

 

 

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?

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

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!

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.