How to retrieve a sub-scripted input field?

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

    How to retrieve a sub-scripted input field?

    I have a page with a form on it in which I have a series
    of input fields like so (note the sub-scripted name):

    <input type='text' name='rating[1]' value='1'>
    <input type='text' name='rating[2]' value='2'>
    <input type='text' name='rating[3]' value='3'>

    How do I retrieve the submitted values in a PHP script?

    For example, just trying to echo the current value
    displays nothing (and there IS a value being submitted)

    echo("rating[$i]=".$_POST['rating[$i]']."<br>");

    Or, trying to process the fields in a loop, there's
    never any value present.

    for($i=1; $i<=3; $i++)
    {
    if($_POST['rating[$i]'] < 0)
    { do something here }
    if($_POST['rating[$i]'] > 10)
    { do something else here }
    etc., etc., etc.
    }

    Syntactically, the above statements work (there's no error
    generated) but there's no value there.

    What am I doing wrong?
  • Ken Robinson

    #2
    Re: How to retrieve a sub-scripted input field?


    Martin wrote:[color=blue]
    > I have a page with a form on it in which I have a series
    > of input fields like so (note the sub-scripted name):
    >
    > <input type='text' name='rating[1]' value='1'>
    > <input type='text' name='rating[2]' value='2'>
    > <input type='text' name='rating[3]' value='3'>
    >
    > How do I retrieve the submitted values in a PHP script?
    >
    > For example, just trying to echo the current value
    > displays nothing (and there IS a value being submitted)
    >
    > echo("rating[$i]=".$_POST['rating[$i]']."<br>");[/color]

    Think about how you would get the value from a multi-level array:
    $x = array('level1'= >array('1','2') ,'level2'=>arra y('2','4'));
    echo $x['level1'][0];

    So, in your case, use:

    echo 'rating['.$i.']='.$_POST['rating'][$i].'<br>';

    Read up on arrays at www.php.net/array

    Ken

    Comment

    • Martin

      #3
      Re: How to retrieve a sub-scripted input field?

      On 9 Aug 2005 16:30:25 -0700, "Ken Robinson" <kenrbnsn@rbnsn .com>
      wrote:
      [color=blue]
      >
      >Martin wrote:[color=green]
      >> I have a page with a form on it in which I have a series
      >> of input fields like so (note the sub-scripted name):
      >>
      >> <input type='text' name='rating[1]' value='1'>
      >> <input type='text' name='rating[2]' value='2'>
      >> <input type='text' name='rating[3]' value='3'>
      >>
      >> How do I retrieve the submitted values in a PHP script?
      >>
      >> For example, just trying to echo the current value
      >> displays nothing (and there IS a value being submitted)
      >>
      >> echo("rating[$i]=".$_POST['rating[$i]']."<br>");[/color]
      >
      >Think about how you would get the value from a multi-level array:
      >$x = array('level1'= >array('1','2') ,'level2'=>arra y('2','4'));
      >echo $x['level1'][0];
      >
      >So, in your case, use:
      >
      >echo 'rating['.$i.']='.$_POST['rating'][$i].'<br>';
      >
      >Read up on arrays at www.php.net/array
      >
      >Ken[/color]

      Thank you.

      Comment

      Working...