Hello all!

happy new year ...

I started learning html and php a few days ago, and im having trouble with using arrays as names in php forms. The PHP manual doesn't have much info on this, neither does any tutorial on the web.

Can anybody help me with the syntax of using arrays in forms ?

thanks

    the syntax for Arrays is:

    $array[key] = value;

    but i dont rightly know if its changed at all in forms.

    If you need anymore help head to Tizag

    EDIT: In response to Bradgranfels(sry if i spelled it wrong)I just copied and pasted it from Tizag.com from the Arrays section of the php tutorial

      Basically, say you have checkboxes like so:

      <input type="checkbox" name="id[]" value="12">12<br/>
      <input type="checkbox" name="id[]" value="123">123<br/>
      <input type="checkbox" name="id[]" value="1234">1234<br/>

      and let's say you check all three and then submit the form.

      Now, $POST['id'] isn't a string, but rather an array. As such, you can loop through it and retrieve the multiple values that were submitted:

      foreach($_POST['id'] as $id) {
          echo 'You checked ID#: ' . $id;
      }

      The most common applications I can think of are checkboxes with the same name (as I illustrated in my example) or a SELECT form entity that allows multiple selections:

      <select multiple="true" name="id[]">
      <option value="12">12</option>
      <option value="123">123</option>
      <option value="1234">1234</option>
      </select>

      EDIT: Also, a note about Htmlwiz's reply: Array indeces in your case should be strings. $row[name] does not have a string as an index, but a constant. PHP will detect this (assuming there actually isn't a constant named 'name') and throw an E_WARNING and then assume it is a string. So, use quotes: $row['name'].

      EDIT: In addition, though I sort of explained it by example, I just want to say it to make it clear: The only change in the HTML code you must make to turn a group of elements into an array is add brackets ( [] ) onto the end of their names.

        well what im trying to do is build a form like so :

        i have a table built with mysql with a few fields, and ive made a form that displays the contents of each field in a texbox, and the form runs within an iterative loop such that each record is also listed.

        i want this form to be used so that i can see the contents of each field of each record simultaneously, modify the contents of each field and allow another php script to take in the form values as arrays and update each record...

        ill post the script in the next message

          <body>

          <?php

          mysql_connect ('localhost','root','');
          mysql_select_db ('test');
          
          $result = mysql_query ("SELECT * FROM courses");
          
          
          while ($row = mysql_fetch_array($result)) { ?>
          
          <form action="index.php" method="post">
          <p><input type="hidden" name="id" value="<?php echo $row["course_id"]; ?>"/>
          Course Name:<br /> <textarea name="name" rows=2 cols=40><?php echo $row["course_name"]; ?></textarea><br />
          Description:<br /> <textarea name="info" rows=6 cols=40><?php echo $row["course_description"]; ?></textarea><br />
          Cost:<br /> <textarea name="cost" rows=1 cols=40><?php echo $row["course_cost"]; ?></textarea></p><?php	
          	} ?>
          <input type="submit" value="Update" />
          </form>
          
          </body>

            Hm... in this case, here's what I would do, assuming that the 'course_id' is a unique ID # (perhaps a primary key in the SQL table?):

            while ($row = mysql_fetch_array($result)) { ?>
            
            <form action="index.php" method="post">
            
            <p><input type="hidden" name="id[<?php echo $row["course_id"]; ?>]" value="<?php echo $row["course_id"]; ?>"/>
            Course Name:<br /> <textarea name="name[<?php echo $row["course_id"]; ?>]" rows=2 cols=40><?php echo $row["course_name"]; ?></textarea><br />
            Description:<br /> <textarea name="info[<?php echo $row["course_id"]; ?>]" rows=6 cols=40><?php echo $row["course_description"]; ?></textarea><br />
            Cost:<br /> <textarea name="cost[<?php echo $row["course_id"]; ?>]" rows=1 cols=40><?php echo $row["course_cost"]; ?></textarea></p><?php	
            	} ?>

            Now, you would access each course_id's newly submitted data like so:

            foreach($_POST['course_id'] as $course_id) {
               $name = $_POST['name'][$course_id];
               $info = $_POST['info'][$course_id];
               $cost = $_POST['cost'][$course_id];
               // do your SQL business...
            }

            EDIT: Note that I didn't do any validation - you'll want to make sure the data is safe/valid (using whatever you prefer) before plugging it into a SQL query...

              The html form doesn't work anymore

              <textarea name="name[<?php echo $row["course_id"]; ?>]" rows=2 cols=40><?php echo $row["course_name"]; ?></textarea>

              instead of doing it like above, can i do the following ?

              <textarea name="name[]" rows=2 cols=40><?php echo $row["course_name"]; ?></textarea>

              Would each iteration of the form in the while loop store each field/record in consecutive variables of the array name[] ?

                yazz wrote:

                Would each iteration of the form in the while loop store each field/record in consecutive variables of the array name[] ?

                Best way to test a theory is to try it. But to answer your question, not specifying an index (as with '[]') will create a numerically indexed array, the first index being 0 and the rest being consective increasing integers.

                  well i have been trying eveyrthing i could think of for the last few hours (and ever since last night)

                  thanks for all the help though .. ill keep trying more

                  other than the PHP manual .. does anyone know of somewhat of an advanced php tutorial ?

                    tizag.com/phpt/
                    is a good one that goes from basics to advanced advanced stuff (yes i know i put 2 advanceds but its true)

                      i think i have been staring at this code and at tutorials and guides and manuals for so long that i kept making some stupid mistake over and over again.

                      anyways i used my old syntax for the form, but i used the syntax you provided for the $_POST (i think thats where i was going wrong over and over again) .. but its worked!

                      thanks again for all your help

                        yazz wrote:

                        i think i have been staring at this code and at tutorials and guides and manuals for so long that i kept making some stupid mistake over and over again.

                        That definitely happens to me as well 😉 Glad I could help!

                          Write a Reply...