Reading from a dynamic amount of text boxes
Moderator: General Moderators
Reading from a dynamic amount of text boxes
Ok, let's see if I can break this down.
I'm building a site with php/Oracle for a university class so that students can go on and view their grades, and TA's can go on and enter them.
So, when adding a gradeset, I have it setup so that the TA can select an event (added by the admin), like Exam 1, Homework 4, etc, and then a screen will pop up with empty text boxes for each student ID currently in the database.
For instance, if only Student ID 1, Student ID 2, and Student ID 7 are in the database at the time, a form would come up with 3 empty text boxes, one for student 1, one for student 2, and one for student 7.
I have written this page, it's the handling file that's giving me problems.
Now normally, when I submit a form into php, you name the textbox and then extract it in php likeso:
$points_received=$_POST['name'];
However, the problem is that because the number of textboxes is dynamically changing, I had to name the textbox by the current student ID.
So, the textbox for student 1 is named "1", etc.
Now, how to go about extracting these into php variables since I don't know how many textboxes there will be or what they will be named beforehand?
I was thinking I'd use an array, but how do I get the actual name part for only the student ID's that a grade was entered for?
There is also a second part to this. Not being able to call the database twice at the same time leads to a bit of a problem for me as well.
Once I have the scores into a php array, I was going to simply do a while loop that ran as long as a StudentID could be fetched from the database, and in each iteration if that student ID contained a value in the points_received array that was not null I would do an insert statement into the database with that student_ID and that points_received value. However, my understanding is that I cannot do an insert statement inside a while loop that is being run on a fetch command. So I'm not quite sure as to how to go about this.
Lastly, a bit of a more minor question. When they selected an event and then were passed on to the 'enter grades' form, is there any way to also send the variable that stored the event they selected into the grade handling page for the insert statement I'm using there?
I know it's confusing, but any help would be greatly appreciated.
I'm building a site with php/Oracle for a university class so that students can go on and view their grades, and TA's can go on and enter them.
So, when adding a gradeset, I have it setup so that the TA can select an event (added by the admin), like Exam 1, Homework 4, etc, and then a screen will pop up with empty text boxes for each student ID currently in the database.
For instance, if only Student ID 1, Student ID 2, and Student ID 7 are in the database at the time, a form would come up with 3 empty text boxes, one for student 1, one for student 2, and one for student 7.
I have written this page, it's the handling file that's giving me problems.
Now normally, when I submit a form into php, you name the textbox and then extract it in php likeso:
$points_received=$_POST['name'];
However, the problem is that because the number of textboxes is dynamically changing, I had to name the textbox by the current student ID.
So, the textbox for student 1 is named "1", etc.
Now, how to go about extracting these into php variables since I don't know how many textboxes there will be or what they will be named beforehand?
I was thinking I'd use an array, but how do I get the actual name part for only the student ID's that a grade was entered for?
There is also a second part to this. Not being able to call the database twice at the same time leads to a bit of a problem for me as well.
Once I have the scores into a php array, I was going to simply do a while loop that ran as long as a StudentID could be fetched from the database, and in each iteration if that student ID contained a value in the points_received array that was not null I would do an insert statement into the database with that student_ID and that points_received value. However, my understanding is that I cannot do an insert statement inside a while loop that is being run on a fetch command. So I'm not quite sure as to how to go about this.
Lastly, a bit of a more minor question. When they selected an event and then were passed on to the 'enter grades' form, is there any way to also send the variable that stored the event they selected into the grade handling page for the insert statement I'm using there?
I know it's confusing, but any help would be greatly appreciated.
-
NateFurtwangler
- Forum Newbie
- Posts: 2
- Joined: Thu Jul 15, 2004 11:15 am
Reading from dynamic amount of textboxes
As for the first part, it seems like you really don't need to know which ID's are being updated/submitted.
You could just foreach through $_POST (and filter for submit button etc so you're only looking at form data) and add each response to an array variable.
For instance,
or something to that effect.
You could just foreach through $_POST (and filter for submit button etc so you're only looking at form data) and add each response to an array variable.
For instance,
Code: Select all
<?php
$scores = array();
foreach($_POST as $id=>$score)
{
if($id != 'nameOfSubmitButton')
$scores[$id] = $score;
}
if(!empty($scores))
{
//process $scores array in database
}
?>-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
http://uk2.php.net/manual/en/ref.session.php
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
Re: Reading from dynamic amount of textboxes
Well, here's what I've got, I put the print statements in to try and error check, when I run it it just prints "ArrayArrayArrayArray" to the screen:
Code: Select all
//get grades and put into database//
$scores = array();
foreach($_POST as $id=>$score)
{
echo $ID;
echo $EDID;
//echo $scores;
if($id != 'submit' && $id != 'annotation' && $id != 'date')
{
$scores[$id] = $score;
}
if(!empty($scores))
{
echo $ID;
echo $EDID;
echo $scores[$id];
//process $scores array in database
$query=OCIParse($connect,'insert into mgargano.student_grades values ( :bind1, :bind2, :bind3, :bind4, :bind5)');
OCIBindByName($query, ":bind1", $ID);
OCIBindByName($query, ":bind2", $EDID);
OCIBindByName($query, ":bind3", $scores[$id]);
OCIBindByName($query, ":bind4", $ANN);
OCIBindByName($query, ":bind5", $DTE);
}
}-
NateFurtwangler
- Forum Newbie
- Posts: 2
- Joined: Thu Jul 15, 2004 11:15 am