hi all, i want to send javascript variable to PHP variable , now, i am using Javascript variable to Form variable first, then Form variable to PHP variable, but i have try so long , but still can't work , I need someone to help me! Please someone respond to me!?... <script> function showAppletMsg(str) { document.getElementById('MSg').innerHTML = str; //receive msg from applet document.InfoPassed.PhpVariable.value = str.length; } if (document.addEventListener) { alert("document.addEventListener"); document.getElementById("InfoPassed").submit(); //when page download complete , JavaScript Form Submit , but looks like didn't work ??????????am i wrong ? } </script> <form name="groovyform" > //execute applet <input type="button" name="groovybtn1" id="btn" class="groovybutton" value="Begin" onMouseOver="goLite(this.form.name,this.name)" onMouseOut="goDim(this.form.name,this.name)" onclick="this.disabled=1; document.getElementById('applet1').go();" > </form> <applet id="applet1" code="myapplet.class" name="myapplet" width="0" height="0" > <PARAM NAME=parm1 VALUE='number'> </applet> <form name="InfoPassed" action="" method="post" > <input type="hidden" name="PhpVariable" value=""> </form> <?php if(isset($_POST["PhpVariable"])){ //receive msg from form , still can't get ???????? $value = $_POST["PhpVariable"]; echo $value; } ?> Code (markup):
Once Javascript starts running, PHP has finished running. PHP runs in the server, creating the page. That page gets sent to the browser and it executes the Javascript. When you first run the page, there are no POST variables, so your if(isset ... is false. You have to use AJAX to send the variables from the browser to a PHP file on the server. That will do what you need and send whatever you echo back to the browser, which can then use Javascript to do what you want with that data. Learn to do it with a separate PHP file first, before you try to submit a file to itself. (That's what you're doing and it gets a little tricky.) There are a lot of AJAX tutorials on the web, but I suggest that you use jQuery's ajax method - it makes AJAX trivial.
$.ajax({ url: "test.php", context: document.body, success: function(data){ $(#myDiv).html(data); } }); Code (markup): Your test.php script just echos the data you want in the div. What it does is up to you - looks it up in a database, calculates it, gets it from another website or RSS feed, etc.
PHP is a server side language where as Javascript is client side, therefore the page is processed by the server before it is processed by the client.