Possible Duplicate:
How to pass JavaScript variables to PHP?
How to assign on this php variable assign javasript value
var var_example = "2222";
Waldi
35.5k6 gold badges22 silver badges67 bronze badges
asked Oct 27, 2012 at 5:41
5
You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript
answered Oct 27, 2012 at 5:44
Mr. AlienMr. Alien
149k33 gold badges288 silver badges272 bronze badges
4
You cannot do that. An alternative may be creating a cookie using javascript and then read the cookie in PHP.
In Javascript:
document.cookie = "cookieName=cookieValue";
And in PHP
But an important point, for this to run the browser needs reload.
Best regards...
answered Oct 27, 2012 at 6:08
avolquezavolquez
7431 gold badge7 silver badges20 bronze badges
2
You can do it using ob_start[]
function [documentation]. If you use this method you can call a function and parse the html content. You search for your variable and get it with php..
var var_example = "2222";
answered Oct 27, 2012 at 5:59
Mihai MateiMihai Matei
23.8k4 gold badges33 silver badges50 bronze badges
Please try this:
Use document.write function in javascript.
answered Oct 27, 2012 at 5:46
suresh.gsuresh.g
3,1186 gold badges26 silver badges58 bronze badges
6
Not the answer you're looking for? Browse other questions tagged php javascript or ask your own question.
To Set PHP Variable in JavaScript, we define a JS variable and assign PHP value using PHP tag with single or double quotes.
Most of the time you have to set PHP variable value in JavaScript. If there is only simple variable like string or any integer then you can simply echo on the JS variable with PHP tag.
For example if you have a string value on the PHP variable and want to assign it and console it on JS.
varnormalText=''; console.log[normalText];
|
Output:
Lorem ipsum dolor sit amet
If you have any array value in PHP and you want to get that value on the same JavaScript variable then,
varphpArray=; console.log[phpArray];
|
Output on Console:
[3] [“Lorem”, “ipsum”, Array[2]]
0: “Lorem”
1: “ipsum”
2: [2] [“dolor”, “amet”]
In This Article
- 1 Complete Example to Set PHP Variable in JavaScript
- 2 Pass PHP Variable as Parameter in JavaScript Function
- 2.1 Related
varnormalText=''; console.log[normalText]; varphpArray=; console.log[phpArray];
|
Output:
Here is the complete example, How to define PHP variable in JavaScript.
Pass PHP Variable as Parameter in JavaScript Function
varnormalText=''; functionprintaddition[userPHPNumber]{ alert[parseInt[userPHPNumber]+5]; } printaddition[normalText];
|
Output:
Alert 10
In this example Pass PHP Variable as Parameter in JavaScript Function,
- First, we define a PHP integer and then we create a JS function and
- Take the PHP variable on our JS variable.
- Then we call that function with JS variable as a parameter, Which alerts the addition of numbers.
I think this is the complete guide about access PHP variables in JavaScript and also set PHP variable value to JS variable.
We also use parseInt[]
to convert string to integer in JS. You also read know more from here //www.w3schools.com/jsref/jsref_parseint.asp
Also Check and Run Live on //phpcoder.tech/php-online-editor/
Also Check:
- How Does JavaScript Filter Function work [Complete Guide With Example]
- Get Selected Date From Calendar in PHP using Ajax
- Auto Add Country Code in Input using JavaScript
- How to Validate Date String in PHP
Happy Coding..!
Was this article helpful?
YesNo
Bikash
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.