|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How do you communicate values to Javascript?
e.g.
I have the following variable defined in a PHP file(error.php) being required into another add form (say, add.php): $error_message = "Please enter something"; In add.php, I 'require' / source in a javascript file. The javascript file contains a function to detect if something is empty upon me saving something. If it is, it prompts using alert(). What I have in this alert function is not text but I put in PHP variables like the following; alert ('<?=$error_message?>'); However, it didn't work. Instead, it echoed the whole variable out literally. How do I communicate PHP variables contained in a required PHP file to the included javascript file's function? I hope I made myself understood. Please help because I need this in my multilingual project. thanks
__________________
Beginner |
|
#2
|
|||
|
|||
|
could you show us what printed out?
|
|
#3
|
|||
|
|||
|
Your web server may have short tags disabled, use <?php instead of <?
Also, make sure the variables are defined as global in your PHP script if you're outputting the JavaScript in a function: <?php function myFunc() { global $var1; global $var2; ... Lastly, make sure you replace ' with \' when you output the variables from PHP. |
|
#4
|
|||
|
|||
|
For instance, if I say alert('<?=$char?>'); and $char = "A";
I am supposed to see the character 'A' in my pop up box but instead I see the whole literal <?=$char?> string And no, it's nothing to do with the short tags. Lastly, what do you mean by the global declarations?Didn't quite get you. |
|
#5
|
|||
|
|||
I thought this was so easy.....And i first started learning php a month ago..Anyway Tim. Ive added your icq. Its best to see more of the exact code cus im rubbish at explaining things and me trying to explain may not help you. Here is what u have right? Code:
.....ert('<?=$char?>'); a....
So what you do...way b4 u call the variable $char u must add the ' and ' at the start and end. so like this. Code:
<?php $char = "'".$char."'"; echo($char); ?> So your final code wud be. Code:
alert (<?php $char="'".$char."'"; echo($char); ?>); Remember to keep the spaces. There important. Unlike u missed them out in ur example. Im sure this is not the best way to do this but as im a beginner that is my way. Anyone has a shorter way then post plz. To see in action visit http://www.3doms.com/private/dump/tim.php |
|
#6
|
||||
|
||||
|
Wow, a four year old thread...
![]()
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
|
#7
|
|||
|
|||
|
Quote:
lol. i didnt realise. found this post while searching google for something so signed up n answered I shudnt expect an answer then lol. |
|
#8
|
||||
|
||||
|
I didn't mean to discourage or degrade the answer.
It might help others who stumble across the thread on Google. |
|
#9
|
|||
|
|||
Safe! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Programming Tools > How do you communicate values to Javascript? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|