views:

111

answers:

2

I have this code below. As you can see I am passing two variables along with the link. The second variabye (category) works whenever it consists of one word, but some categories are two words or more, and then on the receiving php page where I fetch the variable only the first word is fetched.

Any ideas?

Example: I pass along this: Rims & Tires Only this comes through: Rims

$display_table .= "<a href='../ad.php?ad_id=$row[ad_id]&category=$row[category]' target='_parent'>";

Here is how I fetch it in the receiving php file (which the link is to):

$cat = $_GET['category'];
echo $cat; //displays only first word of multiple word categories.

Thanks

A: 

This should do it:

$display_table .= "<a href='../ad.php?ad_id=".urlencode($row[ad_id])."&category=".urlencode($row[category])."' target='_parent'>";
Sarfraz
+2  A: 

You need to use the proper encoding. For query data use urlencode:

"<a href='../ad.php?ad_id=".urlencode($row['ad_id'])."&category=".urlencode($row['category'])."' target='_parent'>"

And since the & inside the attribute value also need to be encoded properly (using htmlspecialchars):

"<a href='".htmlspecialchars("../ad.php?ad_id=".urlencode($row['ad_id'])."&category=".urlencode($row['category']))."' target='_parent'>"

Producing proper code makes things a lot more difficult and when using the variant above probably also a lot more unreadable. But you can split the steps like this:

$row['ad_id'] = urlencode($row['ad_id']);
$row['category'] = urlencode($row['category']);
$href = htmlspecialchars("../ad.php?ad_id=$row[ad_id]&category=$row[category]");
$display_table .= "<a href='$href' target='_parent'>";

And if ad_id is always a numeric value, you don’t even need to apply urlencode on it.

Gumbo
+1 for htmspecialchars
Mark Byers
My first suggestion using *ENT\_QUOTES* quote style replacement is not necessary as `urlencode` is already replacing the single quotes.
Gumbo