如何实现可重用的PHP表单处理逻辑?

I am new to PHP and I have a lots of forms in PHP. The forms make use of the standard HTML Input fields and need to be validated on the serverside. How can I implement this, so that I do not have to write lots of boilerplate HTML over and over again, rather only write the minimal amount of code that generate the "full forms". What is the recommended approach to implement this? Thanks.

If you prefer to do it all yourself, you should at least do it PHP-Classes which will save you from re-writing (if done right ;-)). Handle attributes of the fields through an assoc array, e.g. like this:

<?php

   $form = new Form("MyInput", array ("submit" => "myform.php") );

   $form->AddField("input_text", array ("label" => "Your name") );

?>

To handle validation, you could use attributes such as

   $form->AddField("input_text", array (
        "label" => "Your name" , 
        "validate" => "required"
   ) );

(Only examples, there's a lot of code releated to this which you'd need to write once...)

That should be useful for learning purposes...

Next, you could use JS to validate. Pls. note that JS does client-side validation only and you cannot rely on it being executed (user might have turned of JS in his browser), so you still MUST validate in PHP when receiving the data. (And you could use JS-Libraries for that - I've used Parsley and was quite happy with it...)

If you want to skip that experience, use Frameworks or Templating Engines.

I would suggest to create a form template. Consider using a method (of class View):

private static function capture($view_filename, array $view_data)
{   
    extract($view_data, EXTR_SKIP);

    ob_start();

    require $view_filename;

    return ob_get_clean();
}

And call the static function capture (caution: consider using of __toString() to print objects) Pseudo-code:

echo View::capture('template', array('id' => '1', 'class' => 'userForm', 'inputs' => array(0 => array('label' => 'Name', 'type' => 'text'), 1 => array('label' => 'Password', 'type' => 'password')));