I'm working on a script for myself to keep up with student grades online. For example, I have a form where each student name is displayed and then a box for me to input their test grade. I need to be able to insert each student ($userid) and their grade ($grade) in a database table. My problem is getting the script to read each student array from the form once submitted.

Here's a simplified version of the form:

<tr>
<td>Jane Doe</td><input name="name[userid]" type="hidden" value="<?php echo $thisstudent['userid']; ?>">
<td><input type="text" name="name[grade]" size="3" maxlength="3"></td>
</tr>
<td>John Doe</td><input name="name[userid]" type="hidden" value="<?php echo $thisstudent['userid']; ?>">
<td><input type="text" name="name[grade]" size="3" maxlength="3"></td>
</tr>
...and so on....

Basically in my form, each table row will list the name of the student and a place for the grade. Within each row, I have also placed a hidden input to keep track of the student's userid.

Question - Did I name the form fields correct?

Now for the processing script, I know I will need a foreach statement but I'm not sure how to construct it based on what information is coming from the form. Maybe something like:

foreach ($HTTP_POST_VARS as $value)
{
$query="INSERT INTO grades VALUES ('$value['userid']', '$value['grade']')"; 
$result=mysql_query($query);
}

But this doesn't work!

Thanks for ANY help!

    Answer, 'no'. 🙁

    Either use name="name[]" or name="name['userid'][] ... 😉

    The way you have it now, there is only one set of values (the last student) that is being submitted when the form POSTs ... Then you have a rather small array to do your foreach() with ...

    Do you know about [man]var_dump[/man] and [man]print_r[/man] ... ?? They are either one quite useful for analyzing what information is present when you POST to or GET a script ...

      Thanks for the reply, but I'm still not getting it.

      I need to insert the userid and grade for each student on one "submit".

      Can someone suggest how to name the form inputs and how to code the script to cycle through and insert the info for each student? I've played around with it all day and searched different forums but still haven't found a solid way to do it.

        Perhaps this will clarify:

        // gradeform.html
        
        <form action=gradescript.php method=POST>
        
        Name: <input type=text name=name[]>
        <br>
        Score: <input type=text name=score[]>
        <br>
        <br>
        Name: <input type=text name=name[]>
        <br>
        Score: <input type=text name=score[]>
        <br>
        <br>
        Name: <input type=text name=name[]>
        <br>
        Score: <input type=text name=score[]>
        <br>
        <br>
        Name: <input type=text name=name[]>
        <br>
        Score: <input type=text name=score[]>
        <br>
        <br>
        <input type=submit name=submit value="submit scores">
        
        </form>
        
        // gradescript.php
        
        // this script receives the action 
        // and POSTed values from the form...
        
        // use "print_r" to see what's in the array(s)
        
        print_r($_POST['name']);
        echo "<br>\n";
        print_r($_POST['score']);
        echo "<br>\n<br>\n";
        
        // but that's not really a useful thing
        // for anyone but the programmer ...
        
        // let's print the information to the
        // browser ...
        
        // how many times shall we loop*?
        
        $ct=count($name);
        
        for ($loop=0; $loop<$ct; $loop++) {
            echo $_POST['name'][$loop]." - - - ".$_POST['score'][$loop]."
            <br>
            <br>
        ";
        } // end of for loop

        Maybe a better explanation?

        You should do the database work in the for() loop in much the same way (one query for each loop iteration)....

        *You could just as easily save a line by putting the count() expression in the for() loop, but with large scripts or datasets this wastes time and CPU cycles, as the length of the array would have to be calculated $ct times, instead of just once ...

          Thanks dalecosp for spelling it out for me. Works like a charm!

          I have never constructed anything like

          $_POST['name'][$loop]

          and didn't know it could be done. Not still sure how it works but at least it's in my code toolbox so to speak. By the way, I like your quote at the end of your posts. I'll have to mention it to my physics class (I teach in a Christian School). Here's another I found:

          "The more I study science, the more I believe in God."
          -- Albert Einstein

            Kind of a shame, I don't recall ever reading that Einstein quite got it before departing.

            Are you familiar with the work of Hugh Ross?

              Originally posted by sandgnat
              I have never constructed anything like

              $_POST['name'][$loop]

              and didn't know it could be done. Not still sure how it works but at least it's in my code toolbox so to speak.[/B]

              Well, it's simply a multidimensional array --- in PERL, they call it a "hash" ... I'm not mathematician nor physicist enough to give a scientific explanation, but basically $_POST is an array (of everything that was submitted in the form), ['name'] is also an array (it could just as easily be a single variable, but it's not ...) of all the "names" that were input into the form. "score" is a similar array. These two "sub-arrays", if you will, are indexed numerically by PHP, starting at zero.

              We could just as easily have done:

              $namearray=$_POST['name'];
              $scorearray=$_POST['score'];
              
              foreach ($namearray as $n, $scorearray as $s) {
                  echo "<br>$n --- $s";
              }

              Didn't test it, but it should work. It's what [man]foreach/man was created for.

              There is one critical point here. If, by some chance, a particular field is NULL, then from that point on you could accidentally be inserting Ms. Norris' grade for Mr. Miles, and Mr. Osborne's for Ms. Norris, and etc., etc. ...

              So, a sanity check might be a good thing. Perhaps compare the [man]count/man of the two arrays and halt processing if they are not the same size ...

              Have a good week at school 🙂

                Hi There Sandgnat and Dalecosp,

                I'm interested in the thread for two reasons. Sandgnat, if you PM me I have developed a relational back-end and frontend for entering and setting up student grades, testgroups, etc. and I would love to show it to you.

                Dalecosp, I'm very familiar with Hugh Ross and love his work, neat to see a PHP programmer who knows his stuff. Apparently the thing that troubled Einstein the most was that the implications of General Relativity was that the universe had a beginning and was also not cyclical. Points to a creator, a causal agent outside of matter energy space and time (but you probably knew that already :-).

                Also Sandgnat I'd be happy to look at your DB structure but seriously I have a system that is ready and would be happy to touch bases.

                Sincerely,
                Sam Fullman

                  Originally posted by sfullman
                  Hi There Sandgnat and Dalecosp,

                  I'm interested in the thread for two reasons. Sandgnat, if you PM me I have developed a relational back-end and frontend for entering and setting up student grades, testgroups, etc. and I would love to show it to you.

                  And, on that subject, anyone ever looked at moodle?

                  Dalecosp, I'm very familiar with Hugh Ross and love his work, neat to see a PHP programmer who knows his stuff. Apparently the thing that troubled Einstein the most was that the implications of General Relativity was that the universe had a beginning and was also not cyclical. Points to a creator, a causal agent outside of matter energy space and time (but you probably knew that already :-).

                  And, IIRC, he could not reconcile the mathematically provable idea of a benevolent, omnipotent Creator with the observable, anectdotable reality of pain and suffering...

                    This is what I love about forums! You guys teach me so much.

                    I just ordered some of Hugh Ross' books: Beyond the Cosmos and The Creator and the Cosmos.

                    Because I teach from secular books, I'm always looking for resources to help me incorporate God into my lessons. (I'm so thankful I have the right to do so.)

                    Thanks again for all your help dalecosp. And sfullman, I'll be PMing you on that program!😃

                      Write a Reply...