How to pass value through href - PHP

Passing a value using the href tag

In the HREF tag of the first page, used as

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

The second page uses

 $compname = $_GET['compna']; 

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

Compname as "Chiti Technologies Ltd" When I transfer the value, I get onlt "Chiti"

+4
source share
5 answers

The reason you get only the first word of a company name is because the company name contains spaces. You need to encode the name.

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

You create ambiguous / invalid HTML without quoting the parameter. The result looks something like this:

 <a href=foo bar baz> 

It is recognized that foo belongs to href , and the rest does not. Enter values:

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

Use this code:

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

You need to add quotes for href , in addition, you need to use urlencode to encode the variable.

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

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

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

When using double-quoted strings, you don’t need to insert variables between them, you can simply enter them. Also, when inserting strings together, do not use a comma, but use. To insert strings, otherwise you will get parsing errors. For arrays, include them between braces {}

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

All Articles