i want to pass js variable to php .my code is follows
function sub(uid){
window.location.href='<?php echo $urlp -> certificationceap(uid) ;?>';
here is some problem
-
1sure. there is no PHP in the user's browser.Your Common Sense– Your Common Sense2011年02月02日 14:08:46 +00:00Commented Feb 2, 2011 at 14:08
-
2Hello and welcome to StackOverflow! Please edit your question; adding the following points may get you better answers: 1. What are you trying to accomplish? 2. What have you tried so far? 3. What results did you get (no, "here is some problem" is nowhere near descriptive enough)? 4. How did that differ from the results you were expecting?Piskvor left the building– Piskvor left the building2011年02月02日 14:09:24 +00:00Commented Feb 2, 2011 at 14:09
-
1It seems you want to pass php variable to javascript, right?greg606– greg6062011年02月02日 14:10:28 +00:00Commented Feb 2, 2011 at 14:10
3 Answers 3
You can't.
- The browser makes a request
- The webserver runs the PHP
- The webserver delivers an HTTP resource to the browser
- The browser parses the HTML and executes any JS in it
At this stage, it is too late to send data to the PHP program as it has finished executing.
You need to make a new HTTP request to get data back to it.
Probably something along the lines of:
function sub(uid){
location.href = 'redirect.php?uid=' + encodeURIComponent(uid);
}
answered Feb 2, 2011 at 14:09
Quentin
949k137 gold badges1.3k silver badges1.4k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
check this thread How to pass JavaScript variables to PHP?
answered Feb 2, 2011 at 14:31
Anil Namde
6,64812 gold badges69 silver badges101 bronze badges
Comments
You can use ajax to achieve this..... advance apologies if it is not intended answer...
function send_var(js_var_1, js_var_2){
var parms = 'js_var_1='+js_var_1+'&js_var_2='+js_var_2;
if(js_var_1!='' && js_var_1!=''){
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//your result in xmlhttp.responseText;
}
}
xmlhttp.open("POST","<REMOTE PHP SCRIPT URL>",true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(parms);
}
}
answered Feb 2, 2011 at 15:09
Karthik
1,0911 gold badge13 silver badges36 bronze badges
Comments
default