Professional Web Applications Themes

can I use arrays in forms? with a string index? - PHP Development

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="filesToDelete[]"> <input type="text" name="filesToDelete[]"> <input type="text" name="filesToDelete[]"> </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 ...

  1. #1

    Default 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="filesToDelete[]">
    <input type="text" name="filesToDelete[]">
    <input type="text" name="filesToDelete[]">
    </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?
    lawrence Guest

  2. #2

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

    On 5 Jul 2003 11:09:56 -0700, [email]lkrubnergeocities.com[/email] (lawrence) wrote:
    >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="filesToDelete[]">
    ><input type="text" name="filesToDelete[]">
    ><input type="text" name="filesToDelete[]">
    ></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>
    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 (andyandyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage ysis tool (http://www.andyhsoftware.co.uk/space)
    Andy Hassall Guest

  3. #3

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

    Andy Hassall <andyandyh.co.uk> wrote in message
    > 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"
    > }
    > }

    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.
    lawrence Guest

  4. #4

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

    On 6 Jul 2003 12:15:24 -0700, [email]lkrubnergeocities.com[/email] (lawrence) wrote:
    >Andy Hassall <andyandyh.co.uk> wrote in message
    >> So close.
    >>
    >> Just don't put the quotes in there.
    >>
    >> <form method="post" action="">
    >> <input type="text" name="userInfo[name]">
    >
    >Thank you. However, that syntax is considered incorrect, yes? I may
    >use it for now, but it may not work in the future, yes?
    No, it's fine for HTML.
    >There is all this, on this page:
    >
    >[url]http://us3.php.net/types.array[/url]
    >
    >Array do's and don'ts
    >
    >Why is $foo[bar] wrong?
    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

    [url]http://www.php.net/manual/en/language.types.string.php#language.types.string.pa rsing.simple[/url]

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

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  5. #5

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

    On 7 Jul 2003 10:18:14 -0700, [email]lkrubnergeocities.com[/email] (lawrence) wrote:
    >Andy Hassall <andyandyh.co.uk> wrote in message
    >> But remember it's not really the same situation - the stuff you write in HTML
    >> is being pd by different code to the stuff you're writing in PHP.
    >
    >Good to be reminded of that. What you're saying is that if an HTML form submits
    >
    ><input type="text" name="for[bar]">
    ITYM name="foo[bar]"
    >then PHP will turn that into:
    >
    >$foo["bar"]
    >
    >yes?
    Yes.

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  6. #6

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

    Andy Hassall <andyandyh.co.uk> wrote in message news:<9mq2hvkiatsu6o9ov96kkp3d8a3ctq3f0n4ax.com>. ..
    > On 7 Jul 2003 10:18:14 -0700, [email]lkrubnergeocities.com[/email] (lawrence) wrote:
    >
    > >Andy Hassall <andyandyh.co.uk> wrote in message
    > >> But remember it's not really the same situation - the stuff you write in HTML
    > >> is being pd by different code to the stuff you're writing in PHP.
    > >
    > >Good to be reminded of that. What you're saying is that if an HTML form submits
    > >
    > ><input type="text" name="for[bar]">
    >
    > ITYM name="foo[bar]"
    >
    > >then PHP will turn that into:
    > >
    > >$foo["bar"]
    > >
    > >yes?
    >
    > Yes.

    Good to know. Thanks a million.
    lawrence Guest

Similar Threads

  1. Explain Pls (2d Arrays in forms) Possible and how?
    By EagleEyeMHAFB in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: September 9th, 10:50 AM
  2. string index out of range: 0
    By rmorgan in forum Macromedia ColdFusion
    Replies: 4
    Last Post: May 11th, 07:06 PM
  3. String index out of range: -16
    By jdeline in forum Macromedia ColdFusion
    Replies: 8
    Last Post: April 5th, 05:51 PM
  4. String Index Out of Range Error on String ComparisonQuery
    By FredPope in forum Macromedia ColdFusion
    Replies: 1
    Last Post: March 19th, 07:42 AM
  5. Replies: 2
    Last Post: September 14th, 05:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139