Hey there! any question in your mind? Ask It Now!.

Popular Categories

Access php variable inside javascript

+1 vote
asked in Programming by Aman

I have a java script function :

function take_snapshot(){
            // take snapshot and upload to server

          document.getElementById('pic').value =""; // here in value i have to echo a php variable which will come from a another file

Please give me the solution how can I print a variable from another file inside the javascript.

1 Answer

0 votes
answered by Jake
Use an xmlhttp request to send the variable from one php file to another.

function httpRequest(x,y,z)
    var messageData = [x,y,z];
    if (window.XMLHttpRequest)
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    { // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        //logic for after successful request

If you are simply trying to refer to a php variable via javascript you need to refer to it by ID like you did above or pass it via a method call on an event such as an onClick to your javascript function. Since javascript is client side it is responsive to what is on the page so it will process after the trigger on the page. Then your server side logic, such as php, is executed after.

Related Questions

+1 vote
1 answer 160 views
+1 vote
1 answer 246 views
+1 vote
1 answer 128 views
asked in Programming by amit_pammu Expert (5,030 points)
+1 vote
1 answer 740 views
+3 votes
1 answer 2,508 views
+1 vote
1 answer 147 views
+1 vote
2 answers 159 views
+1 vote
2 answers 107 views

Not a Member yet?

Ask to Folks Login

My Account
747 Folks are online
55 members and 692 guest online
Your feedback is highly appreciated