Submit a textbox's data from a for loop

Hey everyone,

I’m new to the site and this is my first posting. I’m trying to do something here and I’m not sure if I’m going about it right or if it will work at all.

The basic idea is that I have a table that has values already set which I place into a column of textboxes. (the values are taken from a database which I intend to update upon submitting)

So within the for loop I have multiples of this text box in column a table:
<td><center><input type=“TEXT” name=“<?php $nameoffield ?>” METHOD=“POST” value=“<?php echo “$rowoutput” ?>” </td>

And this is the submit button I have:
<td><center><input type=“SUBMIT” name=“updateAssignments” value=“UpdateAll” METHOD =“POST”</td>

How can I use this textbox with a submit button which will submit all the data in all of these different text boxes? Is it possible the way I’m doing it? It doesn’t seem to do anything at all at this point…

Any help is appreciated. Apologies if this is posted in the wrong forum, as the majority of my coding is php… redirect me if necessary! =)

Cheers,
IM Greg.

This might be more of an HTML styled question, depending on how I’m reading this. However, I’m going to answer your question as I understand it and point out a couple coding concerns that I see right away.

For of all, do you have the textboxes and submit button enclosed by a <form> tag? I don’t see it mentioned in your post, but that would be required if the submit button is going to do anything. Sample:

<form action=“form_processing_page.php” method=“post”>
<input type=“text” name=“one_form_box” value=“sample form value” />
<input type=“submit” name=“updateAssignments” value=“UpdateAll” />
<form>

That is a really short form that will submit an update of the value of one_form_box to the page form_processing_page.php where your php coding will take the value from $_POST[‘one_form_box’] and do whatever it is you are going to do with it.

Your input text and input submit tags should not have METHOD=“POST” in them. That is for the <form> tag only. As in the sample, the form tag must start before any text boxes and the closing tag goes after all text boxes and the submit button.

My next concern is that your input, both submit button and text boxes must be properly closed, just like your table cells. Any HTML tag that doesn’t have a separate closing tag must end with />, as in my sample above. If you don’t properly close them, you may get unexpected behaviors from your forms.

For more on forms, see this link: http://www.w3schools.com/html/html_forms.asp

Thanks for the quick reply.

The only reason I kept it out of a separate form was that I didn’t want the submit button to be included beside every textbox. ie:


Column A | Column B |

Info1 | TEXTBOX |
Info2 | TEXTBOX |
Info3 | TEXTBOX |
SUBMIT BUTTON

I want the submit button to be able to enter all these textboxes with the click of the one button. Do i need a separate form for both the submit and textbox to do this?

Thanks again!

You can have (essentially) as many text boxes in a form as you want. On the extreme side, I suppose you could over do it, but within reason, you should be fine.

The submit button will submit all form fields enclosed within the same form (including the submit button itself if it has a name).

Example:

<form action=“form_processor.php” method=“post”>
<input type=“text” name=“text_one” value=“value_one” />
<input type=“text” name=“text_two” value=“value_two” />
<input type=“text” name=“text_three” value=“value_three” />
<input type=“text” name=“text_four” value=“value_four” />
<input type=“text” name=“text_five” value=“value_five” />
<input type=“text” name=“text_six” value=“value_six” />
<input type=“submit” name=“submit” value=“Click Me” />
</form>

If you click the submit button, you will have the following $_POST array:
$_POST[‘text_one’] => ‘value_one’;
$_POST[‘text_two’] => ‘value_two’;
$_POST[‘text_three’] => ‘value_three’;
$_POST[‘text_four’] => ‘value_four’;
$_POST[‘text_five’] => ‘value_five’;
$_POST[‘text_six’] => ‘value_six’;
$_POST[‘submit’] => ‘Click Me’;

You only need the one submit button to submit the form.

I strongly suggest that you read up at the link that I gave you. It will give you great technical knowledge about how HTML forms are constructed and work.

Thanks again for the quick response and help :slight_smile:

That is what I was looking to do, but how can I have this come out in table form? Is there a way to include just an individual textbox from the form in each row of the table (ie. add them one at a time into a table?). I can’t make like 50 forms for this…?

I have already worked with all the form types and how to post them and such, but how to separate them into a table with unique identifier names is confusing me…

One of the main problems is that it is a varying number of rows so I can’t just use a long list of text boxes…

Thoughts?

As I said, as long as they are all enclosed by the same form tags, it will submit as one form. So, if you want to format it into a table, then simply keep the form tags surrounding the table:

<form>
<table>
<tr><td>text box</td></tr>
<tr><td>text box</td></tr>
<tr><td>text box</td></tr>
<tr><td>text box</td></tr>
<tr><td>submit button</td></tr>
</table>
</form>

Or however you specifically want to format the table.

The varying number of rows isn’t going to affect the form. The form will submit as many or few form inputs as you put in.

I’m not sure what you mean by unique identifier names as far as how it is confusing you. Are you worried about duplicating an item?

Yeah but if you have a form that has say 50 textboxes and you only need to display say 5 of them? that just seems messy all around.

To make this more clear (hopefully… haha), i have a database with columns of assignments and rows of student grades. i want these textboxes to be able update the values in the database. if i don’t know the ‘unique’ name of each textbox, it will be hard to tell where to put them back into the database. also, the number of students changes based on the course (could be 5, could be 50) so how do i know how many textboxes to put?

so my php makes a table which will insert the grade into these textboxes, and if they need to update them they will and at the end hit submit and update the database.


student# | A1 |


| box with grade|


| box with grade|


… | … |

if all the textboxes have the same ‘name’, when i grab the value from the POST, it won’t know what box to grab from???

thanks for you patience with me, i’m often confusing and not sure how to ask what im trying to do :blush:

I was under the impression that you were generating the text boxes from your for loop. That would dictate the number of text boxes that your script would create. As you loop through your data records, you create a text box for each row that you retrieve.

The easiest way would be to name your text boxes the name of the database column they are updating. i.e. if you would be updating the column studentGrade in your database, then name your box studentGrade.

If you have multiple text boxes that would have the same name under the strategy above, then you could create a name for the box with a unique identifier for the student, like a student ID. This would give you, for student 23 studentGrade:23. Then, when you access the $_POST array, you split each submitted title at the : and you have a student ID to identify who’s grade and you have the column name studentGrade that you are updating.

I really hope I’m not confusing you in turn.

hahah nah, you’re doing good. I think i get everything now… except:

okay, so when i “include (‘mytextbox.php’)” in the forloop:
Note: i output the table not from the form, but from the php code which includes the form –> is this my problem? i’d really prefer to have it outside of the form.

cuz otherwise, it outputs like so:


student# | A1 |


| box with grade| submit |


| box with grade| submit |


… | … | …|

any way to use the include instead of keeping it all in the form?

Making progress I think… <3

The PHP coding would be outside of the form completely. When the form submits, it sends the key=>value pairs to the php page to process the results and update the database. All of that is separate from the form itself.

Another note, your table here:


student# | A1 |


| box with grade| submit |


| box with grade| submit |


… | … | …|

It looks like you are planning to have multiple submit buttons, one for each line. If you want to do that, then you will want to have separate forms for each student. The buttons don’t know what to do either than to submit all of the inputs for the form at the same time. If you want to submit each student’s grades individually, that would certainly simplify the naming issue. Example:

<table>
<tr><td><form><textbox><submit></form></td></tr>
<tr><td><form><textbox><submit></form></td></tr>
<tr><td><form><textbox><submit></form></td></tr>
<tr><td><form><textbox><submit></form></td></tr>
</table>

Obviously this is not valid HTML coding, merely to demonstrate the point.

In this style, each form tracks only one line of the table, so the name of the text box in the next line is unknown to the form. If you click submit, you will only be submitting the one student’s grade for updating.

Now my example doesn’t break the rows into the cells as you did, so here’s something to consider:

<table>
<form><tr><td>name</td><td><textbox></td><td><submit></td></tr></form>
<form><tr><td>name</td><td><textbox></td><td><submit></td></tr></form>
<form><tr><td>name</td><td><textbox></td><td><submit></td></tr></form>
<form><tr><td>name</td><td><textbox></td><td><submit></td></tr></form>
</table>

Now this is not good HTML, but it will accomplish the goal. You might consider either creating your display in one <td> using CSS to format, or break each row into its own table so that the HTML is still valid, i.e.

<form><table><tr><td>name</td><td><textbox></td><td><submit></td></tr></table></form>
<form><table><tr><td>name</td><td><textbox></td><td><submit></td></tr></table></form>
<form><table><tr><td>name</td><td><textbox></td><td><submit></td></tr></table></form>
<form><table><tr><td>name</td><td><textbox></td><td><submit></td></tr></table></form>

This might be too bulky for your tastes or it might be fine. That part’s up to you.

Okay I think i lost you there. I’m trying to avoid having all these multiple submit boxes. However, I don’t know how the php code will use the form without having both.
I still want just one submit button! :slight_smile:

So this is my forloop to make this more clear:

//for the number of rows
while($row= mysql_fetch_array($result)){
$columnnumber=0;
$rownumber++;

    //for the number of columns
  for($z=0; $z&lt;$numberofcolumns; $z++) {

    $rowoutput= $row[$field[$columnnumber++]-&gt;name];
    $nameoffield= $rownumber . $columnnumber;


    //output text, or else output MY TEXT BOX!!
    if ($rownumber==1 && $columnnumber==1){
      echo "&lt;td&gt;&lt;b&gt;$rowoutput: &lt;/b&gt;&lt;/td&gt;";
    }
    else{   //else, make an editable box with the value from the database
    include ('assignmentupdatemarksbox.php');
    }

  }

THIS IS THE BASIC FORM:
<FORM NAME =“updateAssignmentMarks” METHOD =“POST”>

<td><center><input type=“TEXT” name=“<?php $nameoffield ?>” value=“<?php echo “$rowoutput” ?>” </td>
<td><center><input type=“SUBMIT” name=“updateAssignments” value=“UpdateAll” METHOD =“POST”</td>

</FORM>

now when i include this form, it puts the textbox with the submit button beside it of course. I want the submit to be a completely separate button that goes after the table is finished.

Thanks again!

I’m not sure what’s in your assignmentupdatemarksbox.php file, but, here’s your code in the way that it could be done:


<?php
//for the number of rows
$tableRows = '';
while($row= mysql_fetch_array($result)){
	$columnnumber=0;
	$rownumber++;
	$tableRows .= "<tr>";
	
	//for the number of columns
	for($z=0; $z<$numberofcolumns; $z++) {

		$rowoutput= $row[$field[$columnnumber++]->name];
		$nameoffield= $rownumber . $columnnumber;

		//output text, or else output MY TEXT BOX!!
		if ($rownumber==1 && $columnnumber==1){
			$tableRows .= "<td><b>$rowoutput: </b></td>";
		}
		else{ //else, make an editable box with the value from the database
			$tableRows .= '<input type="TEXT" name="' . $nameoffield . '" value="' . $rowoutput . '" /></td>';
		}

	}
	
	$tableRows .= "</tr>";

}
?>
THIS IS THE BASIC FORM:
<FORM NAME ="updateAssignmentMarks" METHOD ="POST">
<table>
<?php echo $tableRows; ?>
<tr><td><center><input type="SUBMIT" name="updateAssignments" value="UpdateAll" /></td></tr>
</table>

</FORM>

In this style, you create your text boxes as you cycle through your database rows and then add them to the table when you are done. I’ve tabbed the php section to make it a bit easier to read. I removed the include only because I don’t know what it did for sure. Note that I removed the METHOD =“POST” from your submit button.

I also added tags as needed for proper display. It’s not the only way to do it, but it should do the trick. I didn’t test it to make sure it was free of errors, but any errors that I would have created would simply be formatting issues.

An easy way to test it would be to, instead of first attempting the database update, just print_r($_POST); and check to see how the array is coming through when submitted.

The include was including my form. But that looks exactly what I wanted to do! XD

I’ll try it out tomorrow and let you know how it goes. Thanks a million!

Cheeeeers,
IM Greg.

Glad I could help. If you need more assistance, don’t hesitate to ask.

You are my hero. Forgot a <td> line which messed it up for a lil while, but IT’S WORKING NOW!

Cheers, I owe you a few brewskies. XD
Thanks.

-IM Greg.

If the data of these text boxes is linked their names should reflect that by having them be an array.


<input name="input[1]">
<input name="input[2]">

Will be received by PHP as


$_POST['input'][1]; //box 1
$_POST['input'][2]; //box 2

this also makes the generation of the inputs easier

Note - you do not need to specify the type of an input tag unless you need it to be something other than text. The browser will default to a text field. I still do so out of habit, but it’s hardly required.

Adding in the type helps screen readers and such know what type of input it is. Adding an id allows you appropriately assign a label to each text box. Like CSS, each id needs to be different for each textbox.

False on the first point. If type isn’t specified, text is the REQUIRED default. The ID information had nothing to do with what I said - which is that type is no longer considered required as part of the html standard.

Omitting it can cause issues if you want to style against the attribute with

input[type=text]

And there’s no reason not to include it, but it isn’t required.

I always include it out of habit.

Also, I didn’t offer up the idea of using the array option, because I was trying to keep the situation simple. ImGreg was worried about finding the unique name for each value he would be looking for, so I didn’t want to add a layer of complexity. That’s the only reason why I didn’t use it.

Simplicity FTW! nobody else is going to understand my code to start with, so adding in another array will just complicate life – the simpler i keep it the better. thanks for the suggestions tho!

Cheers,
IM Greg.