Jump to content

Need help on posting an array of text that are checkboxed


Recommended Posts

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.

 

<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:

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?

 

 

You should really keep quotes around your attributes.

If the text contained a space, you'd be sunk.

 

Other than that, text inputs posting as part of an array should work fine.

such as..

<?
print_r($_POST);
?>
<form method="post">
<input type="text" name="qty[]" /><br />
<input type="text" name="qty[]" /><br />
<input type="text" name="qty[]" /><br />
<input type="submit" />
</form>

d'oh! Ok, I fixed it and that eliminated the "blank" problem. Thank you for your help on that.

 

But the problem now is it's posting the values from the text field (qty) in the order they are in the table, rather than posting the values that I check.

 

For example, if I have multiple rows like this:

 

[checkbox1] [qty1]

[checkbox2] [qty2]

[checkbox3] [qty3]

 

When I click checkbox1 and 3, It echoes qty1, and 2.

 

How can I get it to echo the qty that corresponds with the appropriate row that is checked?

 

As only checked c/boxes are posted,

<?php
foreach ($_POST['no'] as $i => $no)
{
     echo $no, ' ', $_POST['qty'][$i], '<br/>';
}

 

PS. pre-index the items in the form

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

<input type=checkbox name=no[2] value=$row[1]></input>
<input type=text name=qty[2] value=$row[2]></input>

</form>

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.