Johnson Johnson - 5 months ago 10
PHP Question

Create a PHP function to display an HTML form?

Instead of repeating myself with my 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>


I would like to just call a function and then it would show this form. Could you do that? And for the uID, could i have a parameter to the function?

GWW GWW
Answer
<?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
 }

EDIT:

If you want this method to return the form you could use output buffering like so:

<?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();
 }
Comments