Jump to content

[SOLVED] autoincremented form variable reference problem


Jeffyh

Recommended Posts

I'm new and still unfamiliar with the normal syntax for posting in these forums as well as normal conventions for writing code, so please bear with me. If you need any more info, ask, and I will respond immediately.

 

So I made this form that lists officers of a school and fills text inputs with a default value gotten from a table to allow editing. Each time mysql_fetch_row loops the input name gets autoincremented (ex <input ... name="Position1"...> next loop <input ...name="Position2"...>).

 

In teamchange.php I tried using a for loop that loops as long as the counter is less than or equal to the number of input names created and then updates the table per set ({Position1, FirstName1, LastName1, Email1} {Position2, FirstName2, LastName2, Email2} etc...). The problem is that I can't figure out how to reference the input names using a for loop. What's the best way to handle this?

 

Also could anyone tell me why the nested form causes the outside form to not work?

 


 

Form file:

 

$Result = mysql_query("SELECT * FROM team WHERE Team='$School'");
echo "<h5 class=\"red\">$School</h5>";
echo "<form method=\"POST\" action=\"teamchange.php\">";
echo "<table>";
echo "<tr>";
$Result2 = mysql_query("SELECT Position,FirstName,LastName,Email FROM team");
for($i = 0; $i < mysql_num_fields($Result2); $i++){
$Row2=mysql_fetch_field($Result2,$i);
echo "<td class=\"bold\">$Row2->name</td>";
}
echo "</tr>";
for($i = 0; $Row = mysql_fetch_row($Result); $i++){
$Total = $i;
echo "<tr>";
echo "<td>";
echo "<select>";
$Result3 = mysql_query("SELECT DISTINCT Position FROM team");
while($Row3 = mysql_fetch_row($Result3)){
	if($Row3[0] == $Row[5]){
		echo "<option selected=\"selected\" name=\"Position$i\" value=\"$Row3[0]\">$Row3[0]</option>";
	}
	else{
		echo "<option name=\"Position$i\" value=\"$Row3[0]\">$Row3[0]</option>";
	}
}
echo "</select>";
echo "</td>";
echo "<td><input type=\"text\" name=\"FirstName$i\" value=\"$Row[2]\" size=\"15\"></td>";
echo "<td><input type=\"text\" name=\"LastName$i\" value=\"$Row[3]\" size=\"15\"></td>";
echo "<td><input type=\"text\" name=\"Email$i\" value=\"$Row[4]\" size=\"30\"></td>";
echo "<td>";
/*
echo "<form method=\"POST\" action=\"teamchange.php\">";
echo "<input type=\"hidden\" name=\"School\" value=\"$School\">";
echo "<input type=\"hidden\" name=\"FirstName\" value=\"$Row[2]\">";
echo "<input type=\"hidden\" name=\"LastName\" value=\"$Row[3]\">";
echo "<input type=\"hidden\" name=\"Position\" value=\"$Row[5]\">";
echo "<input type=\"Hidden\" name=\"DeleteChange\" value=\"True\">";
echo "<input type=\"Submit\" value=\"Delete\">";
echo "</form>";
*/
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type=\"Hidden\" name=\"School\" value=\"$Row[0]\">";
echo "<input type=\"Hidden\" name=\"OfficerChanged\" value=\"$Total\">";
echo "<input type=\"Reset\" value=\"Cancel\" onclick=window.history.back()>";
echo "<input type=\"Submit\" value=\"Set\">";
echo "</form>";
?>

 


 

teamchange.php

 

if(isset($_POST['OfficerChanged'])){
$Total = $_POST['OfficerChanged'];
for($i = 0; $i <= $Total; $i++){
	$Position = $_POST['Position$i'];
	$FirstName = $_POST['FirstName$i'];
	$LastName = $_POST['LastName$i'];
	$Email = $_POST['Email$i'];

	mysql_query("UPDATE team SET FirstName='$FirstName', LastName='$LastName', Email='$Email' WHERE Position='$Position'");
}
}

Link to comment
Share on other sites

Firstly, welcome to the forums.

 

Second, the way to present code is to use the


or


tags as appropriate

 

As for your problem, you should be using arrays. For example:

 

<input type="text" name="textfield[0]" /><br />
<input type="text" name="textfield[1]" /><br />
<input type="text" name="textfield[2]" /><br />

 

You can then cycle through them like so:

 

foreach($_POST['textfield'] as $key => value){
    echo "The key was $key and it's value was $value<br />";
}

 

As for nested forms; i've never tried, but it wouldn't surprise me to find they don't work. It looks as if you're trying to have multiple submit buttons? That's entirely possible - just check which one has actually been pressed in your form processing.

Link to comment
Share on other sites

GingerRobot, thank you.

 

Just to be clear... if I use an array. The while statement looping through the rows (Position, FirstName, LastName, Email) of the table uses a for loop to increment the array index?

 

$Result = mysql_query("SELECT Position,FirstName,LastName,Email FROM team");
while($Row = mysql_fetch_row($Result)){
$Columns = [something to figure out # of columns in table];
for($i = 0; $i <= $Columns; $i++){
	echo "<input ... name=\"Array[$i]\">";
	echo "<input ... name=\"Array[$i]\">";
	echo "<input ... name=\"Array[$i]\">";
	echo "<input ... name=\"Array[$i]\">";
}
}

 

which means that

 

Array[0] = Position from row 1

Array[1] = FirstName from row 1

Array[2] = LastName from row 1

Array[3] = Email from row 1

 

Array[4] = Position from row 2

Array[5] = FirstName from row 2

Array[6] = LastName from row 2

Array[7] = Email from row 2

 

 

am I taking this in the wrong direction or is that the jist of it?

Link to comment
Share on other sites

Not quite. You would want separate arrays for each field. Also, you dont need to be figuring out the number of rows in the table. You want something like this:

 

$i=0;
while($Row = mysql_fetch_row($Result)){
    echo "<input ... name=\"position[$i]\">";
    echo "<input ... name=\"firstname[$i]\">";
    //etc
    $i++;
}

Link to comment
Share on other sites

I'm getting owned on this... to be honest, I'm reading up on foreach, and I don't fully quite grasp it.

 

I pretty sure I have the form page correct.

 

$Result = mysql_query("SELECT * FROM team WHERE Team='$School'");
echo "<h5 class=\"red\">$School</h5>";
echo "<form method=\"POST\" action=\"teamchange.php\">";
echo "<table>";
echo "<tr>";
$Result2 = mysql_query("SELECT Position,FirstName,LastName,Email FROM team");
for($i = 0; $i < mysql_num_fields($Result2); $i++){
$Row2=mysql_fetch_field($Result2,$i);
echo "<td class=\"bold\">$Row2->name</td>";
}
echo "</tr>";
for($i = 0; $Row = mysql_fetch_row($Result); $i++){
echo "<tr>";
echo "<td>";
echo "<select>";
$Result3 = mysql_query("SELECT DISTINCT Position FROM team");

while($Row3 = mysql_fetch_row($Result3)){
	if($Row3[0] == $Row[5]){
		echo "<option selected=\"selected\" name=\"Position[$i]\" id=\"Position[$i]\" value=\"$Row3[0]\">$Row3[0]</option>";
	}
	else{
		echo "<option name=\"Position[$i]\" id=\"Position[$i]\" value=\"$Row3[0]\">$Row3[0]</option>";
	}
}
echo "</select>";
echo "</td>";
echo "<td><input type=\"text\" name=\"FirstName[$i]\" id=\"FirstName[$i]\" value=\"$Row[2]\" size=\"15\"></td>";
echo "<td><input type=\"text\" name=\"LastName[$i]\" id=\"LastName[$i]\" value=\"$Row[3]\" size=\"15\"></td>";
echo "<td><input type=\"text\" name=\"Email[$i]\" id=\"Email[$i]\" value=\"$Row[4]\" size=\"30\"></td>";
echo "</tr>";
}
echo "</table>";
echo "<input type=\"Hidden\" name=\"School\" value=\"$Row[0]\">";
echo "<input type=\"Reset\" value=\"Cancel\" onclick=window.history.back()>";
echo "<input type=\"Submit\" name=\"OfficerChanged\" value=\"Set\">";
echo "</form>";

 


 

But nothing I do in the teamchange.php works...

 

40: foreach($_POST['Position'] as $key => $value){
41: 	echo "The key was $key and it's value was $value<br>";
42: }

Gives me an error saying

Warning: Invalid argument supplied for foreach() in C:\Program Files\EasyPHP 2.0b1\www\USCSA\teamchange.php on line 40

 

and somethign as simple as

 

44: echo $_POST['Position'][0];

Gives me an error saying

Notice: Undefined index: Position in C:\Program Files\EasyPHP 2.0b1\www\USCSA\teamchange.php on line 44
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.