Printing a PHP error inline instead of erasing the entire page

前端 未结 2 1511
感情败类
感情败类 2021-01-17 01:07

How can I make PHP print an error inline instead of changing the entire page?

I\'d like it to target #errors and fill that instead of changing everythi

2条回答
  •  臣服心动
    2021-01-17 01:30

    There are 2 ways of doing it.

    A real inline method is not entirely PHP-based, as it cannot be used without JavaScript and AJAX calls.
    Note the irritating disadvantage of this method: you will need to re-check every field again upon receiving form data finally.

    Another one will reload your page but it will be the same page with all the form fields, entered data and also freshly generated error messages. This is called POST/Redirect/GET pattern

    here is a short example

     $val) {
          $form[$key] = htmlspecialchars($val);
        }
    } else {
      $form['name'] = $form['comments'] = '';  
    }
    include 'form.tpl.php';
    ?>  
    

    while in the form.tpl.php file you have your form fields, entered values and conditional output of error messages

    
      
    

提交回复
热议问题