1

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?

3
  • Yes, you could write a function to do that. However, rather than doing that, it would probably be preferable to put the form in it's own file and include() it. Commented Nov 13, 2010 at 16:54
  • Another option would be to use a template engine such as smarty. Commented Nov 13, 2010 at 16:57
  • @GreenMatt yeah, but I cant have and parameter when I include Commented Nov 13, 2010 at 16:58

3 Answers 3

5
<?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();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

No it won't return the form it will output the text directly. If you want it to return the form's text you could use output buffering. I've edited my answer to give an example.
1

There is an extensive article on this but basically here is the code. 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>
    ';
    }

?>

Comments

0

Or you can use the HEREDOC syntax to put your form into a string within a php function:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.