Home » Php » How to Pass a value via href – PHP

How to Pass a value via href – PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

Am passing a value using href tag

In first page Href tag used as

echo "<a href=view_exp.php?compna=",$compname,">$compname</a>";

In the Second page used

$compname = $_GET['compna']; 

To receive the Compna values are pass but only the first word is passed remaining words are skipped.

Compname as ” Chiti Technologies Ltd ”
When I pass the value I receive onlt “Chiti”

How to&Answers:

The reason you’re only getting the first word of the company name is that the company name contains blanks. You need to encode the name.

echo "<a href=view_exp.php?compna=",urlencode($compname),">$compname</a>";

Answer:

You are producing ambiguous/invalid HTML by not quoting the parameter. The result is something like:

<a href=foo bar baz>

Only foo is recognized to belong to href, the rest doesn’t. Quote the values:

echo '<a href="view_exp.php?compna=', urlencode($compname), '">', htmlspecialchars($compname), '</a>';

Answer:

Use this code:

echo '<a href="view_exp.php?compna='.urlencode($compname).'">'.$compname.'</a>';

Answer:

You need add quotes for your href, besides, you also need to use urlencode to encode the variable.

echo '<a href="view_exp.php?compna=' . urlencode($compname) . '">' . $compname . '</a>';

Answer:

change echo "<a href=view_exp.php?compna=",$compname,">$compname</a>";

to echo "<a href=\"view_exp.php?compna=$compname\">$compname</a>";

When using double quoted strings ” you don’t need to paste variables in between, you can just type them.
Also, when pasting strings together don’t use comma’s , but use . to paste strings otherwise you’ll get parse errors.
For arrays include them between curly brackets {}

echo "<a href=\"view_exp.php?compna={$compname["whatever"]}\">$compname</a>";