簡體   English   中英

PHP:html鏈接中的php變量( <a>)</a>

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

請幫我解決這個問題。

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

似乎PHP變量與html鏈接不兼容:(

所以我想知道什么是正確的方法。

TIA ...

像下面那樣在那兒回顯那些變量

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

請使用模板引擎執行此類操作...

使用以下之一:

這些將使您的生活更加美好,並消除html文件的復雜性

您還可以在關聯數組中傳遞所有GET參數,並使用:

http_build_query($params)

所以:

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

或以您的方式:

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

您還可以使用Heredoc構建html / php組合: http : //www.hackingwithphp.com/2/6/3/heredoc

似乎php變量與html鏈接不兼容

好吧,PHP在服務器端運行。 HTML是客戶端。 因此,客戶端代碼無法解釋PHP變量。

您需要將服務器端代碼包含在<?php ?>標記中,以便使其在服務器上執行(就像您在其他地方已經執行的一樣)。 否則,服務器僅將其視為任何其他HTML並將其返回給瀏覽器。 像這樣:

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

如您所見,這有點混亂。 但是您可以將整個內容放在一個echo語句中:

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

請注意,在該位置需要如何轉義雙引號,但是由於整個內容都是雙引號的字符串,因此其中包含的變量將擴展為它們的值。

無論哪種方式,可讀性都有優缺點,因此,如何展示它取決於您。

你應該用這個

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

要么

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

您也可以使用Here Doc語法

<?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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM