Jump to content

Working with Arrays in Forms


Travis959

Recommended Posts

Hey all,

 

I am programming a form that lets users check which items they would like to appear on the details screen, and also allow them to input their own data. This form is generated dynamically using fields from a database to create each row.

 

I am hitting a roadblock because of two things.

 

1) Is there a way for the input fields to not be in the submission url (using $GET), if the checkboxes in their rows are not checked? Right now, all of them get submitted. Here is the url now:

 

creator_custom2.php?product_no[]=9-2-20-198&price_9-2-20-198=9.00&price_9-2-20-204=0.00&price_9-2-20-202=0.00&price_9-2-20-200=0.00&price_9-2-20-206=0.00&price_9-2-20-209=0.00&price_9-2-20-211=0.00&price_9-2-20-195=0.00&price_9-2-20-199=0.00&price_9-2-20-201=0.00&price_9-2-20-205=0.00&price_9-2-20-208=0.00&price_9-2-20-196=0.00&price_9-2-20-210=0.00&price_9-2-20-197=0.00&price_9-2-20-207=0.00&price_9-2-20-203=0.00&price_10-1-20-072=0.00&price_10-1-20-070=0.00&price_10-1-20-071=0.00&price_21404TJXP=0.00&price_21402TJXP=0.00&price_21400TJXP=0.00&price_21396TJXP=0.00&price_21397TJXP=0.00&price_21395TJXP=0.00&price_21398TJXP=0.00&price_21401TJXP=0.00&price_21394TJXP=0.00&price_21399TJXP=0.00&price_21416TJDI=0.00&price_21416TJXP=0.00&price_21419TJDI=0.00&price_21419TJXP=0.00&price_21421TJDI=0.00&price_21421TJXP=0.00&price_21418TJDI=0.00&price_21418TJXP=0.00&price_21415TJDI=0.00&price_21415TJXP=0.00&price_21417TJDI=0.00&price_21417TJXP=0.00&price_21420TJDI=0.00&price_21420TJXP=0.00&price_21398TJDI=0.00&comments=&line=813&discontinued=&seasonal=&newproduct=N&orderby=Description&prices[]=&submit=Create+Design+Sheets

 

Here is what I would like:

 

creator_custom2.php?product_no[]=9-2-20-198&price_9-2-20-198=9.00&submit=Create+Design+Sheets

 

2) How would I go about linking the checkbox field with the input field? Perhaps this related to #1, but for instance, if I wanted to check the first box, and input $9.00 in it's price field, how would I display this on the next page? I can show which boxes are checked fine, but pulling the price field for each is eluding me.

 

index.php?action=dlattach;topic=348986.0;attach=17123;image

 

Here is my code for the form fields:

 

$selectimages = "SELECT * FROM product WHERE line = '$line' ORDER BY $orderby+0, $orderby";
$run = mysql_query($selectimages) or die (mysql_error());

while ($row = mysql_fetch_assoc($run))
{
	$productnumber = $row['Number'];
	$productdescription = $row['Description'];
	$price = $row['Price'];
	$newproduct = $row['NewProduct'];
	echo '<tr>
	<td align="left" valign="top" bgcolor="#e6e6e6" width="30"><input type="checkbox" value="'.$productnumber.'" name="product_no[]"></td>
	<td align="left" valign="top" bgcolor="#e6e6e6" width="150">'.$productnumber.'</td>
	<td align="left" valign="top" bgcolor="#e6e6e6" width="200">'.$linename.'</td>
	<td align="left" valign="top" bgcolor="#e6e6e6" width="400">'.$productdescription;
	if($newproduct == 'Y') {
		echo ' <font color="#FF0000">(new)</font>';
	}
	echo '</td>
	<td align="left" valign="top" bgcolor="#e6e6e6" width="120">$<input type="text" value="'.number_format($price, 2, '.', '').'" name="price_'.$productnumber.'" size="10" maxlength="10"></td>
	</tr>';
}

 

Here's the results page, with just random testing code:

 

$productnumbers = $_GET['product_no'];

print_r($productnumbers);

$price_list = 'price_'.$productnumbers;

echo $_GET['$price_list'];

echo $price_list;

 

[attachment deleted by admin]

Link to comment
Share on other sites

Make the price field an array and use the product number as the index.

 

<input type="text" value="'.number_format($price, 2, '.', '').'" name="price[' . $productnumber . ']" size="10" maxlength="10">

 

$product_no = $_GET['product_no'];
$price = $_GET['price'];

// loop through checkboxes
foreach($product_no as $p)
{
     if (isset($price[$p])) {
          echo $p . ' - ' . $price[$p] . '<br />';
     }
}

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.