/    Sign up×
Community /Pin to ProfileBookmark

How to use document.getElementById() with php?

How do I retrieve a php variable in JS? I tried using “document.getElementById()” but it always returns NULL. There must be something I don’t understand from the instructions. I tried several variations.

I.e. if a php variable is named $test
what does the retrieval to JS look like?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rootNov 07.2013 — You can not use JavaScript server-side, JavaScript is a client-side language.

You can use PHP to generate HTML and JavaScript code but not the other way around, not in the way you appear to be using it.

To retrieve data from a server using JavaScript you need to use what is referred to as AJAX which is Asynchronous Javascript And XML

To be very basic and rudimentary an AJAX routine consists of...

[CODE]// we make an AJAX (aka XML) object
ajax = new XMLHttpRequest();

// we now make a request object
ajax.open("GET","yourScript.php",true);

// we now make reference to a method to handle the results (if any)
ajax.onreadystatechange = function(){
// test if the result has completed the request states
if( ajax.readyState == 4){
// we completed but was the result OK?
if(ajax.status == 200){
// we were OK because we had HTTP 200 signal
alert(ajax.responseText); // show the result
}
}
}
// now make the request
ajax.send();
[/CODE]


Your receiving script yourScript.php will be any number of methods of returning information requested, it could be as simple as...
[code=php] echo date("r");[/code]
returning an RFC based time stamp

You can test this on your computer if you have a web server installed, or a service that will allow development testing on your PC, one word of advice is that you need and can't stress enough, to install a firewall, forget windows firewall as its about as useful as a chocolate tea pot.
Copy linkTweet thisAlerts:
@httpsterNov 08.2013 — You can use php inside of javascript on the client side.

So, as long as the variable $test is currently present somewhere on the page you could do...

[CODE]<script type="text/javascript">
console.log(<?php $test ?>);
</script>[/CODE]


This is done in wordpress files all the time.
Copy linkTweet thisAlerts:
@httpsterNov 08.2013 — sorry, that should be <?php echo $test ?>
×

Success!

Help @zron spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...