Printing array items sent in a form

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

    Printing array items sent in a form

    I'm trying an example from the book "Learning PHP5" but it doesn't
    work as Expected. Snippet:

    <form method="POST" action="index.p hp">
    <select name="lunch[ ]" multiple>
    <option value="pork">BB Q Pork Bun</option>
    <option value="chicken" >Chicken Bun</option>
    <option value="lotus">L otus Seed Bun</option>
    <option value="bean">Be an Paste Bun</option>
    <option value="nest">Bi rd-Nest Bun</option>
    </select>
    <input type="submit" name="submit">
    </form>

    Selected buns:
    <br/>
    <?php
    foreach ($_POST['lunch'] as $choice) {
    print "You want a $choice bun. <br/>";
    }
    ?>


    When I select several itens with the Ctrl key, it still prints the
    last item only, ie: "You want a nest bun."

    I double-checked, and I selected several items. Any idea?
    Thanks,

  • ED

    #2
    Re: Printing array items sent in a form


    "Charles" <landemaine@gma il.comwrote in message
    news:1170427958 .915021.200480@ p10g2000cwp.goo glegroups.com.. .
    I'm trying an example from the book "Learning PHP5" but it doesn't
    work as Expected. Snippet:
    >
    <form method="POST" action="index.p hp">
    <select name="lunch[ ]" multiple>
    <option value="pork">BB Q Pork Bun</option>
    <option value="chicken" >Chicken Bun</option>
    <option value="lotus">L otus Seed Bun</option>
    <option value="bean">Be an Paste Bun</option>
    <option value="nest">Bi rd-Nest Bun</option>
    </select>
    <input type="submit" name="submit">
    </form>
    >
    Selected buns:
    <br/>
    <?php
    foreach ($_POST['lunch'] as $choice) {
    print "You want a $choice bun. <br/>";
    }
    ?>
    >
    >
    When I select several itens with the Ctrl key, it still prints the
    last item only, ie: "You want a nest bun."
    >
    I double-checked, and I selected several items. Any idea?
    Thanks,
    >
    hi Charles,
    looks like you have space characters in the select name (between the square
    brackets),
    try: <select name="lunch[]" multiple>
    and not: <select name="lunch[ ]" multiple>

    cheers,
    ED


    Comment

    • Charles

      #3
      Re: Printing array items sent in a form

      Ok, I found out, there's gotta be only one space between the brackets
      instead of two:

      <select name="lunch[ ]" multiple>

      Small detail. Big difference.

      Comment

      • Michael Fesser

        #4
        Re: Printing array items sent in a form

        ..oO(Charles)
        >Ok, I found out, there's gotta be only one space between the brackets
        >instead of two:
        >
        ><select name="lunch[ ]" multiple>
        There's gotta be _no_ space!

        <select name="lunch[]" multiple="multi ple">

        Micha

        Comment

        • ED

          #5
          Re: Printing array items sent in a form


          "Michael Fesser" <netizen@gmx.de wrote in message
          news:q9m6s2pue3 r8b9bkdkpaq92j3 nir2c64d8@4ax.c om...
          .oO(Charles)
          >
          >>Ok, I found out, there's gotta be only one space between the brackets
          >>instead of two:
          >>
          >><select name="lunch[ ]" multiple>
          >
          There's gotta be _no_ space!
          >
          <select name="lunch[]" multiple="multi ple">
          >
          Micha

          Interestingly, it appears that this array syntax actually does work with a
          single space (or a single tab character for that matter):

          $foo = array();
          $foo[] = 'empty';
          $foo[ ] = 'space';
          $foo[ ] = 'tab';
          print_r($foo);

          results in:
          Array
          (
          [0] =empty
          [1] =spacechar
          [2] =tab
          )strange huh?cheers, ED


          Comment

          Working...