alternative to $_POST

后端 未结 3 988
悲哀的现实
悲哀的现实 2021-01-27 09:20

I have a huge form with inputs of type (text, checkboxes, hidden et). The content of the form inputs are taken from a database. The user has to make some changes and to save the

相关标签:
3条回答
  • 2021-01-27 09:24

    As far as loop processing goes, the foreach loop contruct in PHP makes a copy of the array you are working on. If you want to process $_POST with a different loop construct (for / while) and use functions like count(), current(), reset(), next(), prev(), end(), each(), or key(), have at it.

    Programming PHP: Chapter 5, p. 128-129

    0 讨论(0)
  • 2021-01-27 09:26

    Use the Post/Redirect/Get pattern.

    0 讨论(0)
  • 2021-01-27 09:34

    There is absolutely nothing wrong in POST itself. You just have to use it properly
    An HTTP standard says you ought to make a GET redirect after receiving POST request.
    So, as easy code as this

        header("Location: ".$_SERVER['PHP_SELF']);
        exit;
    

    after processing your form will solve all your "problems"

    in case you want to handle post errors, you can use POST/Redirect/GET pattern. However it does not redirect on error, the problems you mentioned becoming negligible.

    here is a concise example of it:

    <?  
    if ($_SERVER['REQUEST_METHOD']=='POST') {  
      //processing the form    
      $err = array();
      //performing all validations and raising corresponding errors
      if (empty($_POST['name']) $err[] = "Username field is required";  
      if (empty($_POST['text']) $err[] = "Comments field is required";  
    
      if (!$err) {  
        //if no errors - saving data and redirect
        header("Location: ".$_SERVER['PHP_SELF']);
        exit;
      }  else {
        // all field values should be escaped according to HTML standard
        foreach ($_POST as $key => $val) {
          $form[$key] = htmlspecialchars($val);
        }
    } else {
      $form['name'] = $form['comments'] = '';  
    }
    include 'form.tpl.php';
    ?>  
    

    on error it will show the form back. but after successful form submit it will redirect as well.

    0 讨论(0)
提交回复
热议问题