简体   繁体   中英

get value of a php variable set by url in javascript

Hey i defined a php variable like this inside the body tag:

<?php $v=null;?> 

Now i am using this code to check if the variable is set through the url

if ("<?php echo $_GET["v"];?>;"  == null) {
          // Do something because the variable is set through the url

    }
 else {
         // Do something else because the variable is not set through the url    
    }

I am using an url like this: www.example.com/index.php?v=12345

When i run this code by opening the index with this url it says v is undefined.. When i run this code by opening the index with "www.example.com/index.php" it is undefined as well.. but not 'null'

What is the problem here? Thank you

Try like

var my_var_arr = location.search.replace('?', '').split('=');
if (my_var_arr[1]  == null) {
      // Do something because the variable is set through the url

}
else {
     // Do something else because the variable is not set through the url    
}

Try this FIDDLE

You can use javascript to get the query string parameter instead of php. There is lot of tutorials around here.

Ref: http://javascriptproductivity.blogspot.in/2013/02/get-url-variables-with-javascript.html

Possible solution: jquery get querystring from URL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM