Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Query string using HTML (https://www.warriorforum.com/programming/797865-query-string-using-html.html)

Calvin Woon 5th June 2013 11:42 AM

Query string using HTML
 
Hi warriors!

Got a programming question and would appreciate any advice.

I have a HTML site and the link that is given to my affiliates look something like this:

http://mysite.com/?10001

now how do i retrieve that query string using html? (not sure if I've phrased this correctly)

meaning to say, i would like the "10001" to appear somewhere in my website.

would appreciate any advice...

thanks!

Calvin

Brandon Tanner 5th June 2013 11:52 AM

Re: Query string using HTML
 
Normally, a query string has 2 parts, a field name and it's value, separated by an equals sign, like...

http://mysite.com/?aff=10001

In the above example, "aff" would be the field name, and "10001" is its value.

So to extract the value of "aff", all you need is a little bit of PHP...

<?php $aff_value = $_GET['aff']; ?>

Then you can display that value anywhere you need to on the page...

<?php echo $aff_value; ?>

Of course, you will need to give that page a PHP extension, but that's pretty much all there is to it.

FirstSocialApps 5th June 2013 12:09 PM

Re: Query string using HTML
 
http://mysite.com/?10001

[php
<?php
$url="http://mysite.com/?10001";
$parts=explode($url, '/?');
$string=$parts['1'];
echo $string;
?>
[/php]

Brandon Tanner 5th June 2013 12:35 PM

Re: Query string using HTML
 
^ That will only work if the URL value always ends with "10001". If you need to account for varying values at the end of the URL (which I'm assuming is what the OP wants to do), then you'd need to change $url to this in order for that code to work...

$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Also, the explode line should be this instead...

$parts = explode('?', $url);

So, altogether...

<?php
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = explode('?', $url);
$string = $parts['1'];
echo $string;
?>

wayfarer 6th June 2013 07:42 AM

Re: Query string using HTML
 
None of which you should do without escaping the data before putting it into the page. You don't want to allow arbitrary people to link to your page injecting whatever scripts or other garbage they so choose. Read about cross site scripting before you inject any content in query strings into your page.

1stranked 6th June 2013 08:47 AM

Re: Query string using HTML
 
Agree with wayfarer - you must check $_GET or $_POST data before using it in your query. Brandon gives you an overview how you can achieve to get parameters from URL but you'll need to use more secure approach.

yukon 6th June 2013 08:54 AM

Re: Query string using HTML
 
You'll never get the query string with HTML, try PHP.

get google search query string "php"

Calvin Woon 6th June 2013 11:16 AM

Re: Query string using HTML
 
thanks!

But what if my affiliate links come in this format: http://www.mysite.com/?10001

so how should the codes be inserted?

Calvin

1stranked 6th June 2013 01:39 PM

Re: Query string using HTML
 
Brandon already showed you complete code:

<?php
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = explode('?', $url);
$string = $parts['1'];
echo $string;
?>


All times are GMT -6. The time now is 04:06 PM.