Pushing out a PHP variable from a form

Discussion in 'PHP' started by karl_murphy, May 27, 2009.

  1. #1
    Hello all,

    I've got a problem with a form I'm trying to create. Basically the piece of code that follows pulls through items from a database and places them into a table. Each item of data populates a row of the table, and then a new row is created for the next item and so on. For each item of data I want to create a submit button to push through that item's id code. The code is as follows:

    echo "<table align = 'center' border = '1' cellspacing = '0' cellpadding = '5' width = '624'>";
    	echo "<tr>";
    		echo "<th>ID</th>";
    		echo "<th>Manufacturer</th>";
    		echo "<th>Product Code</th>";
    		echo "<th>Common Name</th>";
    		echo "<th>Price (£)</th>";
    		echo "<th>No. In Stock</th>";
    		echo "<th>Minimum Stock Level</th>";
    		echo "<th>Quantity Being Removed</th>";
    		echo "<th>Toner Taken By</th>";
    		echo "<th>Take Toner</th>";
    	echo "</tr>";
    			
    	$sql = "SELECT * FROM `TonerOwner` 
    			WHERE `TO_Type` = 'T' OR `TO_Type` = 'D'";
    			
    	include("connect.php");
    	
    	while ($row = mysql_fetch_assoc($rst))
    	{
    		?>
    		<form action = "<?php echo $PHP_SELF; ?>" method = "post" >
    		<?php
    		echo "<tr>";
    			echo "<td>";
    				echo $row['TO_ID'];
    			echo "</td>";
    			echo "<td>";
    				echo $row['TO_Manufact'];
    			echo "</td>";
    			echo "<td>";
    				echo $row['TO_Code'];
    			echo "</td>";
    			if($row['TO_Name'] == 'Black')
    			{
    				echo "<td bgcolor = '#000000'><font color = '#ffffff'>";
    					echo $row['TO_Name'];
    				echo "</font></td>";
    			}
    			elseif($row['TO_Name'] == 'Cyan')
    			{
    				echo "<td bgcolor = '#00ffff'>";
    					echo $row['TO_Name'];
    				echo "</font></td>";
    			}
    			elseif($row['TO_Name'] == 'Yellow')
    			{
    				echo "<td bgcolor = '#ffff00'>";
    					echo $row['TO_Name'];
    				echo "</font></td>";
    			}
    			elseif($row['TO_Name'] == 'Magenta')
    			{
    				echo "<td bgcolor = '#ff007f'>";
    					echo $row['TO_Name'];
    				echo "</font></td>";
    			}
    			else
    			{
    				echo "<td>";
    					echo $row['TO_Name'];
    				echo "</td>";
    			}
    			echo "<td>";
    				echo $row['TO_Price'];
    			echo "</td>";
    			echo "<td align = 'center'>";
    				echo $row['TO_Qty'];
    			echo "</td>";
    			echo "<td align = 'center'>";
    				echo $row['TO_ReOrder'];
    			echo "</td>";
    			echo "<td align = 'center'>";
    				echo "<select name = 'remove' value = $remove; />";
    					echo "<option value = '-' 'selected'>-</option>";					
    					for($i = 01; $i <= $row['TO_Qty']; $i++)
    					{    
    						printf("<option value = '%s' %s>%s</option>", $i, ($remove == $i),$i);
    					}
    				echo "</select>";
    			echo "</td>";
    			echo "<td align = 'center'>";
    				echo "<select name = 'remove_dept' value = $remove_dept; />";
    					echo "<option value = '-' 'selected'>-</option>";
    					echo "<option value = 'Ad'>Admin</option>";
    					echo "<option value = 'Ar'>Art</option>";
    					echo "<option value = 'Bs'>Business Studies</option>";
    					echo "<option value = 'Dt'>CDT</option>";
    					echo "<option value = 'En'>English</option>";
    					echo "<option value = 'Ft'>Food Technology</option>";
    					echo "<option value = 'Gg'>Geography</option>";
    					echo "<option value = 'Dc'>Graphics</option>";
    					echo "<option value = 'Hi'>History</option>";
    					echo "<option value = 'Cp'>ICT</option>";
    					echo "<option value = 'Lb'>Library</option>";
    					echo "<option value = 'Ma'>Maths</option>";
    					echo "<option value = 'Me'>Media</option>";
    					echo "<option value = 'Mf'>MFL</option>";
    					echo "<option value = 'Mu'>Music</option>";
    					echo "<option value = 'Pe'>PE</option>";
    					echo "<option value = 'Sj'>Science</option>";
    					echo "<option value = 'Sn'>SEN</option>";
    					echo "<option value = 'Sf'>Sixth Form</option>";
    					echo "<option value = 'Sr'>Staff Room</option>";
    					echo "<option value = 'Tx'>Textiles</option>";
    				echo "</select>";
    			echo "</td>";
    			echo "<input type = 'hidden' name = 'pr_id' value = '" . $row['TO_ID'] . "' />";
    			echo "<td>";
    			?>
    				<input type = "submit" name = "submit_form" value = "Submit" onclick = 'javascript: return confirm("Are you sure you want to take from this product line?")' />
    				<?php
    			echo "</td>";
    		echo "</tr>";
    	}
    	echo "</table>";	 	
    	?>
    PHP:
    So far I have managed to print out all the items to the table and place a submit button next to each item. However, no matter which submit button I click the id code of the last item in the table is always printed out. Can anyone see where I am going wrong with this?

    Any help would be greatly appreciated.

    Regards.
     
    karl_murphy, May 27, 2009 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try to close <form> tag
     
    cont911, May 28, 2009 IP
  3. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    The form is closed further down in the code, I just didn't include in what I've posted
     
    karl_murphy, Jun 5, 2009 IP