如何通过 href 传递值 - PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11772493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:02:31  来源:igfitidea点击:

How to Pass a value via href - PHP

phphref

提问by RajaSekar

Am passing a value using href tag

正在使用 href 标签传递一个值

In first page Href tag used as

在第一页 Href 标记用作

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.

接收 Compna 值是通过的,但只传递第一个词,其余的词被跳过。

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

公司名称为“ Chiti Technologies Ltd ” 当我传递值时,我收到的只有“ Chiti”

采纳答案by mpieperhoff

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>";

回答by deceze

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

您通过不引用参数来生成不明确/无效的 HTML。结果是这样的:

<a href=foo bar baz>

Only foois recognized to belong to href, the rest doesn't. Quote the values:

只有foo被承认属于href,其余不属于。引用值:

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

回答by u?nb??s

Use this code:

使用此代码:

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

回答by xdazz

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

您需要为您的 加引号href,此外,您还需要使用urlencode来对变量进行编码。

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

回答by Tschallacka

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

改变回声 "<a href=view_exp.php?compna=",$compname,">$compname</a>";

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

回声 "<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>";