473,395 Members | 1,823 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,395 software developers and data experts.

Use php variable in javascript function

Hello guys.
I have a question about js function and php variables.

I want to use php variables in javascript function. For example,
<?php
$ex=(int)$_POST['Example'];
?>

$ex value is 3

There's js function named "practice"
I want to run

<input type="button" onclick="practice(3); ">

in this case, 3 is $ex. However, I cannot use like this:

<input type="button" onclick="practice( <? $ex ? ); ">

How can I use php variables in javascript function? Thank you.

Oct 19 '06 #1
3 1870
Oh, I find out!.
it's just

<input type="button" onclick="practice( '<? echo $ex; ?>' ); ">

right?

em********@gmail.com wrote:
Hello guys.
I have a question about js function and php variables.

I want to use php variables in javascript function. For example,
<?php
$ex=(int)$_POST['Example'];
?>

$ex value is 3

There's js function named "practice"
I want to run

<input type="button" onclick="practice(3); ">

in this case, 3 is $ex. However, I cannot use like this:

<input type="button" onclick="practice( <? $ex ? ); ">

How can I use php variables in javascript function? Thank you.
Oct 19 '06 #2
<em********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Oh, I find out!.
it's just

<input type="button" onclick="practice( '<? echo $ex; ?>' ); ">

right?

Yes, that works. You can assign javascript variables with php like that,
just remember that it doesn't work the other way though, you can't assign
php variables with javascript. :)

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Oct 19 '06 #3
You have to keep in your mind that, in fact, PHP only mission is to 'paint'
the data that travel to the user browser.

Knowin' that, you can 'send' html, javascritp, css... plain text, or what
ever you want. Then the browser will treat is exactly as if it were reading
a normal .html file for example.
En las nuevas, el em********@gmail.com escribió:
Oh, I find out!.
it's just

<input type="button" onclick="practice( '<? echo $ex; ?>' ); ">

right?

em********@gmail.com wrote:
>Hello guys.
I have a question about js function and php variables.

I want to use php variables in javascript function. For example,
<?php
$ex=(int)$_POST['Example'];
>>>

$ex value is 3

There's js function named "practice"
I want to run

<input type="button" onclick="practice(3); ">

in this case, 3 is $ex. However, I cannot use like this:

<input type="button" onclick="practice( <? $ex ? ); ">

How can I use php variables in javascript function? Thank you.

Oct 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.