php var to javascript var
Send PHP variable to javascript function [duplicate]
I have a php file that generates a variable and I would like the variable to be put into a javascript function which is called by onclick on the main page. Is this possible to send from PHP to javascript?
6 Answers 6
You can do the following:
Now it’s available as a JavaScript variable by the name of my_variable_name at any point below the above code.
Your JavaScript would have to be defined within a PHP-parsed file.
For example, in index.php you could place
If I understand you correctly, you should be able to do something along the lines of the following:
A great option is to use jQuery/AJAX. Look at these examples and try them out on your server. In this example, in FILE1.php, note that it is passing a blank value. You can pass a value if you wish, which might look something like this (assuming javascript vars called username and password :
In the FILE2.php example, you would retrieve those values like this:
Then do your MySQL lookup and return the values thus:
This would deliver the message You are logged in to the success function in FILE1.php, and the message string would be stored in the variable called «data». Therefore, the alert(data); line in the success function would alert that message. Of course, you can echo anything that you like, even large amounts of HTML, such as entire table structures.
The approach is to create your form, and then use jQuery to detect the button press and submit the data to a secondary PHP file via AJAX. The above examples show how to do that.
The secondary PHP file receives the variables (if any are sent) and returns a response (whatever you choose to send). That response then appears in the Success: section of your AJAX call as «data» (in these examples).
The jQuery/AJAX code is javascript, so you have two options: you can place it within tags within your main PHP document, or you can at the bottom of your PHP document. If you are using jQuery, don’t forget to include the jQuery library as in the examples given.
In your case, it sounds like you can pretty much mirror the first example I suggested, sending no data and receiving the response in the AJAX success function. Whatever you need to do with that data, though, you must do inside the success function. Seems a bit weird at first, but it works.
How to get JavaScript variable value in PHP
I want the value of JavaScript variable which i could access using PHP. I am using the code below but it doesn’t return value of that variable in PHP.
this gives me the following error :-
Another php code i used which give empty value
When I echo it shows nothing.
10 Answers 10
You will need to use JS to send the URL back with a variable in it such as: http://www.site.com/index.php?uid=1
by using something like this in JS:
Add a cookie with the javascript variable you want to access.
Then acces it in php via
Here is the Working example: Get javascript variable value on the same page.
You might want to start by learning what Javascript and php are. Javascript is a client side script language running in the browser of the machine of the client connected to the webserver on which php runs. These languages can not communicate directly.
Depending on your goal you’ll need to issue an AJAX get or post request to the server and return a json/xml/html/whatever response you need and inject the result back in the DOM structure of the site. I suggest Jquery, BackboneJS or any other JS framework for this. See the Jquery documentation for examples.
If you have to pass php data to JS on the same site you can echo the data as JS and turn your php data using json_encode() into JS.
Access PHP var from external javascript file
I can access a PHP var with Javascript like this:
But what if I want to use an external JS file:
12 Answers 12
You don’t really access it, you insert it into the javascript code when you serve the page.
However if your other javascript isn’t from an external source you can do something like:
and then in the file.js use color like so:
You can also access data from php script in Javascript (I’ll use jQuery here) like this
Create input hidden field within you php file like this
in your javascript file:
This will do the job as well 🙂
What I do recommend is fetching the values through AJAX and have the PHP file return the value to the JS file. Which is a much cleaner method.
First of all you have to understand that no program can actually have access to the other program’s variable.
When you realize that, the rest is simple.
You can either set up a js variable in the main file and then include your external js, or make this external js dynamic, generated by PHP as well
What you likely want, is called Asynchronous JavaScript and XML (AJAX): http://www.w3schools.com/ajax/default.aspa
It helps to have some understanding of taint checking, data verification, and security 😉
As the others are saying, javascript doesn’t have access to php variables. However, it does have access to the DOM. So, you can use php to add attributes to some page element. And then you can access those attributes with javascript.
How do I pass JavaScript variables to PHP?
I want to pass JavaScript variables to PHP using a hidden input in a form.
16 Answers 16
You cannot pass variable values from the current page JavaScript code to the current page PHP code. PHP code runs at the server side, and it doesn’t know anything about what is going on on the client side.
You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.
Just save it in a cookie:
And then read it with PHP:
It’s not a pretty solution, but it works.
There are several ways of passing variables from JavaScript to PHP (not the current page, of course).
Here is the Working example: Get javascript variable value on the same page in php.
Here’s how I did it (I needed to insert a local timezone into PHP:
I was trying to figure this out myself and then realized that the problem is that this is kind of a backwards way of looking at the situation. Rather than trying to pass things from JavaScript to php, maybe it’s best to go the other way around, in most cases. PHP code executes on the server and creates the html code (and possibly java script as well). Then the browser loads the page and executes the html and java script.
It seems like the sensible way to approach situations like this is to use the PHP to create the JavaScript and the html you want and then to use the JavaScript in the page to do whatever PHP can’t do. It seems like this would give you the benefits of both PHP and JavaScript in a fairly simple and straight forward way.
One thing I’ve done that gives the appearance of passing things to PHP from your page on the fly is using the html image tag to call on PHP code. Something like this:
Get variable from PHP to JavaScript [duplicate]
I want to use a PHP variable in JavaScript. How is it possible?
6 Answers 6
You can print the PHP variable into your javascript while your page is created.
Of course this is for simple variables and not objects.
You can pass PHP Variables to your JavaScript by generating it with PHP:
It depends on what type of PHP variable you want to use in Javascript. For example, entire PHP objects with class methods cannot be used in Javascript. You can, however, use the built-in PHP JSON (JavaScript Object Notation) functions to convert simple PHP variables into JSON representations. For more information, please read the following links:
You can generate the JSON representation of your PHP variable and then print it into your Javascript code when the page loads. For example:
I think the easiest route is to include the jQuery javascript library in your webpages, then use JSON as format to pass data between the two.
In your HTML pages, you can request data from the PHP scripts like this:
In bar.php you can do this:
This is what’s usually called AJAX, and it is useful to give web pages a more dynamic and desktop-like feel (you don’t have to refresh the entire page to communicate with PHP).
Other techniques are simpler. As others have suggested, you can simply generate the variable data from your PHP script:
Most web pages nowadays use a combination of the two.
This is the easiest way of passing a php variable to javascript without Ajax.
You can also use something like this:
This said to be safer or more secure. i think
Update: I completely rewrote this answer. The old code is still there, at the bottom, but I don’t recommend it.
There are two main ways you can get access GET variables:
With PHP, you can just make a «template», which goes something like this:
However, I think the mixture of languages here is sloppy, and should be avoided where possible. I can’t really think of any good reasons to mix data between PHP and JavaScript anyway.
It really boils down to this:
In the original answer, I had two methods for getting the query string, but it was too messy and error-prone. Those are now at the bottom of this answer.
Anyways, I designed a nice little «class» for getting the query string (actually an object constructor, see the relevant section from MDN’s OOP article):
It’s much more robust, doesn’t rely on regex, combines the best parts of both the previous approaches, and will validate your input. You can give it query strings other than the one from the url, and it will fail loudly if you give bad input. Moreover, like a good object/module, it doesn’t know or care about anything outside of the class definition, so it can be used with anything.
All the work is done for you at instantiation.
Here’s how to use it:
That’s much better. And leaving the url part up to the programmer both allows this to be used in non-browser environments (tested in both node.js and a browser ), and allows for a scenario where you might want to compare two different query strings.
As I said above, there were two messy methods that are referenced by this answer. I’m keeping them here so readers don’t have to hunt through revision history to find them. Here they are:
Here comes the second method:
2) Object Build by Loop
AND this is possible
This is definitely not possible with the function.
Again, I don’t recommend this old code. It’s badly written.