Jump to content

pass values to another page using javascript


subhomoy

Recommended Posts

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...

 

 

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. 

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

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.