can I use arrays in forms? with a string index?

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

    can I use arrays in forms? with a string index?

    I've waited 6 weeks for an answer to my other question and still no
    luck, so let me rephrase the question. I know I can do this:

    <form method="post" action="$self">
    <input type="text" name="filesToDe lete[]">
    <input type="text" name="filesToDe lete[]">
    <input type="text" name="filesToDe lete[]">
    </form>


    On the recieving end, I'll get an array called $filesToDelete, and
    then I can loop through to get all the ids of the files that need to
    be deleted.

    But is there anyway to build an array in a form and get a string
    index? I've tried this, and I seem to get an array, but I'm unable to
    get any info out of it, neither through extract() nor direct use of
    the string index:


    <form method="post" action="$self">
    <input type="text" name="userInfo['name']">
    <input type="text" name="userInfo['email']">
    <input type="text" name="userInfo['password']">
    </form>


    Any help?
  • Andy Hassall

    #2
    Re: can I use arrays in forms? with a string index?

    On 5 Jul 2003 11:09:56 -0700, lkrubner@geocit ies.com (lawrence) wrote:
    [color=blue]
    >I've waited 6 weeks for an answer to my other question and still no
    >luck, so let me rephrase the question. I know I can do this:
    >
    ><form method="post" action="$self">
    ><input type="text" name="filesToDe lete[]">
    ><input type="text" name="filesToDe lete[]">
    ><input type="text" name="filesToDe lete[]">
    ></form>
    >
    >
    >On the recieving end, I'll get an array called $filesToDelete, and
    >then I can loop through to get all the ids of the files that need to
    >be deleted.
    >
    >But is there anyway to build an array in a form and get a string
    >index? I've tried this, and I seem to get an array, but I'm unable to
    >get any info out of it, neither through extract() nor direct use of
    >the string index:
    >
    >
    ><form method="post" action="$self">
    ><input type="text" name="userInfo['name']">
    ><input type="text" name="userInfo['email']">
    ><input type="text" name="userInfo['password']">
    ></form>[/color]

    So close.

    Just don't put the quotes in there.

    <form method="post" action="">
    <input type="text" name="userInfo[name]">
    <input type="text" name="userInfo[email]">
    <input type="text" name="userInfo[password]">
    <input type="submit">
    </form>
    <pre>
    <?php
    var_dump($_POST );
    ?>
    </pre>

    Submit a,b,c and get:

    array(1) {
    ["userInfo"]=>
    array(3) {
    ["name"]=>
    string(1) "a"
    ["email"]=>
    string(1) "b"
    ["password"]=>
    string(1) "c"
    }
    }

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk )
    Space: disk usage analysis tool (http://www.andyhsoftwa re.co.uk/space)

    Comment

    • lawrence

      #3
      Re: can I use arrays in forms? with a string index?

      Andy Hassall <andy@andyh.co. uk> wrote in message[color=blue]
      > So close.
      >
      > Just don't put the quotes in there.
      >
      > <form method="post" action="">
      > <input type="text" name="userInfo[name]">
      > <input type="text" name="userInfo[email]">
      > <input type="text" name="userInfo[password]">
      > <input type="submit">
      > </form>
      > <pre>
      > <?php
      > var_dump($_POST );
      > ?>
      > </pre>
      >
      > Submit a,b,c and get:
      >
      > array(1) {
      > ["userInfo"]=>
      > array(3) {
      > ["name"]=>
      > string(1) "a"
      > ["email"]=>
      > string(1) "b"
      > ["password"]=>
      > string(1) "c"
      > }
      > }[/color]


      Thank you. However, that syntax is considered incorrect, yes? I may
      use it for now, but it may not work in the future, yes?

      There is all this, on this page:


      http://us3.php.net/types.array

      Array do's and don'ts

      Why is $foo[bar] wrong?

      You should always use quotes around an associative array index. For
      example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar]
      wrong? You might have seen the following syntax in old scripts:


      <?php
      $foo[bar] = 'enemy';
      echo $foo[bar];
      // etc
      ?>



      This is wrong, but it works. Then, why is it wrong? The reason is that
      this code has an undefined constant (bar) rather than a string ('bar'
      - notice the quotes), and PHP may in future define constants which,
      unfortunately for your code, have the same name. It works, because the
      undefined constant gets converted to a string of the same name
      automatically for backward compatibility reasons.

      Comment

      • Andy Hassall

        #4
        Re: can I use arrays in forms? with a string index?

        On 6 Jul 2003 12:15:24 -0700, lkrubner@geocit ies.com (lawrence) wrote:
        [color=blue]
        >Andy Hassall <andy@andyh.co. uk> wrote in message[color=green]
        >> So close.
        >>
        >> Just don't put the quotes in there.
        >>
        >> <form method="post" action="">
        >> <input type="text" name="userInfo[name]">[/color]
        >
        >Thank you. However, that syntax is considered incorrect, yes? I may
        >use it for now, but it may not work in the future, yes?[/color]

        No, it's fine for HTML.
        [color=blue]
        >There is all this, on this page:
        >
        >http://us3.php.net/types.array
        >
        >Array do's and don'ts
        >
        >Why is $foo[bar] wrong?[/color]

        That syntax is incorrect within PHP. The syntax I'm referring to is within the
        form in HTML. Using quotes there is incorrect.

        It's somewhat similar to how you use array indexes within double quotes.

        $a = $b['x']; // right
        $a = $b[x]; // wrong

        $a = "$b['x']"; // wrong
        $a = "$b[x]"; // right



        But remember it's not really the same situation - the stuff you write in HTML
        is being parsed by different code to the stuff you're writing in PHP.

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • Andy Hassall

          #5
          Re: can I use arrays in forms? with a string index?

          On 7 Jul 2003 10:18:14 -0700, lkrubner@geocit ies.com (lawrence) wrote:
          [color=blue]
          >Andy Hassall <andy@andyh.co. uk> wrote in message[color=green]
          >> But remember it's not really the same situation - the stuff you write in HTML
          >> is being parsed by different code to the stuff you're writing in PHP.[/color]
          >
          >Good to be reminded of that. What you're saying is that if an HTML form submits
          >
          ><input type="text" name="for[bar]">[/color]

          ITYM name="foo[bar]"
          [color=blue]
          >then PHP will turn that into:
          >
          >$foo["bar"]
          >
          >yes?[/color]

          Yes.

          --
          Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
          Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

          Comment

          • lawrence

            #6
            Re: can I use arrays in forms? with a string index?

            Andy Hassall <andy@andyh.co. uk> wrote in message news:<9mq2hvkia tsu6o9ov96kkp3d 8a3ctq3f0n@4ax. com>...[color=blue]
            > On 7 Jul 2003 10:18:14 -0700, lkrubner@geocit ies.com (lawrence) wrote:
            >[color=green]
            > >Andy Hassall <andy@andyh.co. uk> wrote in message[color=darkred]
            > >> But remember it's not really the same situation - the stuff you write in HTML
            > >> is being parsed by different code to the stuff you're writing in PHP.[/color]
            > >
            > >Good to be reminded of that. What you're saying is that if an HTML form submits
            > >
            > ><input type="text" name="for[bar]">[/color]
            >
            > ITYM name="foo[bar]"
            >[color=green]
            > >then PHP will turn that into:
            > >
            > >$foo["bar"]
            > >
            > >yes?[/color]
            >
            > Yes.[/color]


            Good to know. Thanks a million.

            Comment

            Working...