0

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

asked Feb 2, 2011 at 14:06
3
  • 1
    sure. there is no PHP in the user's browser. Commented Feb 2, 2011 at 14:08
  • 2
    Hello 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? Commented Feb 2, 2011 at 14:09
  • 1
    It seems you want to pass php variable to javascript, right? Commented Feb 2, 2011 at 14:10

3 Answers 3

1

You can't.

  1. The browser makes a request
  2. The webserver runs the PHP
  3. The webserver delivers an HTTP resource to the browser
  4. 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
Sign up to request clarification or add additional context in comments.

Comments

0
answered Feb 2, 2011 at 14:31

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.