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

reju
  • 31
  • 3
  • 6
  • 1
    sure. there is no PHP in the user's browser. – Your Common Sense Feb 02 '11 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? – Piskvor left the building Feb 02 '11 at 14:09
  • 1
    It seems you want to pass php variable to javascript, right? – greg606 Feb 02 '11 at 14:10

3 Answers3

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);
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

check this thread How to pass JavaScript variables to PHP?

Community
  • 1
  • 1
Anil Namde
  • 6,452
  • 11
  • 63
  • 100
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);
    }
}
Karthik
  • 1,091
  • 1
  • 13
  • 36