I know this is an old thread but what i what im trying to do is below, and i was wndering if someone could help me I have a PHP documents that retrieves xml data at request of $_GET for example: /index.php?app=testradio&module=jsphp&get=currentDJ now when i access it through the web browser but im trying to build a JS autoupdater wich will pull the data from the php file and use getelementbyID to update the section on the page? Im not entirely sure how to do this and i was wondering if someone could help? im not sure how to pull the data into a variable in JS. Any Help would be appreciated Thanks Dev
Founded a good article on your question "javascript variable to php variable", here is a link to the website: http://freephpskr.com/php-and-javascript/20-php-and-javascript/86-passing-javascript-variable-to-php.html
i have this javascript code echo '<!DOCTYPE html PUBLIC > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link title="main" href="css/main.css" rel="stylesheet" type="text/css" /> <link title="main" href="css/ofert.css" rel="stylesheet" type="text/css" /> <link title="main" href="css/cos.css" rel="stylesheet" type="text/css" /> <script language="javascript"> function add(idp) { alert(idp); '; $id = $_GET['id']; $resulta = mysql_query("SELECT * FROM produse WHERE id =".$id); $rowa = mysql_fetch_array($resulta); $id1 = $rowa['nrcrt']; $idc = $_SESSION['userid']; $den = $rowa['den']; $pretu = $rowa['pret']; $prett = $rowa['pret']; mysql_query("INSERT INTO cos (id, idp, denp, cant, pru, prt) VALUES ( '$idc' , '$id' , '$den' , 1 , '$pretu', '$prett');"); echo ' } </script> <title>'.$title.'</title> </head> <body oncontextmenu="return false" ondragstart="return false" > <center>'; Code (markup): the code work ok untill insert the php part to it what am i doing wrong?
Be careful with security issues when passing a PHP variable to Javascript. Check the "Atribute Context" section of this article smashingmagazine.com/2011/01/11/keeping-web-users-safe-by-sanitizing-input-data/
So, I didn't read all the way to the end of this thread and this solution might've already come up. If not, here ya go: var myVar='string'; document.write("<a href='blah.php?string="+myVar+"' />click</a>"); Still a pain in the ass if you want automation, but better than nothing.
Afreaz, plain old AJAX does that. But whether it will work depends on what you want to do. PHP runs on the server and creates the page. Then it sends the entire page, Javascript included, to the user. If you expect that sending a Javascript variable to PHP will change the existing page before it's sent (yes, some people have asked how to do that, even though it's lifting yourself up by the bootstraps you don't have yet), it won't work. You need to send the data to a PHP page that creates some data and sends it back to the Javascript that started the whole thing (the "A" in AJAX stands for asynchronous), so it can write that data to the existing page. There are AJAX tutorials all over the web. (If you don't mind the overhead, jQuery makes AJAX almost trivial, but I'd suggest learning how to do it manually first, so that you understand what's happening.)