HELP NEEDED - Posting array values from a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

HELP NEEDED - Posting array values from a form

Post by RobertGonzalez »

Can someone help me? I am sure I am overlooking something simple, but it is eluding me right now. I have a script that captures vehicle information and places it into a form for updating car wash mileage and car wash dates. On submit, I want the script to process each post and enter the data into the DB. I am testing the script with the PHP functions LIST and EACH. Here is the HTML form (shortened for this forum):

Code: Select all

<form method="POST" action="/admin/cp_carwash.php">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td height="25" class="cat"><span class="cattitle">Plate</span></td>
		<td height="25" class="cat"><span class="cattitle">Vehicle</span></td>
		<td height="25" class="cat"><span class="cattitle">Wash Date</span></td>
		<td height="25" class="cat"><span class="cattitle">Mileage</span></td>
	</tr>
	<tr>
		<td><span class="gensmall">5088-01B</span></td>
		<td><span class="gensmall">5TEGN92N03Z187273 - 2003 Toyota Tacoma PreRunner Dbl Cab V6</span></td>
		<td><input type="text" size="12" name="wash_dateї114]" value="6/9/2004" /></td>
		<td><input type="text" size="12" name="wash_milesї114]" value="9230" /></td>
	</tr>
	<tr>
		<td class="row1"><span class="gensmall">5088-02B</span></td>
		<td class="row1"><span class="gensmall">5TEVL52N63Z193651 - 2003 Toyota Tacoma 4X2 Ext Cab Auto</span></td>
		<td class="row1"><input type="text" size="12" name="wash_dateї208]" value="6/2/2004" /></td>
		<td class="row1"><input type="text" size="12" name="wash_milesї208]" value="13280" /></td>
	</tr>
	<tr>
		<td><span class="gensmall">5088-17B</span></td>
		<td><span class="gensmall">5TEHN72N83Z180516 - 2003 Toyota Tacoma 4X4 Dbl Cab Auto V6</span></td>
		<td><input type="text" size="12" name="wash_dateї190]" value="5/26/2004" /></td>
		<td><input type="text" size="12" name="wash_milesї190]" value="7765" /></td>
	</tr>
	<tr>
		<td colspan="4" align="center" valign="middle">
		<input type="hidden" name="submit_form" value="true" />
		<input type="submit" name="send_wash_data" value="Submit Wash Data Changes" />
		</td>
	</tr>
</table>
</form>
Here is the code that processes this submit (in test form to track output):

Code: Select all

<?php
if ( isset($_POST['submit_form']) && $_POST['submit_form'] == 'true')
{
	// how about we make arrays out of them first
	$wd_array = $_POST['wash_date'];
	
	// Here comes the data
	// dates first
	$message = 'Preparing the insert...';
	if (is_array($wd_array))
	{
		$message .= '<br />wash_date is an array...';
			
		while (list($car_id, $wash_date) = each($_POST['wash_date']))
		{
			if ($wash_date == '')
			{
				$wash_date = 0;
			}
			else 
			{
				strtotime($wash_date);
			}
			
			$message .= '<br />Vehicle: ' . $car_id . ' was updated to ' . $wash_date . '...';
		}
		$message .= '<br /><br />Now for the mileage...';
		while (list($car_id, $wash_miles) = each($_POST['wash_miles']))
		{
			$message .= '<br />Vehicle: ' . $car_id . ' was updated to mileage ' . $wash_miles . '...';
		}
	}
	else 
	{
		$message .= '<br />Wash_date is not an array, here is: ' . $_POST['wash_date'];
	}
	
	$message .= '<br />post_wash_date = ' . $_POST['wash_date'];
	$message .= '<br />post_wash_miles = ' . $_POST['wash_miles'];
}
?>
$message is only used to track var values passed to the script. When the form submits, I get an a error that says $_POST['wash_date'] is not an array. But when I print $_POST['wash_date'] to the screen, the value shows up as Array. The same holds true for $_POST['wash_miles']. Can someone please help out with this one. I am stumped.

Thanks in advance for all your help.
talorlik
Forum Newbie
Posts: 6
Joined: Tue Jul 13, 2004 3:54 am

dont know if will help but doesnt hurt to try

Post by talorlik »

try the following, normaly when fields on the template share the same name PHP recognises it as array i know i havent had a problem doing something very similar to what you are.

Code: Select all

<?php

     
   foreach($_POST["wash_date"]) as $key => $value)
      {
         if ($value == '')
          $value = 0;
         else
           strtotime($value);
                  
         $message .= '<br />Vehicle: ' . $key . ' was updated to ' . $value . '...';
      }         

?>
good luck
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I tried it, but I didn't think it would work. PHP for some reason is not seeing that POST value as an array. Because of that, you can't pass $POST into a function expecting an array or you'll get an error. Any other ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

doing some testing:

Code: Select all

<?php

if($_SERVER['REQUEST_METHOD'] != 'POST')
{
echo <<<STOP
<html><body><form method="POST" action="{$_SERVER['SCRIPT_NAME']}">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
   <tr>
      <td height="25" class="cat"><span class="cattitle">Plate</span></td>
      <td height="25" class="cat"><span class="cattitle">Vehicle</span></td>
      <td height="25" class="cat"><span class="cattitle">Wash Date</span></td>
      <td height="25" class="cat"><span class="cattitle">Mileage</span></td>
   </tr>
   <tr>
      <td><span class="gensmall">5088-01B</span></td>
      <td><span class="gensmall">5TEGN92N03Z187273 - 2003 Toyota Tacoma PreRunner Dbl Cab V6</span></td>
      <td><input type="text" size="12" name="wash_date[114]" value="6/9/2004" /></td>
      <td><input type="text" size="12" name="wash_miles[114]" value="9230" /></td>
   </tr>
   <tr>
      <td class="row1"><span class="gensmall">5088-02B</span></td>
      <td class="row1"><span class="gensmall">5TEVL52N63Z193651 - 2003 Toyota Tacoma 4X2 Ext Cab Auto</span></td>
      <td class="row1"><input type="text" size="12" name="wash_date[208]" value="6/2/2004" /></td>
      <td class="row1"><input type="text" size="12" name="wash_miles[208]" value="13280" /></td>
   </tr>
   <tr>
      <td><span class="gensmall">5088-17B</span></td>
      <td><span class="gensmall">5TEHN72N83Z180516 - 2003 Toyota Tacoma 4X4 Dbl Cab Auto V6</span></td>
      <td><input type="text" size="12" name="wash_date[190]" value="5/26/2004" /></td>
      <td><input type="text" size="12" name="wash_miles[190]" value="7765" /></td>
   </tr>
   <tr>
      <td colspan="4" align="center" valign="middle">
      <input type="hidden" name="submit_form" value="true" />
      <input type="submit" name="send_wash_data" value="Submit Wash Data Changes" />
      </td>
   </tr>
</table>
</form></body></html>
STOP;
}
else
{
	print_r(array('_POST'=>$_POST));
}

?>
outputs when submitted

Code: Select all

Array
(
    &#1111;_POST] =&gt; Array
        (
            &#1111;wash_date] =&gt; Array
                (
                    &#1111;114] =&gt; 6/9/2004
                    &#1111;208] =&gt; 6/2/2004
                    &#1111;190] =&gt; 5/26/2004
                )

            &#1111;wash_miles] =&gt; Array
                (
                    &#1111;114] =&gt; 9230
                    &#1111;208] =&gt; 13280
                    &#1111;190] =&gt; 7765
                )

            &#1111;submit_form] =&gt; true
            &#1111;send_wash_data] =&gt; Submit Wash Data Changes
        )

)
so it works for me.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There is something strange in some code I am including somewhere in the total script (I believe anyway). The same code used in the script I posted - which still does not work in the script - works fine when used as a stand alone process in a different page. As a dirty hack, I bounced the form to another page, ran the sql scripts needed using list - each, then redirect back to the calling page. The entire process works the way it is supposed to... as long as it is not in the same page that call it.

Any ideas what might be causing this condition?
Post Reply