Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grab keyword from url

Status
Not open for further replies.

ghayman73

Technical User
Jul 3, 2002
55
FR
how can i grab the keyword from the url.
I have an url like this


is it possible to grab the "keyword" from the url and use it in text in html.
I would also like to insert it into a link.

the set up is that i would like to show my visitors the keyword and then when it is clicked the keyword forms part of the url to the next page.

thank you for any help
 
That's not a php question (unless you want to make it harder on purpose), but yes, you can do it with javascript, I think it gives javascript a variable named whatever it is in the url. Been a while since I played with that.
 
Actually that is definately an easy thing to do in PHP

in your example URL
Code:
 $_GET['keyword'] would be equal to akeyword

-Rob
 
skiflyer is right:

This is one of the basic tasks PHP was invented for, namely to process variables/input before sending the output to the browser.

All you need to do is to access the correct variable ($_GET['keyword']) and output some HTML.

The structure of the HTML as such would not be a PHP question, just the way how you make your keyword appear in the resulting HTML page. Also, how you link to the next page is a HTML issue - again PHP can print/echo out the code.

Wherever you want your keyword you just need to echo it within a PHP block:
Code:
<a href=&quot;[URL unfurl="true"]http://myserver.com/myScript.php?keyword=&quot;<?php[/URL] print $_GET['keyword'];?>&quot;>Next Page</a>

PHP &quot;infuses&quot; the values anywhere you want.
The real power of PHP, however, is unleashed once you start manipulating variables, accessing databases, other servers etc.
 
thanks for the input. ive been trying to use the $_get method but with no joy.

can i use this method if the globals are switched off.

this is what i have so far:


then i would like to diplay it as text something like this:

<php
print &quot;why not try searching ANother site for $_GET['keyword']<a href=&quot; print $_GET['keyword'];?>&quot;>search now</a>&quot;
?>
 
Ok,

There's some cleanup that you need to do.
Quotes have to match and it's safest to take $_GET['keyword'] out of the double quoted print statement.
Also, the <?php ?> are also set wrong. They only need appear once, they can't be nested.

Try this:
Code:
<?php
print &quot;Why not try searching another site for &quot;.$_GET['keyword'];
?>
<a href=&quot;[URL unfurl="true"]http://myserver.com/myScript.php?keyword=&quot;<?php[/URL] print $_GET['keyword'];?>>search now</a>

You have two different things up there:
1. Print the entire HTML with PHP
2. Have the HTML (the anchor tag) and just use a PHP block to fill in the value.
 
i am using tables for the main page so i was going to use
<?php
include &quot;?>

in the table cell.
not tried this yet (if you know a better way.....)

when i used your code it didnt seem to pick up the keyword just the text , i read someware about globals being turned off by default which sounds like a good thing but does this effect what i am trying to do.

thanks again
grant
 
The GET, POST. REQUEST arrays are superglobals. That means that they are available anywhere within your PHP script.
register globals has no influence on that. In fact, $_GET, $_POST etc. is the recommended way of accessing variables since it will work in any PHP version > 4.1.0

When you include the file within a PHP file you need to make sure that there are no nested PHP blocks.
 
Oh, so you're trying to pick up those variables from an include... that won't work, the $_GET is a superglobal and is going from the actual URL, not from the one you put as the argument to the include statement.

All you need to do in that case is set some variable though. The scope stays in the include statement.

i.e. if script1 is something like
Code:
<?php
$title = &quot;My WebPage&quot;;
include &quot;[URL unfurl="true"]http://www.mydomain.com/header.php&quot;;[/URL]
?>

and header.php looks like

<?php
echo &quot;<html><head><title>&quot;.$title.&quot;</title></head><body>&quot;;
?>

The title will print as you set it in the first script.

-Rob
(assuming I interpretted your last post correctly)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top