user3660378
user3660378

Reputation:

PHP: php variable in html link (<a>)

Please help me with this problem.

<a href="specificmonthlyattreport.php?instructor_id=$inst_id&description=$description"><?php echo $userRow2['description']; ?></a>

It seems that the PHP variable is incompatible with html link :(

so I want to know what is the proper method.

TIA...

Upvotes: 0

Views: 2470

Answers (6)

Yuri Blanc
Yuri Blanc

Reputation: 645

You can also use Here Doc Syntax

<?php
//test variables
$inst_id = 1;
$description = "Test 1";

$eof = <<<EOF 
<a href="specificmonthlyattreport.php?instructor_id=$inst_id&description=$description">$description</a>
EOF;

//test output
echo $eof;

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Upvotes: 0

David
David

Reputation: 218837

it seems that the php variable is incompatible with html link

Well, PHP runs server-side. HTML is client-side. So there's no way for client-side code to interpret PHP variables.

You need to enclose server-side code in <?php ?> tags in order for it to execute on the server (like you already do elsewhere). Otherwise the server just treats it as any other HTML and returns it to the browser. Something like this:

<a href="specificmonthlyattreport.php?instructor_id=<?php echo $inst_id; ?>&description=<?php echo $description; ?>"><?php echo $userRow2['description']; ?></a>

As you can see, that gets a bit messy. But you can put the whole thing in one echo statement:

echo "<a href=\"specificmonthlyattreport.php?instructor_id=$inst_id&description=$description\">$userRow2[description]</a>";

Notice how the double-quotes needed to be escaped in that one, but since the whole thing was a double-quoted string the variables contained therein would expand to their values.

There are readability pros and cons either way, so it's up to you how you want to present it.

Upvotes: 1

Yuri Blanc
Yuri Blanc

Reputation: 645

You can also pass all your GET params in an associative array, and use:

http_build_query($params)

so:

<a href="yourscript.php/<?php echo http_build_query($params); ?>"></a>

or in your way:

<a href="specificmonthlyattreport.php?instructor_id=<?php echo $inst_id ?>&description=<?php echo $description ?>"><?php echo $userRow2['description']; ?></a>

You can also build html/php mix with heredoc: http://www.hackingwithphp.com/2/6/3/heredoc

Upvotes: 1

Elger van Boxtel
Elger van Boxtel

Reputation: 1060

Please use a template engine for these kinds of things...

Use one of:

These will brighten up your day and remove the complexity out of your html files

Upvotes: 1

KLin
KLin

Reputation: 479

you should use this

<a href="specificmonthlyattreport.php?instructor_id=<?php echo $inst_id;?>&description=<?php echo $description;?>"><?php echo $userRow2['description']; ?></a>

or

<a href="specificmonthlyattreport.php?instructor_id=<?=$inst_id?>&description=<?=$description?>"><?=$userRow2['description']?></a>

Upvotes: 0

Syed mohamed aladeen
Syed mohamed aladeen

Reputation: 6755

echo those variables there like the following.

<a href="specificmonthlyattreport.php?instructor_id=<?php echo $inst_id; ?>&description=<?php echo $description; ?>"><?php echo $userRow2['description']; ?></a>

Upvotes: 2

Related Questions