Re: How to validate $_POST array

Lew Pitcher <[email protected]>
Newsgroups comp.lang.php
Organization A noiseless patient Spider
Message-ID <[email protected]>
On Thu, 11 Aug 2022 14:43:41 +0000, Lew Pitcher wrote:

> On Thu, 11 Aug 2022 11:47:02 +0200, Jakub wrote:
> 
>> I have this code
>> 
>> if ( isset($_POST) ) {
>> 
>> }
>> 
>> how to do filter_has_var function to check if $_POST exist and have value?
> 
> If the originating web page has issues a POST, then 
>   $_SERVER['REQUEST_METHOD'] will equal 'POST'
> 
> Your php code can only extract from the POST values that were set by
> the web page when it issued the POST. It can't demand a specific value,
> but it can establish whether or not the page supplied a specific value.
> 
> Say, for instance, you expect that the page will supply a text string
> in a variable called 'TextString', you can check to see if
> $_POST['TextString'] is set, and validate it's contents.
> 
> So, your php logic might look like
> 
>   if ($_SERVER['REQUEST_METHOD'] == "POST")
>   {
>     if (isset($_POST['TextString']))
>     {
>       /* validate the contents of $_POST['TextString'] */
>     }
>     else
>     {
>       /* page didn't supply 'TextString'. Handle this situation */
>     }
>   }
>   else
>   {
>     /* page didn't POST. Handle the situation. */
>   }

Note that $_POST is an array, so you /can/ iterate across the values in it

   if ($_SERVER['REQUEST_METHOD'] == "POST")
   {
      foreach ($_POST as $pKEY => $pVALU)
      {
        /*
        ** $pKEY is the subscript to $_POST,
        ** $pVALU is the value carried by $_POST[$pKEY]
        ** use $pKEY to choose your $pVALU validation criteria
        */
      }
   }
   else
   {
     /* page didn't POST. Handle the situation. */
   }





-- 
Lew Pitcher
"In Skills, We Trust"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.