Query about empty POST arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thejo

    Query about empty POST arrays

    Hi all,

    I started programming in PHP recently and have a query about empty
    $_POST arrays. I can see two scenarios when this could happen.

    1. When some tries to directly load the page to which data is being
    posted (for example, opening www.foo.com/xyz.php directly when a form
    action is xyz.php)
    2. When the user clicks the submit button without entering anything.

    I am aware that Javascript validation can take care of the second case,
    but it's unreliable. What is the accepted way of handling the above
    situations on the server side?
    Do I just reload the calling page if the array is empty?

    Thanks,
    Thejo

  • Dikkie Dik

    #2
    Re: Query about empty POST arrays

    Thejo wrote:[color=blue]
    > Hi all,
    >
    > I started programming in PHP recently and have a query about empty
    > $_POST arrays. I can see two scenarios when this could happen.
    >
    > 1. When some tries to directly load the page to which data is being
    > posted (for example, opening www.foo.com/xyz.php directly when a form
    > action is xyz.php)
    > 2. When the user clicks the submit button without entering anything.
    >[/color]
    Then (if method=post), the submit button itself is ported (it has a name
    and a value).[color=blue]
    > I am aware that Javascript validation can take care of the second case,
    > but it's unreliable. What is the accepted way of handling the above
    > situations on the server side?
    > Do I just reload the calling page if the array is empty?
    >
    > Thanks,
    > Thejo
    >[/color]

    It is not uncommon to omit the action attribute on the form of a PHP
    webpage. This means that the same page is used for displaying of the
    form AND for processing the results. This can be handy for continuous
    adding or giving the user a second change to correct the data posted.
    Off course, an if-statement gives you the power to have the same page
    behave totally different depending on what was posted.

    Best regards

    Comment

    • chotiwallah

      #3
      Re: Query about empty POST arrays


      Dikkie Dik wrote:[color=blue]
      > Thejo wrote:[color=green]
      > > Hi all,
      > >
      > > I started programming in PHP recently and have a query about empty
      > > $_POST arrays. I can see two scenarios when this could happen.
      > >
      > > 1. When some tries to directly load the page to which data is being
      > > posted (for example, opening www.foo.com/xyz.php directly when a form
      > > action is xyz.php)[/color][/color]

      i generally use a hidden field to detect a submit.
      [color=blue][color=green]
      > > 2. When the user clicks the submit button without entering anything.[/color][/color]

      then the post array is not empty, the elements just contain empty
      strings. the validation routine of the form data should take care of
      that.
      [color=blue][color=green]
      > >[/color]
      > Then (if method=post), the submit button itself is ported (it has a name
      > and a value).[/color]

      careful here, ie does NOT submit the button if the form is submitted
      using the return key.
      [color=blue][color=green]
      > > I am aware that Javascript validation can take care of the second case,
      > > but it's unreliable. What is the accepted way of handling the above
      > > situations on the server side?
      > > Do I just reload the calling page if the array is empty?
      > >
      > > Thanks,
      > > Thejo
      > >[/color]
      >
      > It is not uncommon to omit the action attribute on the form of a PHP
      > webpage. This means that the same page is used for displaying of the
      > form AND for processing the results. This can be handy for continuous
      > adding or giving the user a second change to correct the data posted.
      > Off course, an if-statement gives you the power to have the same page
      > behave totally different depending on what was posted.
      >[/color]

      micha

      Comment

      • DrewBe12@gmail.com

        #4
        Re: Query about empty POST arrays

        Exactly.

        if ($_POST['submit']) // Gives you the validation that the submit
        button itself has been pushed,

        then to detect an empty field

        $myfield = trim($myfield);
        if (!$myfield)
        {
        echo "error, no data provided";
        }

        Comment

        • Justin Koivisto

          #5
          Re: Query about empty POST arrays

          chotiwallah wrote:
          [color=blue]
          > Dikkie Dik wrote:
          >[color=green]
          >>Thejo wrote:
          >>[color=darkred]
          >>>2. When the user clicks the submit button without entering anything.[/color][/color]
          >
          > then the post array is not empty, the elements just contain empty
          > strings. the validation routine of the form data should take care of
          > that.[/color]

          Not true... if the form is made up of all radio buttons and/or
          checkboxes (with no default selected) AND the submit button doesn't have
          a "name" attribute, $_POST will be empty (as in $_POST=array() type of
          empty).
          [color=blue][color=green]
          >>Then (if method=post), the submit button itself is ported (it has a name
          >>and a value).[/color]
          >
          > careful here, ie does NOT submit the button if the form is submitted
          > using the return key.[/color]

          Most browsers don't because the button wasn't clicked...

          --
          Justin Koivisto, ZCE - justin@koivi.co m

          Comment

          • Justin Koivisto

            #6
            Re: Query about empty POST arrays

            DrewBe12@gmail. com wrote:
            [color=blue]
            > Exactly.
            >
            > if ($_POST['submit']) // Gives you the validation that the submit
            > button itself has been pushed,
            >
            > then to detect an empty field
            >
            > $myfield = trim($myfield);
            > if (!$myfield)
            > {
            > echo "error, no data provided";
            > }[/color]

            Assuming that $myfield was a text input, a hidden field, a select
            element, a textarea, etc. Checkboxes that aren't selected and radio
            button groups that don't have a value chosen will not even exist in the
            $_POST array... Also, you should have used:

            $myfield = trim($_POST['myfield']);

            Get out of the habit of using register_global s - especially when trying
            to give examples in a newsgroup. Too many newbies will wonder why it
            isn't working for them, then we'll get the whole "if
            register_global s=On" threads again...

            --
            Justin Koivisto, ZCE - justin@koivi.co m

            Comment

            Working...