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

1
  • assign <?php echo $_GET["v"];?> this to any javascript variable. var v=<?php echo $_GET["v"];?> ; Commented Dec 7, 2013 at 5:28

2 Answers 2

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

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

4 Comments

I´ve tried this before many times.. it did not change something
works if nothing is set through the url, but if the url is like www.example.com/index.php?v=1234 is returns ?v,1234 ... i want just to return 1234 .. how can i do that?
Oh ok I got it.See now
Ok the fiddle helped, i mixed it with "document.URL" to get the url.. thank you!
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

2 Comments

Do i need to modify the .htaccess for that?
No need for changing htaccess

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.