[英]Create a PHP function to display an HTML form?
而不是重復我的表單:
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="$id">
<br>
<input type="submit" value="Lägg till">
</form>
我只想調用一個函數,然后它將顯示這種形式。 你能做到嗎? 對於uID,我可以為函數指定一個參數嗎?
<?php
function showForm($uid){
?>
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="<?=$uid?>">
<br>
<input type="submit" value="Lägg till">
</form>
<?php
}
編輯:
如果希望此方法返回表單,則可以使用輸出緩沖,如下所示:
<?php
function showForm($uid){
ob_start();
?>
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="<?=$uid?>">
<br>
<input type="submit" value="Lägg till">
</form>
<?php
return ob_get_clean();
}
關於這一點,有大量文章,但基本上是代碼。 https://a1websitepro.com/create-form-php-function/
<?php
function form(){
echo '
<form method="post" action="">
<input type="text"name="firstName"/>
<input type="submit" name="submit" value="submit"/>
</form>
';
}
?>
或者,您可以使用HEREDOC語法將表單放入php函數中的字符串中:
<?php
function display_form($uid) {
$str = <<<EOT
<form name="addBlockList" action="" method="post">
Välj blockeringsgrad:
<select name="blockeringsgrad" style="font-size: 12px;">
<option value="1">1 - Bilder</option>
<option value="2">2 - Bilder, Vän, Vägginlägg, PM</option>
<option value="3">3 - Ingen tillgång till profil</option>
</select>
<input type="hidden" name="uID" value="$uid">
<br>
<input type="submit" value="Lägg till">
</form>
EOT;
return $str;
}
echo display_form(1);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.