I am searching for a better way to send my PHP variable to Javascript. Currently I echo the variable in PHP:
<script>
var numbers = <?php echo $numbers ?>;
</script>
I then access this variable in Javascript like this: class.method(numbers);
My PHP file is included in the index.php before the javascript file, so I have access to this variable in the javascript file.
Now I am looking for a nicer way to submit the PHP variable than using echo
. Does anyone have an idea?
The output of numbers looks like:
5
Answers
If you want to mix your php with JS .the one you have used is better .also you can try something like this
Add a display none span to your html page with id :document_target
example
in js file get data of span with id
One better alternative would be to output the PHP value to a
data
attribute. Given that this variable doesn’t seem to be tied to a specific element, you could put it at root level on thebody
element. Then you can read it from there when the JS executes once the DOM has loaded, like this:This is under the assumption that the
$numbers
variable in your PHP is holding a value which can be coerced to a string— Update —
Given the edit to your question where you clarify that
$numbers
in fact contains an object, you can instead encode it to JSON, output it to a<script>
tag and then use it from there:You can use an inline script tag to add data to the HTML document like this:
As can be seen, when running the example, the script with inappropriate JS type attribute is not executed.
In that script tag you can echo variables or other values as you need (like in the latter example), or even echo a
json_encode
d string. This is one of the safest way to include PHP variables to JS, and you can still save your actual JavaScript into an external file. The data script must be included in the HTML document, because you can’t access .js files with PHP, and also, for security reasons, JS can’t readtext
from an external file.What comes to the usage of
echo
, it’s a PHP language construct specifically meant to add content to the markup, there’s no reason to avoid it. I’ve used a short hand ofecho
in the latter example, but it still isecho
.Here is a possible way to do this:
Get the values from the form fields if there’s a form, or whatever and send them in a query to php from JS, this work regardless where the php file is, it can be a remote file on a different server, or just another local page,etc.
The php file receives the request and reply back
retrieve the reply of the php file in the JS.
An example of the above using jquery:
Now in the php file you need to make the file react whenever it recieves something containing the line I indicated above containing
some_requestPHP_name
, this is a parameter that you decideDepending on your usage you may not have a form/button etc so you may want to remove the .change() and execute the function right away, at this point it’s up to you to adapt the example to your needs.
Well, I did it like this:
See, that
onClick='returnValues(".$x.");'
have inside itself variable from PHP. So you will give varibale to javascript using function. Now it is like you probably know:Now you could be good, when you will have any other questions, feel free to ask 😀
Have a nice day!