• 0

(PHP) POST'ing values from a table with checkboxes?


Question

Sorry for the never ending flow of questions, but I have another question, that i was looking for some help for regarding a search stock page that i have been building:

As you can see from the code below, the search query outputs a table. The user first checks a row corresponding to an item they want (the RFQ column) and then one of the columns (called Quantity), in where the user can enter a quantity of the product they want. They then click a button and a request for quotation will be sent to the company (at the moment im just trying to echo out the selections made).

The query and output:

 	while ($rows = mysql_fetch_array($query))
 	{
 	echo '<tr>';
 	echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
 	echo '<td>' . $rows['Part Number'] . '</td>';
 	echo '<td>' . $rows['Manufacturer'] . '</td>';
 	echo '<td>' . $rows['Stock'] . '</td>';
 	echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[]" tabindex="' . $tabindex++ .'" /></td>';
 	echo '<td><select name="Delivery" tabindex="' . $tabindex++ .'">
 	<option value="Any">Any</option>
 	<option value="Emergency">Emergency</option>
 	<option value="Next Day">Next Day</option>
 	<option value="2-3">2-3 Days</option>
 	<option value="4-6">4-6 Days</option>
 	<option value="7-10">7-10 Days</option>
 	<option value="10+">10+ Days</option>
 	</select></td>';
 	echo '</tr>';
 	}
 	echo '</table>';
 	echo $rows;

 	// Send request for quotation
 	echo '<input type="submit" name ="SendRFQ" value="Request Quotation">';
 	echo '</form>';

Which is then displayed:

// Check if parts have been selected for quotation already
 	if (isset($_POST['SendRFQ']))
 	{
 	$RFQ = $_POST['RFQ'];
 	$Quantity=$_POST['Quantity'];
 	$NumberRFQ = count($RFQ);

 	// List parts selected for quotation
 	if ($NumberRFQ>0)
 	{
 	echo 'RFQs chosen: '.$NumberRFQ.'<br /><br />';
 	echo 'You chose the following:<br /<br />';

 	echo '<table border="1" align="center">';
 	echo '<tr><th>Part Number</th>';
 	echo "<th>Quantity?</th></tr>";

 	for ($i=0; $i<$NumberRFQ; $i++)
 	{
 	echo '<tr>';
 	echo '<td>' . $RFQ[$i] . '</td>';
 	echo '<td>' . $Quantity[$i] . '</td>';
 	echo '</tr>';


 	}
 	echo "</table>";
 	echo '<br /><br />';
 	}
 	else
 	{
 	echo 'No parts have been chosen for quotation<br /><br />';
 	echo '<a href="/search.php">Click here</a> to go back<br /><br />';
 	}
 	}

Which kinda works.

If i select say, the first three rows sequentially, the quantities are displayed correctly.

But say i select rows 1, 3 and 5 (and enter quantities as 1, 3 and 5 respectively), then row 1 shows quantity 1, row 2 shows nothing and row 3 shows quantity 3?!

I understand it is becuase the RFQ[] array only contains the checked part numbers, where as the Quantity[] array contains all the cells contents, so the $i dont match - but i have no idea how to correct this?

Any ideas?

Cheers

Link to comment
Share on other sites

20 answers to this question

Recommended Posts

  • 0

hmm, seems your

name="RFQ[]"

and

name="Quantity[]"

are getting out of sync

you could maybe try having a counter while fetching mysql records and use

name="RFQ[$counter]"

and

name="Quantity[$counter]"

not sure whether this will work, but if it does it'll keep things in sync and preserve the POST-ed arrays you're working with

Link to comment
Share on other sites

  • 0

In a checkbox array, only the checked checkboxes will have values passed (if you have 10 checkboxes and 3 are checked, the checkbox array will only have 3 elements, not 10 with 3 having a value).

One way you can fix this problem, and someone may come up with a better solution later on, but you could use the part number for the index of your quantity array and make it a multi-dimensional array.

echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>'; 
        echo '<td>' . $rows['Part Number'] . '</td>'; 
        echo '<td>' . $rows['Manufacturer'] . '</td>'; 
        echo '<td>' . $rows['Stock'] . '</td>'; 
        echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[' . $rows['Part Number'] . '][]" tabindex="' . $tabindex++ .'" /></td>';

Then when you need to reference that in your display code, you can use the checkbox array's value as the key for the Quantity array.

for ($i=0; $i<$NumberRFQ; $i++) 
        { 
        echo '<tr>'; 
        echo '<td>' . $RFQ[$i] . '</td>'; 
        echo '<td>' . $Quantity[$RFQ[$i]][] . '</td>'; 
        echo '</tr>'; 
        } 

Link to comment
Share on other sites

  • 0

Got it!

 		$stripQuantity = array_filter($Quantity); 		$resetQuantity = array_values($stripQuantity); 		print_r($RFQ); 		echo '<br />'; 		print_r($resetQuantity); 		echo '<br />'; 

First remove the empty values, then reorder the array

Thanks for the help

EDIT: @+metal_dragen Ive just seen your response. Looks smarter than mine... ill check it out. Thanks a lot

EDIT2: Fatal error: Cannot use [] for reading

:(

I have no idea on multidimensional arrays so copied exactly as it and thats what i got.

Link to comment
Share on other sites

  • 0

keep in mind the possibility for people to enter things in the Quantity fields but change their mind and un-check the checkbox, etc..

keeping the two arrays in sync via a common index for both arrays should do it

a multidimensional array is a variation of this anyways

on the error - remove the []

echo '<td>' . $Quantity[$RFQ[$i]][] . '</td>'; 

like this

echo '<td>' . $Quantity[$RFQ[$i]] . '</td>'; 

as you're trying to read an array element using []

d'oh ;-)

Link to comment
Share on other sites

  • 0

Thanks for the reply - that fixed the error.

However, im noticing something very wierd. Sometimes the Manufacturer hasnt been entered and so the cell is just empty. When i select a product that has no Manufacturer, and enter a quantity, nothing is sent to the array?! I dont see any connection in the code between the two?

Im also wondering whether i need to update this:

 	 
$RFQ = $_POST['RFQ']; 		
$Quantity = $_POST['Quantity']; 		
$NumberRFQ = count($RFQ); 

To something like:

 		
$RFQ = $_POST['RFQ']; 		
$Quantity = $_POST['Quantity[RFQ]']; 	 
 $NumberRFQ = count($RFQ); 

?

Thanks

EDIT: Ok, its even wierder. Say i select 3 items without a manufacturer, the first and last will show - not the second?! Try it for yourself here to see what i mean. Try searching for a 6202 and select the first two and last one.

Link to comment
Share on other sites

  • 0

Thanks for the reply. Sure thing. Ill post the whole thing. I appreciate the continued support as i am well and truly stuck!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<body>
<?php

	// Check if parts have been selected for quotation already
	if (isset($_POST['SendRFQ']))
	{
		$RFQ = $_POST['RFQ'];
		$Quantity = $_POST['Quantity'];
		$NumberRFQ = count($RFQ);

		//$stripQuantity = array_filter($Quantity);
		//$resetQuantity = array_values($stripQuantity);
		print_r($RFQ);
		echo '<br />';
		print_r($Quantity);
		echo '<br />';

		// List parts selected for quotation
		if ($NumberRFQ>0)
		{
			echo 'RFQs chosen: '.$NumberRFQ.'<br /><br />';
			echo 'You chose the following:<br /<br />';

			echo '<table border="1" align="center">';
			echo '<tr><th>Part Number</th>';
			echo "<th>Quantity?</th></tr>";

			for ($i=0; $i<$NumberRFQ; $i++)
			{
				echo '<tr>';
				echo '<td>' . $RFQ[$i] . '</td>';
				echo '<td>' . $Quantity[$RFQ[$i]] . '</td>'; 
				echo '</tr>';


			}
			echo "</table>";
			echo '<br /><br />';
		}
		else
		{
			echo 'No parts have been chosen for quotation<br /><br />';
			echo '<a href="/search.php">Click here</a> to go back<br /><br />';
		}
	}

	// Else, check if a part search has been subitted
	elseif (isset($_POST['Search']))
	{
	?>

		<!-- New part search -->
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			Part Number: <input name="PartNumber" type="text" /> <input name="Search" type="submit" value="Search" />
		</form>

		<div id="RFQDetails">
			<!-- Contact Details -->
			<fieldset class="floatLeft">
				<legend>Contact Details</legend>

				<label id="title" for="title">* Title</label>
				<select tabindex="1" name="title">
					<option selected="selected" value="Mr.">Mr.</option>
					<option value="Mrs.">Mrs.</option>
					<option value="Miss">Miss</option>
					<option value="Ms.">Ms.</option>
					<option value="Dr.">Dr.</option>
					<option value="Prof.">Prof.</option>
					<option value="Rev.">Rev.</option>
					<option value="Other">Other</option>
				</select>

				<br>

				<label for="firstName">* First Name</label>
				<input type="text" tabindex="2" class="required" value="" size="30" id="firstName" name="firstName">

				<br>

				<label for="lastName">* Last Name</label>
				<input type="text" tabindex="3" class="required" value="" size="30" id="lastName" name="lastName">

				<br>

				<label for="email">* Email</label>
				<input type="text" tabindex="4" class="required email" value="" size="30" id="email" name="email">

				<br>
			</fieldset>

			<!-- Company Details -->
			<fieldset class="floatRight">
				<legend>Company Details</legend>

				<label for="companyName">* Company Name</label>
				<input type="text" tabindex="5" class="required" value="" size="30" id="companyName" name="companyName">

				<br>

				<label id="country" for="country">* Country</label>
				<select tabindex="6" name="country">
				</select>

				<br>

				<label for="telephone">Telephone Number</label>
				<input type="text" tabindex="7" value="" size="30" id="telephone" name="telephone">

				<br>

				<label for="website">Website</label>
				<input type="text" tabindex="8" value="" size="30" id="website" name="website">

				<br>
			</fieldset>
		</div>

		<div class="clear"></div>

		<?php 
		// Get search input
		$PartSearch = $_POST['PartNumber'];

		// Strip everything except letter, digit, underscore, dash or space
		$PartSearch = preg_replace("/[^\w| |-]/i", "", $PartSearch);

		echo 'Search results for "' . $PartSearch . '":<br />';

		// Connect to database
		mysql_connect("localhost", "Username", "Password") or die(mysql_error());
		mysql_select_db("Database") or die(mysql_error());

		// Search the database
		$query = mysql_query("SELECT * FROM `Stock Search` WHERE `Part Number` LIKE '%$PartSearch%'") or die(mysql_error());

		// Display the results
		$NumberResults = mysql_num_rows($query);

		// If no results are found
		if ($NumberResults == 0) {
			echo 'No results were found for: "' . $PartSearch . '"<br />';
			echo '<a href="/search.php">Click here</a> to go back <br />';
		} 

		// Display the reuslts that are found
		else
		{
			echo $NumberResults . ' matching parts found: <br />';

			// Show the results table and build a new form
			echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
			echo '<table border="1" align="center">';
			echo '<tr>';
			echo '<th>RFQ</th>';
			echo '<th>Part Number</th>';
			echo '<th>Manufacturer</th>';
			echo '<th>Stock</th>';
			echo '<th>Quantity</th>';
			echo '<th>Delivery</th>';
			echo '</tr>';

			$tabindex = 8;
			while ($rows = mysql_fetch_array($query))
			{
				echo '<tr>';
				echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
				echo '<td>' . $rows['Part Number'] . '</td>';
				echo '<td>' . $rows['Manufacturer'] . '</td>';
				echo '<td>' . $rows['Stock'] . '</td>';
				echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[' . $rows['Part Number'] . ']" tabindex="' . $tabindex++ .'" /></td>';
				echo '<td><select name="Delivery" tabindex="' . $tabindex++ .'">
						<option value="Any">Any</option>
 	<option value="Emergency">Emergency</option>
 	<option value="Next Day">Next Day</option>
 	<option value="2-3">2-3 Days</option>
 	<option value="4-6">4-6 Days</option>
 	<option value="7-10">7-10 Days</option>
 	<option value="10+">10+ Days</option>
					</select></td>';
				echo '</tr>';
			}
			echo '</table>';
			echo $rows;

			// Send request for quotation
			echo '<input type="submit" name ="SendRFQ" value="Request Quotation">';
			echo '</form>';
		}

	}
	// If nothing is POST'd - display a new search
	else
	{
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	Part Number: <input name="PartNumber" type="text" /> <input name="Search" type="submit" value="Search" />
</form>

<?php
	}
?> 

</body>
</html>

Link to comment
Share on other sites

  • 0

sorry for the delay, I was busy..

hmm

name="Quantity[' . $rows['Part Number'] . ']"

and

name="RFQ[]"

seem to be using different methods of storing the values in the request arrays

try keeping it consistent (use a consistent index like $rows['Part Number'] or some simple counter) so that all your Array populating inputs are using the same element indexes per each of the rows of inputs

something like this

name="RFQ[' . $rows['Part Number'] . ']"

will make sure whenever you find an element of one of the arrays, you can consistently count on finding the matching elements in the other arrays (for the other input fields)

I hope this makes sense

Cheers

Link to comment
Share on other sites

  • 0

Thanks for your reply.

I think i see where you're coming from, but then i dont see i will show the results, as:

			for ($i=0; $i<$NumberRFQ; $i++)
			{
				echo '<tr>';
				echo '<td>' . $RFQ[$i] . '</td>';
				echo '<td>' . $Quantity[$RFQ[$i]] . '</td>'; 
				echo '</tr>';


			}

no longer works, becuase the arrays would look like:

Array ( [6202-16ZZ] => 6202-16ZZ [62022RS] => 62022RS )

Array ( [6202] => [6202-16ZZ] => 10 [62022RS] => 20 [62022RSC3] => [6202 2RSC3] => [62022RSNR] => [6202FF] => [6202 FF] => [6202NR] => [6202 NR] => [6202Z] => [6202 ZE] => [6202 ZZ] => [6202ZZ] => [LRB 162020] => )

How would i cycle through part numbers that have been selected?

Also, i have noticed that this still doesnt solve the problem of missing quantities?!

Another thing, part number is not necessarily always the same, so i think its bad to rely on it as an index, for example:

6202 - Manufacturer 1

6202 - Manufacturer 2

Probably should have thought of that sooner!

Link to comment
Share on other sites

  • 0

Well i think ive given up with all these and will have to return to my previous

		$stripQuantity = array_filter($Quantity);
		$resetQuantity = array_values($stripQuantity);

Method becuase it just works, and ill have to just do some validation to make sure someone doesnt tick a box but then not enter a quantity (and hence screw up the array).

Thanks for all the help - no doubt ill be back soon!

Link to comment
Share on other sites

  • 0

I still think the stripping and validating and such is a messy way of doing it. All you need is a tiny logical move (introduce a row index variable, use it across all fields belonging to the same row) to keep them all in line :-)

So array1[index] would be corresponding to array2[index], array3[index] etc.. natively

Good luck, anyways :-)

Link to comment
Share on other sites

  • 0

Yeah ive just realised i need to scrap that. I tried to POST over the Manufacturer and it all went to hell again...

Yep i now completely support what you are suggesting. Some kind of hidden ID field for each row, that i can then reference in any array i want.

Im going to give it a go now, but no doubt it will end badly! Thanks again for all your help. Ill report back!

But any example code you may want to post in the mean time will be much appreciated :p

EDIT: Yeah, ok i actually have no idea how to code this, call i have is:

$id = 0;
			while ($rows = mysql_fetch_array($query))
			{
				echo '<tr>';
				echo '<input type="hidden" value="' . $id++ . '" name="Identification">';
				echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
				echo '<td>' . $rows['Part Number'] . '</td>';
				echo '<td>' . $rows['Manufacturer'] . '</td>';
				echo '<input type="hidden" value="' . $rows['Manufacturer'] . '" name="Manufacturer[]">';
				echo '<td>' . $rows['Stock'] . '</td>';
				echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[]" tabindex="' . $tabindex++ .'" /></td>';
				echo '<td><select name="Delivery" tabindex="' . $tabindex++ .'">
						<option value="Any">Any</option>
 	<option value="Emergency">Emergency</option>
 	<option value="Next Day">Next Day</option>
 	<option value="2-3">2-3 Days</option>
 	<option value="4-6">4-6 Days</option>
 	<option value="7-10">7-10 Days</option>
 	<option value="10+">10+ Days</option>
					</select></td>';
				echo '</tr>';
			}

Would you mind spelling it out a bit for me?

Link to comment
Share on other sites

  • 0

Yeah ive just realised i need to scrap that. I tried to POST over the Manufacturer and it all went to hell again...

Yep i now completely support what you are suggesting. Some kind of hidden ID field for each row, that i can then reference in any array i want.

Im going to give it a go now, but no doubt it will end badly! Thanks again for all your help. Ill report back!

But any example code you may want to post in the mean time will be much appreciated :p

EDIT: Yeah, ok i actually have no idea how to code this, call i have is:

$id = 0;
			while ($rows = mysql_fetch_array($query))
			{
				echo '<tr>';
				echo '<input type="hidden" value="' . $id++ . '" name="Identification">';
				echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
				echo '<td>' . $rows['Part Number'] . '</td>';
				echo '<td>' . $rows['Manufacturer'] . '</td>';
				echo '<input type="hidden" value="' . $rows['Manufacturer'] . '" name="Manufacturer[]">';
				echo '<td>' . $rows['Stock'] . '</td>';
				echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[]" tabindex="' . $tabindex++ .'" /></td>';
				echo '<td><select name="Delivery" tabindex="' . $tabindex++ .'">
						<option value="Any">Any</option>
 	<option value="Emergency">Emergency</option>
 	<option value="Next Day">Next Day</option>
 	<option value="2-3">2-3 Days</option>
 	<option value="4-6">4-6 Days</option>
 	<option value="7-10">7-10 Days</option>
 	<option value="10+">10+ Days</option>
					</select></td>';
				echo '</tr>';
			}

Would you mind spelling it out a bit for me?

no need for a hidden ID

name="RFQ['.$index.']"

name="Manufacturer['.$index.']"

and so on for each field that belongs to the same row

then you increment $index so the new row's array alements have their own consistent key

This way your POST-ed arrays RFQ, Manufacturer, etc.. will all contain corresponding elements with the same key

Then you go through the array that matters with foreach($arrayName as $key=>$value){...}

inside the code {..} you have access to the particular $key variable, and using it you can get the corresponding elements from the other POST-ed arrays..

foreach($_POST['Manufacturer'] as $key=>$manufacturerValue){
  $rfqValue = $_POST['RFQ'][$key]; // the RFQ that was part of the same row, as it has the same $key (this was the $index in the form rendering loop)
.....
}

I hope this makes sense

:-)

Link to comment
Share on other sites

  • 0

Sorry for the delay in replying, but thanks a lot for all your help. I do think this is a much better way of doing things, but am having a bit of trouble getting my head around it (the most advanced thing i'd done before this was a contact form!)

The main thing is i dont see how i can assign name="..." to Manufacturer (or Part Number or Stock). They are just <td>'s - dont i need to have <input>'s to set the name=".."? My understanding is that to POST something, first you need a <form>, inside of which you have <input>'s, of which the value=".." is POSTed? So all i have so far is:

			$index = 0;
			while ($rows = mysql_fetch_array($query))
			{
				echo '&lt;tr&gt;';
				echo '&lt;td&gt;&lt;input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ['. $index++ .']"&gt;&lt;/td&gt;';
				echo '&lt;td&gt;' . $rows['Part Number'] . '&lt;/td&gt;';
				echo '&lt;td&gt;' . $rows['Manufacturer'] . '&lt;/td&gt;';
				echo '&lt;td&gt;' . $rows['Stock'] . '&lt;/td&gt;';
				echo '&lt;td&gt;&lt;input type="text" value="' . $_POST['Quantity'] . '" name="Quantity['. $index++ .']" tabindex="' . $tabindex++ .'" /&gt;&lt;/td&gt;';
				echo '&lt;td&gt;&lt;select name="Delivery" tabindex="' . $tabindex++ .'"&gt;
						&lt;option value="Any"&gt;Any&lt;/option&gt;
 	&lt;option value="Emergency"&gt;Emergency&lt;/option&gt;
 	&lt;option value="Next Day"&gt;Next Day&lt;/option&gt;
 	&lt;option value="2-3"&gt;2-3 Days&lt;/option&gt;
 	&lt;option value="4-6"&gt;4-6 Days&lt;/option&gt;
 	&lt;option value="7-10"&gt;7-10 Days&lt;/option&gt;
 	&lt;option value="10+"&gt;10+ Days&lt;/option&gt;
					&lt;/select&gt;&lt;/td&gt;';
				echo '&lt;/tr&gt;';
			}
			echo '&lt;/table&gt;';

Sorry i need to be spoon fed everything, but i really appreciate the support!

Link to comment
Share on other sites

  • 0

You could include hidden inputs

&lt;input type="hidden" name="....." value="......" /&gt; 

inside each of your 'non-input' TDs (for clarity). Set the name and value attributes accordingly, so that you have the data where needed in the POST-ed array(s)

Those hidden inputs are both invisible and accomplish passing data at POST time

I hope this helps

P.S. Think of the whole exercise as a slightly more advanced way of submitting simple data with a form

The more advanced stuff is just how you craft your names of your inputs, so that the data gets nicely grouped in arrays at the server-side. It's that simple.

Then the struggle with checkboxes being left out - just name them so they get coupled to other never-empty inputs (you've done this already I think) and don't iterate using the checkboxes' data, but something more consistently non-empty ;-)

Link to comment
Share on other sites

  • 0

Hey,

Just wanted to say thanks for much for all your help. I think ive cracked it now! The way you suggested is so much better than anything i could have come up with and i feel ive learnt alot (especially about arrays). So thanks again.

Im sure ill be back with more questions sometime soon though! (Still need to intergrate the contact details and mail it, but that should be easy-ish)

Cheers

Link to comment
Share on other sites

  • 0

Ive just been tweaking it and it all seems good so far!

I was wondering though if you could clarify by what you mean here:

"Then the struggle with checkboxes being left out - just name them so they get coupled to other never-empty inputs (you've done this already I think) and don't iterate using the checkboxes' data, but something more consistently non-empty ;-) "

Ive been using:

			while ($rows = mysql_fetch_array($query)) {
				echo '&lt;tr&gt;';
				echo '&lt;td&gt;&lt;input type="checkbox" value="' . $index . '" name="id['. $index .']"&gt;&lt;/td&gt;';
				echo '&lt;td&gt;&lt;input type="hidden" value="' . $rows['Part Number'] . '" name="partNumber['. $index .']"&gt;' . $rows['Part Number'] . '&lt;/td&gt;';
				echo '&lt;td&gt;&lt;input type="hidden" value="' . $rows['Manufacturer'] . '" name="manufacturer['. $index .']"&gt;' . $rows['Manufacturer'] . '&lt;/td&gt;';
				echo '&lt;td&gt;&lt;input type="hidden" value="' . $rows['Stock'] . '" name="stock['. $index .']"&gt;' . $rows['Stock'] . '&lt;/td&gt;';
				echo '&lt;td&gt;&lt;input type="text" value="" name="quantity['. $index .']" tabindex="' . $tabindex++ .'" /&gt;&lt;/td&gt;';
				echo '&lt;td&gt;&lt;select name="delivery['. $index .']" tabindex="' . $tabindex++ .'"&gt;
						&lt;option value="Any"&gt;Any&lt;/option&gt;
						&lt;option value="Emergency"&gt;Emergency&lt;/option&gt;
						&lt;option value="Next Day"&gt;Next Day&lt;/option&gt;
						&lt;option value="2-3 Days"&gt;2-3 Days&lt;/option&gt;
						&lt;option value="4-6 Days"&gt;4-6 Days&lt;/option&gt;
						&lt;option value="7-10 Days"&gt;7-10 Days&lt;/option&gt;
						&lt;option value="10+ Days"&gt;10+ Days&lt;/option&gt;
					&lt;/select&gt;&lt;/td&gt;';
				echo '&lt;/tr&gt;';

				// Iterate for next row
				$index++;
			}

and

// Foreach of the parts selected (the id array), select the value corresponding to each key
			foreach($_POST['id'] as $key=&gt;$value){

				// Using the same key (id value), select the corresponding values from each of the arrays
				$partNumberValue = $_POST['partNumber'][$key];
				$manufacturerValue = $_POST['manufacturer'][$key];
				$stockValue = $_POST['stock'][$key];
				$quantityValue = $_POST['quantity'][$key];
				$deliveryValue = $_POST['delivery'][$key];

				echo '&lt;tr&gt;';
				echo '&lt;td&gt;' . $partNumberValue . '&lt;/td&gt;';
				echo '&lt;td&gt;' . $manufacturerValue . '&lt;/td&gt;';
				echo '&lt;td&gt;' . $stockValue . '&lt;/td&gt;';
				echo '&lt;td&gt;' . $quantityValue . '&lt;/td&gt;';
				echo '&lt;td&gt;' . $deliveryValue . '&lt;/td&gt;';
				echo '&lt;/tr&gt;';
			}

Is that ok?

Finally, just as a general question, i was wondering whether someone could clarify the difference between GET and POST - am i right in using POST here? Am i right in thinking the only(?) difference between the two is that GET changes the URL?

Thanks for the help, ive really learnt a lot

Link to comment
Share on other sites

  • 0

Hi,

Glad you're progressing

What I meant was based on the fact that (in some browsers at least) checkboxes values have the habit of not being submitted at all if not checked

This means that looping with foreach(checkboxArray as... would hypothetically not yield a full number of loops, if you're after the full number of loops (i.e. to rebuild the form based on everything the user submitted, regardless of whether a checkbox leading the row was checked.)

It'd be better to do a foreach( loop on some array that would hold the full number of values, although some would be empty. I think text inputs work fine for this purpose.

GET vs POST

as a loose rule of thumb - use GET to be getting data (including specifying the data you want to get - categories location, search results, particular page number, posts from a date, with a tag, etc..)

POST - submitting things to the server that you'd not like to submit more than once and generally don't focus on the reply it comes back with

Cheers

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.