subhomoy Posted March 2, 2014 Share Posted March 2, 2014 hello eveyone.. How can i pass values from one page to another using javascript.. I can do it using php but I need in javascript or jquery... suppose take this as an example... <script src="myscript.js?uid=123"></script> if the above step is possible then plz help me out.... IF NOT THEN the second way i want is <script src="myscript.php?uid=123"></script> If the above code doesn't have script tag then i can access the value but tthe <script> tag is making me impossible to do it...... I have used this step to access the value from the above code but it shows nothing... <?php $val = $_GET['uid']; echo $val; ?> Any help will be greatly appreciated... Thank you in advance... Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 2, 2014 Share Posted March 2, 2014 How can i pass values from one page to another using javascript Usually using ajax. <script src="myscript.php?uid=123"></script> PHP will receive the uid querystring parameter. But whatever your PHP script outputs the browser will try to parse it as javascript, as this what the browser expects from a <script> tag. Quote Link to comment Share on other sites More sharing options...
subhomoy Posted March 3, 2014 Author Share Posted March 3, 2014 Thanks for the reply... Can you plz provide me with an example or link if possible... Quote Link to comment Share on other sites More sharing options...
Solution WebStyles Posted March 3, 2014 Solution Share Posted March 3, 2014 (edited) here's an example of an Ajax request and POST to a php page. function createRequest(){ var xmlhttp = false; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); if(xmlhttp.overrideMimeType){ xmlhttp.overrideMimeType('text/xml'); } } else if(window.ActiveXObject){ try{ xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ } } } if(!xmlhttp) { return false; }else{ return xmlhttp; } } function callFile(uid){ var url = '_php/myscript.php'; var msg = "&uid="+uid; var xmlhttp = createRequest(); if(!xmlhttp) { return false; } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState==4){ // DO SOMETHING HERE alert(xmlhttp.responseText); } } // POST STUFF xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", msg.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(msg); } Hope this helps Edited March 3, 2014 by WebStyles Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted March 3, 2014 Share Posted March 3, 2014 I have used this step to access the value from the above code but it shows nothing... <?php $val = $_GET['uid']; echo $val; ?> You need to echo a proper javascript function/method by php because the query string of the source attribute is parsed by javascript. This should work: myscript.php <?php $uid = $_GET['uid']; echo "alert('Query ID = '+$uid)"; However, as already suggested above use AJAX. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.