0

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

Marc Ster
  • 2,276
  • 6
  • 33
  • 63

2 Answers2

1

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

GautamD31
  • 28,552
  • 10
  • 64
  • 85
1

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

Community
  • 1
  • 1
Krish R
  • 22,583
  • 7
  • 50
  • 59