Jump to content

Form Array - Show Values


tomtimms

Recommended Posts

I have an edit form that I can't seem to get the values from when submitted.  If the first checkbox is checked I can get and display all the values.  However if anything other than the first box is checked I just get the checkbox value.  Here is my form.

 

<input type="checkbox" name="cbox[]" value="<?PHP echo $rows['id'];?>" />

 

<input type="text" name="adjustment[]" size="5" />

 

here is my process page

 

$id = $_POST['cbox'];

$adjustment = $_POST['adjustment'];

 

 

// loop through array

$numb= count($id);

$number = $numb-1;

 

for ($i=0; $i<=$number; $i++)

{

 

    $itno = $id[$i];

    $desc = $adjustment[$i];

 

 

 

 

    if ($id[$i] <> '') {

    echo "Item: " . $itno . "  Description: " . $desc . "<p>";

    }

}

 

 

I am trying to pass the values from the form from only the boxes that are checked.  Anyone know what I did  wrong?

Link to comment
https://forums.phpfreaks.com/topic/201017-form-array-show-values/
Share on other sites

Could you post the entire form and php script.

 

I am not sure why you named your first check box cbox[]

 

But one thing I notice is that your $id variable you have

$id = $_POST['cbox'];

 

I think it sould be

 

$id = $_POST['cbox[]'];

 

But post the whole script and I will try my best to help.

 

 

 

The way HTML handles checkbox arrays is that only those items that are checked get sent to the processing script.

 

Put

<?php
if (isset($_POST['cbox'])) {
  echo '<pre>' . print_r($_POST,true) . '</pre>';
}
?>

at the start of your processing script to see what is being sent.

 

See if my test script helps you:

<?php
if (isset($_POST['submit'])) {
	echo '<pre>' .print_r($_POST,true) . '</pre>';
	if (isset($_POST['cbox'])) {
		foreach ($_POST['cbox'] as $i => $item) {
			echo $i . ': ' . $item . ' -- Adjustment: ' . $_POST['adjustment'][$i] . "<br>\n";
		}
	}
}
?>
<html>
<head>
	<title>Testing</title>
</head>
<body>
	<form action="" method="post">
		<?php
			$num = rand(5,10);
			for ($i=0;$i<$num;++$i) {
				echo $i . ': <input type="checkbox" name="cbox[' . $i . ']" value="' . rand(100,200) . '">, Adjustment: <input type="text" name="adjustment[' . $i . ']" size="5"><br>' . "\n";
			}
		?>
		<input name="submit" value="Send Values" type="submit">
	</form>
</body>
</html>

 

Ken

Ok so far I have this as my Form Code

 

<html>
   <head>
      <title>Testing</title>
   </head>
   <body>
      <form action="processing.php" method="post">
         <?php
      
while ($rows = mysql_fetch_assoc($result)){

               echo '<input type="checkbox" name="cbox[]" value="' . $rows['company'] . '"> Adjustment: <input type="text" name="adjustment[]" size="5"> Company: '.$rows['name'].' <br>' . "\n";
            
            }
         ?>
         <input name="submit" value="Send Values" type="submit">
      </form>
   </body>
</html>

 

and this as my processing

if (isset($_POST['submit'])) {

    if (isset($_POST['cbox'])) {
        
        
       foreach ($_POST['cbox'] as $i => $item) {
        
        
        $sql = "UPDATE revenue_company SET status = '1' ,adjustment = ".mysql_real_escape_string($_POST['adjustment'][$i])." WHERE company = ".$item."";
        $result = $db->db_query($sql);
        
        echo $sql;

       }
    }
}

 

 

My form display's 3 rows of data.  If you check the second row or third row that adjustment field doesn't process.  However if you select all 3 rows it works just fine.  Anyone know what's going on?

 

If you notice is my sample code, I use an explicit index when creating the input checkbox arrays, without it the arrays sent to your script won't match up correctly. Change your code to be:

<?php
$i = 0;
while ($rows = mysql_fetch_assoc($result)){

               echo '<input type="checkbox" name="cbox[' . $i . ']" value="' . $rows['company'] . '"> Adjustment: <input type="text" name="adjustment[' . $i . ']" size="5"> Company: '.$rows['name'].' <br>' . "\n";
               $i++;
            
            }
         ?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.