Sahil Chhabra
Sahil Chhabra

Reputation: 11676

How to send multiple values in a href link in PHP?

I want to send multiple values to a different page using a href link. But what I retrieve is the first 2 values only, rest shows an error of undefined index.

My code is:

<?php 

echo "<a href='index.php?choice=search&cat=".$cat."&subcat=".$subcat."&srch=".$srch."&page=".$next."'> Next </a>";

?>

I get the values of "choice" and "cat" only. Please tell me whats wrong in the above code.

Upvotes: 1

Views: 42805

Answers (2)

knittl
knittl

Reputation: 265251

You must HTML-escape those ampersands properly:

?coice=search&amp;cat=...&amp;subcat=...&amp;srch=...

&sub (of &subcat) gets interpreted as &sub; which is a special HTML entity for the subset operator:

&sub; or &#8834; = subset of ⊂

Also make sure you properly escape your variables to prevent XSS.

Upvotes: 3

Stu
Stu

Reputation: 4150

try using urlencode, as if there are any special characters in the strings, it could have an effect on the querystring as a whole;

echo "<a href='index.php?choice=search&cat=".urlencode($cat)."&subcat=".urlencode($subcat)."&srch=".urlencode($srch)."&page=".urlencode($next)."'> Next </a>";

Plus you had a space between srch and page, that could have been causing a problem.

Upvotes: 6

Related Questions