Need help on posting an array of text that are checkboxed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moleculo
Forum Newbie
Posts: 18
Joined: Thu Nov 15, 2007 9:05 am

Need help on posting an array of text that are checkboxed

Post 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]
dancaragea
Forum Newbie
Posts: 10
Joined: Sat Oct 14, 2006 2:03 pm

Post 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;
moleculo
Forum Newbie
Posts: 18
Joined: Thu Nov 15, 2007 9:05 am

Post 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.....
dancaragea
Forum Newbie
Posts: 10
Joined: Sat Oct 14, 2006 2:03 pm

Post by dancaragea »

You don't need the for(). Just the print_r() and/or echos
Post Reply