创建一个PHP函数来显示HTML表单?

创建一个PHP函数来显示HTML表单?,php,Php,不要用我的表格重复我自己: <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ä

不要用我的表格重复我自己:

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

Välj blockeringsgrad:
1-舱底
2-比尔德,沃恩,沃金,下午
3-Ingen Tilgång till profil

我只想调用一个函数,然后它会显示这个表单。你能那样做吗?对于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
 }
Välj blockeringsgrad: 1-舱底 2-比尔德,沃恩,沃金,下午 3-Ingen Tilgång till profil

Välj blockeringsgrad:
1-舱底
2-比尔德,沃恩,沃金,下午
3-Ingen Tilgång till profil

或者,您可以使用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);
?>

或者您可以使用herdoc语法将表单放入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);
?>

关于这方面有很多文章,但基本上这里是代码


关于这方面有很多文章,但基本上这里是代码


是的,您可以编写一个函数来实现这一点。但是,与其这样做,不如将表单放在它自己的文件中并保存它。另一种选择是使用诸如smarty之类的模板引擎。@Greenmart是的,但我不能有和参数,当我包括Yes时,您可以编写一个函数来实现这一点。但是,与其这样做,不如将表单放在它自己的文件中,然后将其保存。另一种选择是使用smarty之类的模板引擎。@Greenmart是的,但我不能使用和参数,当我包含时,它不会返回表单,它将直接输出文本。如果希望它返回表单的文本,可以使用输出缓冲。我已经编辑了我的答案以给出一个例子。不,它不会返回表单,而是直接输出文本。如果希望它返回表单的文本,可以使用输出缓冲。我已经编辑了我的答案以给出一个例子。
    <?php
    function form(){
    echo '
    <form method="post" action="">
    <input type="text"name="firstName"/>
    <input type="submit" name="submit" value="submit"/>
    </form>
    ';
    }