reju
reju

Reputation: 11

javascript variable to php

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

Upvotes: 0

Views: 237

Answers (3)

Karthik
Karthik

Reputation: 1091

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);
    }
}

Upvotes: 0

Anil Namde
Anil Namde

Reputation: 6608

check this thread How to pass JavaScript variables to PHP?

Upvotes: 0

Quentin
Quentin

Reputation: 943142

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);
}

Upvotes: 1

Related Questions