Page 1 of 1

Need help on posting an array of text that are checkboxed

Posted: Tue Dec 04, 2007 11:40 am
by moleculo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello. I'm trying to post an array of various rows all at once. One field has the "checkbox" type, the other has the "text" type. I want it to post all the values for both fields but only for the rows that are checked (could be more than 1). I can get the checkbox type to post, but not the text.

[syntax="html"]<form action=update.php method=post>
<input type=checkbox name=no[] value=$row[1]></input>
<input type=text name=qty[] value=$row[2]></input>
</form>
update.php:[/syntax]

Code: Select all

if(isset($_POST['update']))
{
	for ($i=0; $i<count($_POST['no']);$i++) 
	{
	    $no = $_POST['no'][$i];
	    $qty = $_POST['qty'][$i];
	    echo "no = $no<br>";
	    echo "qty = $qty<br>";
	}
}
Using the above code, I get the correct values for $no, but for $qty, I get blank.

Any ideas what I'm doing wrong?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Dec 04, 2007 6:04 pm
by dancaragea
First of all use double quotes in html around attribute values like this:

Code: Select all

<form action="update.php" method="post">
<input type="checkbox" name="no[]" value="1">
<input type="text" name="qty" value="some value">
</form>
Second, the <input> html element has no ending tag, see my code above.

Third, adding the square brackets in your variable "no[]" makes sense for checkboxes elements only (in most situations). All others should probably have no brackets like the "qty" in my code above.

Fourth, in update.php, you read the vars like this:
$no=$_POST['no'];
$qty=$_POST['qty'];
$no is an array now (with one element but still an array) due to the square brackets in the variable name while $qty is a string variable.
To display them you could do something like:
print_r($no);
echo $no[0];
echo $qty;

Posted: Wed Dec 05, 2007 11:00 am
by moleculo
Thank you for your help.

I fixed everything with the quotes and the tags.

Where should I put the
print_r($no);
echo $no[0];
echo $qty;

Do you mean put it inside the
for ($i=0; $i<count($_POST['no']);$i++)
{ }

Or should I completely remove the for ($i.....

Posted: Wed Dec 05, 2007 1:13 pm
by dancaragea
You don't need the for(). Just the print_r() and/or echos