Link to home
Start Free TrialLog in
Avatar of UniqueData
UniqueDataFlag for United States of America

asked on

Post not working for arrays

what is wrong with the following code?  For $outputResult  I am getting garbage.

<head>
<?php
$outputResult='nothing to see';
if ( !empty($_POST['postData'])  )
{ 
    $outputResult = '<table border="1">';
    
    foreach ( $_POST['entry'] as $entry )
    {
        $outputResult .= '<tr>';
        $outputResult .= '  <td>'. $entry['uuk']. '</td>';
        $outputResult .= '  <td>'. $entry['ActivityID']. '</td>';
        $outputResult .= '  <td>'. $entry['Units']. '</td>';
        $outputResult .= '  <td>'. $entry['RegHours']. '</td>';
        $outputResult .= '  <td>'. $entry['OTHours']. '</td>';
        $outputResult .= '</tr>';
    }
    $outputResult .= '</table>';  
}
?>
</head>
<body>

<form method="post" name="newActivities" action="test.php">
	<table>
		<tbody>
	  		<tr>
	  			<td class='headcol'>Doe, John</td>
	  			<td>
	  				<input type='hidden' name='entry[uuk]' value='d83a0d81d478dfe53d1d1cca01e38620'>
	  				<input type='hidden' name='entry[ActivityID]' value='9'>
	  				<input class='qty' type='text' name='entry[Units]' size='4'>
				</td>
				<td>
					<input class='regHrs' type='text' name='entry[RegHours]' size='4'>
				</td>
				<td>
					<input class = 'otHrs' type='text' name='entry[OTHours]' size='4'>
				</td>
				<td>
					<input type='hidden' name='entry[uuk]' value='d83a0d81d478dfe53d1d1cca01e38620'>
					<input type='hidden' name='entry[ActivityID]' value='10'>
					<input class='qty' type='text' name='entry[Units]' size='4'>
				</td>
				<td>
					<input class='regHrs' type='text' name='entry[RegHours]' size='4'>
				</td>
				<td>
					<input class = 'otHrs' type='text' name='entry[OTHours]' size='4'>
				</td>
			</tr>
		</tbody>
	</table>
    <button type="submit" name = "postData" class="btn btn-default" value="clicked">Post Data</button>
</form>
<?php echo $outputResult; ?>
 </body>

Open in new window

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Try changing:
<button type="submit" name = "postData" class="btn btn-default" value="clicked">Post Data</button>

to:

<input type="submit" name="postData" value="Post Data">
I made no changes and it works. Check your server settings?
I also noticed you have duplicate input names, so the latter ones would overwrite the former.
Why are you using a foreach loop? You just have to preocess a form, isn't it?

<?php
$outputResult='nothing to see';
if ( !empty($_POST['postData'])  )
{ 
    $outputResult = '<table border="1">';
		$outputResult .= '<tr>';
		$outputResult .= '  <td>'. $_POST['entry']['uuk']. '</td>';
		$outputResult .= '  <td>'. $_POST['entry']['ActivityID']. '</td>';
		$outputResult .= '  <td>'. $_POST['entry']['Units']. '</td>';
		$outputResult .= '  <td>'. $_POST['entry']['RegHours']. '</td>';
		$outputResult .= '  <td>'. $_POST['entry']['OTHours']. '</td>';
		$outputResult .= '</tr>';
    $outputResult .= '</table>';  
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UniqueData

ASKER

gonzo, adding the [0], [1], etc worked perfectly.  Thanks. Instead of incrementing I will just use a combination of uuk and ActivityID since that combination will be unique. I left the button alone, it seems to work ok as is (using Bootstrap, so I just used their sample code).


marco, this is just a small sample of what I am trying to achieve.  There will be many more columns going across for various ActivityIDs and many rows down for various StaffID.  In the example I posted that one row currently needs to return two records, but the final result each row will need to return many records. I can't think of anyway of doing that without a loop. Is there?
I am having an issue when I use my whole recordset (all activities and all staff).  Nothing gets returned.  When I limit to a single staff or group of activities then it works.

Is this a limitation with arrays and if so, what is a workaround?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it turned out to be the line:
     if ( !empty($_POST['postData'])  )

I changed it to if ( !empty($_POST)  )  when trying out Ray's suggestion and it worked for the full recordset.  

Odd, though, that when I had a smaller recordset the $_POST['postData']) seemed to work fine.