Problems with form processing: array_key_exists() and while (list($k,$v) = each($arr))

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

    Problems with form processing: array_key_exists() and while (list($k,$v) = each($arr))

    Hello,

    I have an HTML form I'd like to process.

    <select name="items[]" multiple>
    <option value="doughnut s">Hot Doughnuts</option>
    <option value="coffee"> Hot Brewed Coffee</option>
    <option value="tea">Hot Tea</option>
    <option value="cake">Sp onge Cake</option>
    <option value="chips">H ot Chips</option>
    </select>

    The problem I have it where I follow the Programming PHP book:

    // Should the 's' be the name of the select tag, or the name of
    // the submit button?

    if (array_key_exis ts( 's', $_POST ))
    {
    $selected = "User selected these options:\n\n";

    while (list($k, $v) = each($_POST['items']))
    {
    // Is $v "doughnuts" or "Hot Doughnuts"?
    $selected .= "$v\n"
    }

    // ...
    }

    Thanks for your help.

    --
    Spartacus
  • Toby A Inkster

    #2
    Re: Problems with form processing: array_key_exist s() and while(list($k, $v) = each($arr))

    Spartacus wrote:
    // Should the 's' be the name of the select tag, or the name of
    // the submit button?
    Either.

    --
    Toby A Inkster BSc (Hons) ARCS
    [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
    [OS: Linux 2.6.12-12mdksmp, up 115 days, 22:05.]

    dict, thes & ency

    Comment

    • Spartacus

      #3
      Re: Problems with form processing: array_key_exist s() and while (list($k,$v) = each($arr))

      Toby A Inkster wrote:
      Spartacus wrote:
      >
      >// Should the 's' be the name of the select tag, or the name of
      >// the submit button?
      >
      Either.
      >
      Were there any other potential problems with the code I posted? I
      presume not otherwise you'd have said so. Thanks for your help. I'll use
      the select tag name instead since PHP is complaining that it's not an array.

      -Spartacus

      Comment

      • Toby A Inkster

        #4
        Re: Problems with form processing: array_key_exist s() and while(list($k, $v) = each($arr))

        Spartacus wrote:
        Were there any other potential problems with the code I posted? I
        presume not otherwise you'd have said so.
        Not that I noticed. And $v will be "doughnuts" , not "Hot Doughnuts".

        --
        Toby A Inkster BSc (Hons) ARCS
        [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
        [OS: Linux 2.6.12-12mdksmp, up 11:23.]

        dict, thes & ency

        Comment

        • Spartacus

          #5
          Re: Problems with form processing: array_key_exist s() and while (list($k,$v) = each($arr))

          Toby A Inkster wrote:
          Spartacus wrote:
          >
          >Were there any other potential problems with the code I posted? I
          >presume not otherwise you'd have said so.
          >
          Not that I noticed. And $v will be "doughnuts" , not "Hot Doughnuts".
          >
          Thanks Toby very much. I've solved the problem by changing to:

          array_key_exist s( 'moreof', $_POST );

          which is the name of my select tag.

          I've also changed the value="" in the option tags to be the same as the
          key, just to verify it works.

          I was getting odd errors even when no such keys existed. I had changed
          them all and still I was getting my old ones. Maybe my stupid browser
          cached them or something. So I renamed the HTML form and the PHP script
          and it worked just fine.

          Again thanks for your help.

          --
          Spartacus

          Comment

          • Toby A Inkster

            #6
            Re: Problems with form processing: array_key_exist s() and while(list($k, $v) = each($arr))

            Spartacus wrote:
            I've also changed the value="" in the option tags to be the same as the
            key, just to verify it works.
            This:

            <option value="Foo">Foo </option>

            can be written as this:

            <option>Foo</option>

            That is, when the value attribute is missing, the option's text content is
            used instead.

            --
            Toby A Inkster BSc (Hons) ARCS
            [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
            [OS: Linux 2.6.12-12mdksmp, up 18:36.]

            dict, thes & ency

            Comment

            • Toby A Inkster

              #7
              Re: Problems with form processing: array_key_exist s() and while(list($k, $v) = each($arr))

              Spartacus wrote:
              while (list($k, $v) = each($_POST['items']))
              By the way, most people would say that the following is more readable:

              foreach ($_POST['items'] as $k=>$v)

              and it's functionally equivalent. (Indeed, after parsing I believe PHP
              treats them exactly the same.)

              --
              Toby A Inkster BSc (Hons) ARCS
              [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
              [OS: Linux 2.6.12-12mdksmp, up 18:38.]

              dict, thes & ency

              Comment

              Working...