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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
gr8gonzoConsultantCommented:
I also noticed you have duplicate input names, so the latter ones would overwrite the former.
0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.