Using HTM Links to generate variables for PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Using HTM Links to generate variables for PHP

Post by idnoble »

Hello

I'm trying to use a set HTML links on my website to query a database using PHP, writing the php script is not a problem but generating variables through the HTML link is beyond me, i don't want to create different scripts for the links instead i want them to share just one script.

Has anyone got any idea of how to do this?

Thank you
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Code: Select all

<a href="website.com?var1=value1&var2=value2&var3=value3">Link</a>
Then:

Code: Select all

<?php
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];
$var3 = $_GET['var3'];

?>
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Post by idnoble »

Hey thank you for that. I'm just trying to code now. i noticed you used

Code: Select all

$_GET
in the script, as I have register globals turned off I'm thinking of using

Code: Select all

HTTP_POST_VARS
.

Does this makes any difference?

Thanks
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

HTTP_POST_VARS is old, it is now$_POST. But that is for post variables. The variables here are $_GET (old: $HTTP_GET_VARS) variables, as thats what variables passed in the URL are!
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Post by idnoble »

Hello, thank you for the tip, i've switched to $_GET, tested my script and all works fine only that no variable was passed to PHP from my HTML links even though I used

<a href="Genre.php?Var='Action'"></a>

Whenever I click on the link in my web browser the error message is always

QUERY WAS EMPTY

If i can find another to enable my HTML links to generate a corresponding variable for PHP to work with all will be rosy, do you have any idea of how I can generate variables through clicking an HTML link?

Thank you
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

what are you doing on the page that recieves the form? "QUERY IS EMPTY" seems like a custom error that a query to a DB returned no hits... let see the code?
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Post by idnoble »

Here's the code, I have a set 7 links

Action
Classic
Comedy
Family
Horror
Sci-fi
Music

Once clicked in the browser each is supposed to generate a corresponding variable for PHP to process.

Here's the php script for processing

Code: Select all

<?php
//Create short variable names
$Action = $_GET['Action'];
$Classic = $_GET['Classic'];
$Comedy = $_GET['Comedy'];
$Family = $_GET['Family'];
$Horror = $_GET['Horror'];
$Scifi = $_GET['Scifi'];
$Music = $_GET['Music'];

$session = mysql_connect('localhost', 'root', 'platinum') or die(mysql_error());
if (!$session)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

mysql_select_db('xplosive')or die(mysql_error());
if ($Action)
$query = "select*
          from movies
		  where Genre = 'Action'";
		  
else if ($Classic)
$query = "select*
          from movies
		  where Genre = 'Classic'";
		  
else if ($Comedy)
$query = "select*
          from movies
		  where Genre = 'Comedy'";
		  
else if ($Family)
$query = "select*
          from movies
		  where Genre = 'Family'";
		  
else if ($Horror)
$query = "select*
          from movies
		  where Genre = 'Horror'";
		  
else if ($Scifi)
$query = "select*
          from movies
		  where Genre = 'Science_Fiction'";
		  
else if ($Music)
$query = "select*
          from Music";
$result = mysql_query($query) or die(mysql_error()); 

$num_results = mysql_num_rows($result);

echo '<p style= "font-size: 10pt"><strong>Search hits: '.$num_results.'</strong></p>';
for ($i=0; $i <$num_results; $i++)
{
 $row = mysql_fetch_array($result);
echo '<p style= "font-size:14pt; color: blue"><strong>'. ($i+1). ':'; 
echo htmlspecialchars (stripslashes($row['Title']));
 echo '<p style= "font-size: 12pt; color: black"<br />Genre: ';
 echo stripslashes($row['Genre']);
 echo '<br/> Synopsis: ';
 echo stripslashes ($row['Synopsis']);
 echo'<br> Price: ';
 echo stripslashes ($row['Price']);
 echo '<br/> Itemcode: ';
 echo stripslashes ($row['Price']);
 echo '</p>';
}
?>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

<a href="Genre.php?Var='Action'"></a>
You can't use quotes in the query-string, skip the qoutes, no matter if you set the var to a string or integer...

Code: Select all

<a href="genre.php?var=action">action</a>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

You need to change the beginninng tosomething else - it will be $var = get['Var'];
then do
if($var=="action"){
$query = "select*
from movies
where Genre = 'Action'";

}
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Post by idnoble »

do you mean I should switch the

Code: Select all

$Action = $_GET['Action'];
$Classic = $_GET['Classic'];
$Comedy = $_GET['Comedy'];
$Family = $_GET['Family'];
$Horror = $_GET['Horror'];
$Scifi = $_GET['Scifi'];
$Music = $_GET['Music'];
to

Code: Select all

$var = get['Var'];
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

correct

$_GET['var'] gets the contents of var in your link. There is no $_GET['Action'] for example.

Basically you will have

$_GET['var'] = "Action" if your link looks like:
<a href="link.com?var=Action">Action Link</a>
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Post by idnoble »

Hey thank you I got it working now all the links are cool
Thaks to y'all a gazillion times
Post Reply