1

I want to to pass a js variable to another page ..what to do..code is given bellow..

Code

`

$('#disInfo').on('click','tr',function(){
    var obj = $(this);
    var result=  obj.find('td').eq(1).html();
    window.location.href='http://localhost/Trying/search_cus.php';
});

`

I want to send the value of result variable with page link and how to receive the value in another page

1
  • if you are in control of both pages you can set a cookie or localstorage variable to hold the javascript variable... i do the same thing to verify password change requests Commented May 6, 2018 at 18:11

1 Answer 1

1

You could pass it via an url parameter. If result contains html or characters that cannot be used in an url then you need to encode it first.

$('#disInfo').on('click','tr',function(){
    var obj = $(this);
    var result = encodeURIComponent( obj.find('td').eq(1).html() );
    window.location.href='http://localhost/Trying/search_cus.php?result=' + result;
});

On the target page you can get url parameters via window.location.search ... there's plenty of examples on stackoverflow on how to do that.

NOTE: be aware that the server will also get the request for ?result=whatever. If the server side code processes this (eg PHP: $_GET['result']) you must ensure it sanitizes the value to prevent malicious code injection.

BTW, other ways of passing data is via cookies, sessionStorage, localStorage, etc.

Sign up to request clarification or add additional context in comments.

7 Comments

plus one on the storage options.
if I want to send more then one value?
add more than one parameter or storage entry
can u please give the the code for passing two value?
var result2 = encodeURIComponent(obj.find('td').eq(whatever number it is).html()); window.location.href='localhost/Trying/search_cus.php?result=' + result +'&result2=' + result2;
|

Your Answer

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.