-2

Possible Duplicate:
php - insert a variable in an echo string

I have a link that when it is clicked calls the next page and should pass two varibles to the page, the first is Manu and the second is fuel_type.

The problem is I can not seem to get the code to identify the variables and instead it passes the dollar sign and the variable name and not the value.

The code is below, i have tried writing this several different ways, but I am just running out of ideas.

 .'<a href=\act/manufacturer.php?manufacturer=$manu&fuel_type=$fuel_type>'.$manu.'</a>'.
1
  • 4
    You forgot to read the PHP documentation. The PHP manual quite clearly explains how variables are interpolated inside strings. In this case the difference between ' and " is key. Please go read the manual... Commented Nov 5, 2012 at 1:14

5 Answers 5

4

First, be sure that the variables contain what you want (from the superglobal GET variable):

$manu = $_GET['manu'];
$fuel_type = $_GET['fuel_type'];

Then, you have two options:

  1. Use double quotes " instead of single quotes, variables inside of quotes will be expanded

    "My dog is named $dog_name, and is $dog_age years old";
    
  2. Concatenate the strings inside single quotes with variables:

    'My dog is named ' . $dog_name . ', and is ' . $dog_age . ' years old.';
    

Be absolutely certain, however that you sanitize the variables before using or displaying them to the browser. At a minimum, you want any HTML/JS code removed, then some checking to make sure the result is of the type and length that you expect. There are quite a few questions on SO discussing ways to do this, a quick search for [php] sanitize GET variables will turn them up.

Sign up to request clarification or add additional context in comments.

Comments

2

you have to insert the var outside the '

like this:

.'<a href=\act/manufacturer.php?manufacturer='.$manu.'&fuel_type='.$fuel_type.'>'.$manu.'</a>'.

Comments

2

Single quotes do not parse variables, while double quotes do. Change your quotes around, and your link should work correctly.

Example:

."<a href=\act/manufacturer.php?manufacturer=$manu&fuel_type=$fuel_type>".$manu.'</a>'.

Comments

2

Single quotes dont allow parsing of variables, double-quotes do. So you'd be better off doing something like:

"<a href=\act/manufacturer.php?manufacturer=$manu&fuel_type=$fuel_type>$manu</a>";

-- or in your case --

'<a href=\act/manufacturer.php?manufacturer='.$manu.'&fuel_type='.$fuel_type.'>'.$manu.'</a>'

2 Comments

this is the sum of mine and Daedalus's
@gabrielem Yeah. Great minds...
-1

You can wrap your variables around curly braces to escape variable expressions:

'<a href=\act/manufacturer.php?manufacturer={$manu}&fuel_type={$fuel_type}>'.$manu.'</a>'.

1 Comment

This however will not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.