SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array and POST value

    Hi there,

    I am trying to insert the array values to the DB but cannot get the POST's value.

    I have an input field named 'title[]' that is printed out few times.
    After 'submit' I want to insert that title var to new row of the DB.

    When I use this to print-test my array, it works just fine:
    PHP Code:
    $countArray sizeof($title);

    for (
    $x 0$x $countArray$x++) {
      echo 
    $title[$x] . '<br>';

    but I wanted to use $_POST method instead, so I have
    PHP Code:
    $countArray sizeof($_POST['title']);

    for (
    $x 0$x $countArray$x++) {
      echo 
    $_POST['title'][$x] . '<br>';

    but this way does not return array elements, but array's name elements (A r r a y).
    What Am I doing wrong?


    Thank you!

    G.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does your form submit using post?
    ie <form method="post"...

    Also, you may want to have a look at foreach()
    PHP Code:
    foreach($_POST['title'] as $title) {
      echo 
    "$title<br />\n";

    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  3. #3
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it does work with foreach(), but I got very curious why this way
    PHP Code:
    $_POST['var'][0
    does not?

    anyways,
    Thanks!

    G.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends on how the POST array is being submitted probably. Try doing a print_r($_POST['var']) to see what indexes are being used. Its possible there is no [0]
    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    both ways should work fine.

    im thinking your problem was not in the loop itself, but the array was not what you think it was. basically, those odd values were what was posted to your script.
    PHP Code:
    foreach($_POST['title'] as $title) {
      echo 
    "$title<br />\n";
    }
    $countArray sizeof($_POST['title']);

    for (
    $x 0$x $countArray$x++) {
      echo 
    $_POST['title'][$x] . '<br>';

    should give the same output, twice.


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
  •