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

LVL 7
UniqueDataAsked:
Who is Participating?
 
gr8gonzoConsultantCommented:
Try changing entry[fieldname] in your inputs to entry[0][fieldname]:

Old:
entry[OTHours]

Open in new window


New:
entry[0][OTHours]

Open in new window


For each complete entry, just increment [0]. For example:

<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[0][uuk]' value='d83a0d81d478dfe53d1d1cca01e38620'>
	  				<input type='hidden' name='entry[0][ActivityID]' value='9'>
	  				<input class='qty' type='text' name='entry[0][Units]' size='4'>
				</td>
				<td>
					<input class='regHrs' type='text' name='entry[0][RegHours]' size='4'>
				</td>
				<td>
					<input class = 'otHrs' type='text' name='entry[0][OTHours]' size='4'>
				</td>

				<td>
					<input type='hidden' name='entry[1][uuk]' value='d83a0d81d478dfe53d1d1cca01e38620'>
					<input type='hidden' name='entry[1][ActivityID]' value='10'>
					<input class='qty' type='text' name='entry[1][Units]' size='4'>
				</td>
				<td>
					<input class='regHrs' type='text' name='entry[1][RegHours]' size='4'>
				</td>
				<td>
					<input class = 'otHrs' type='text' name='entry[1][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


That should give you an array of entries that you can loop through.
0
 
gr8gonzoConsultantCommented:
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">
0
 
Dallas SmetterSolution EngineerCommented:
I made no changes and it works. Check your server settings?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
gr8gonzoConsultantCommented:
I also noticed you have duplicate input names, so the latter ones would overwrite the former.
0
 
Dallas SmetterSolution EngineerCommented:
The output I see...
0
 
Marco GasiFreelancerCommented:
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

0
 
UniqueDataAuthor Commented:
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?
0
 
UniqueDataAuthor Commented:
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?
0
 
Ray PaseurCommented:
Here's a strategy that will let you see exactly what is getting submitted.  Use HTML preformatting to make it easy to read, and run the entire request array through var_dump().
http://iconoun.com/demo/temp_uniquedata.php

<?php // demo/temp_uniquedata.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28632534.html
 */
error_reporting(E_ALL);

if ( !empty($_POST)  )
{
    echo '<pre>';
    var_dump($_POST);
    echo '</pre>';
}
?>

<form method="post" name="newActivities">
	<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>

Open in new window

0
 
UniqueDataAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.