473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can I use a php variable in a javascript function?

108 100+
I want to use a php variable in my javascript.
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function display_alert(x){
  3. alert(x);
  4. }
  5. </script>
  6. <body>
  7. <?php $var1='phpvariable';
  8.  
  9. echo "<button onclick='display_alert($var1)'>Diaplay alert</button>";
  10. ?>
  11. </body>
I am not able to execute such a script..
I need to pass a php variable to a javascript function .Is that possible ?
If so,hw cn i implement this?
Thanks in adv
With regards
phpuser123
Apr 13 '10 #1

✓ answered by chathura86

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function display_alert(x)
  3. {
  4.     alert(x);
  5. }
  6. </script>
  7. <body>
  8. <?php 
  9.  
  10. $var1='phpvariable';
  11. $var2='display_alert("' . $var1 . '")';
  12.  
  13. echo "<button onclick=$var2>Display alert</button>";
  14. ?>
  15. </body>
try this

Regards

4 1490
Dormilich
8,658 Expert Mod 8TB
you need to pass the variable as JavaScript string, that is, surrounded by " (or ')
Apr 13 '10 #2
phpuser123
108 100+
I passed the variable within quotes as u mentioned..I got $var1 instead of phpvariable ...

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2.    function display_alert(x){
  3.   alert(x);
  4.   }
  5.   </script>
  6.   <body>
  7.   <?php $var1='phpvariable';
  8.  $var2='display_alert("$var1")';
  9.  
  10.  echo "<button onclick=$var2>Display alert</button>";
  11.  ?>
  12.  </body>
Is that what u exactly u mean ?
To some extent it worked bt I gt the name of my variable instead of the value it holds..
Apr 15 '10 #3
chathura86
227 100+
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function display_alert(x)
  3. {
  4.     alert(x);
  5. }
  6. </script>
  7. <body>
  8. <?php 
  9.  
  10. $var1='phpvariable';
  11. $var2='display_alert("' . $var1 . '")';
  12.  
  13. echo "<button onclick=$var2>Display alert</button>";
  14. ?>
  15. </body>
try this

Regards
Apr 15 '10 #4
phpuser123
108 100+
@chathura86
Thanks that worked out..
Apr 16 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: R.G. Vervoort | last post by:
I am using a javafunction (onclick in select) in which i am calling a function in php (thats why i send this to both php and javascript newsgroups). in the onclick i call the function...
7
by: Donna Hawkins | last post by:
I want to use javascript to redirect to a URL which has been passed as a variable (in php). I have searched but cannot find any solution. I think this code is a basic redirect: <script...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
8
by: nescio | last post by:
hello, i have got a javascript that opens a new window. when i call the javascript function i want to ad a variable. what you see on the pop up depends on the variable. but the variable in...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
11
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the...
0
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I want to display a progress bar on my web page. I have this code for progress bar in javascript in my web page. I have a button on the web page, if someone clicks that button...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
3
Atli
by: Atli | last post by:
Hi everybody. This is not so much a problem, since I have already managed to find a solution, but my solution requires the use of the eval() function, which I just hate to use. The problem is...
1
by: faultykid | last post by:
I would like to store a variable then call it back later. I have a variable on line 198 www = ''+this._ad.clickUrl+''; and on line 321 i try document.write(www);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.