我是 php 的新手...我有一个将用户名变量传递给 php scrit 的表单,这是代码..
<form action="bottone.php" method="get"> Inserisci il tuo nome utente: <input name="username" type="text" /> <input type="submit" value="GENERA CODICE" /> </form>
我想在 botton.php 脚本中显示此 HTML 代码:
<a href=www.mysite.com/$username <img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>
其中 $username 是从表单传递的变量...我如何使用 echo 函数来做到这一点?
谢谢
像这样:
<?php
echo '<a href="http://www.mysite.com/'.$username.'"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>';
?>
或者,像这样:
<a href="http://www.mysite.com/<?=$username?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>
虽然你可能想确保 $username 是安全的...至少使用 urlencode , htmlspecialchars ,或类似的东西。
*编辑*
我假设您已经知道如何从您提到的表单中获取 $username,但如果您不知道,您只需执行以下操作:
$username = $_GET['username'];
或者您可以借此机会使用我上面提到的那些功能(除非您在回显之前需要 $username 用于其他目的。
例子:
$username = urlencode($_GET['username']);
或者你可以直接在 echo 中这样做:
<a href="http://www.mysite.com/<?=urlencode($_GET['username'])?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>