Forum Moderators: coopster

Message Too Old, No Replies

Help with my form - passing array to another php script

         

chrisneedham

9:25 am on Jul 12, 2007 (gmt 0)

10+ Year Member



I have been setting up a shopping basket and am having difficulties with the php scripting of one of the forms.
Basically I have a shop site that people can book on a specific event/s - once they book one or more events the form request them to enter the name of the person attending each event.
What i am struggling with is passing the inputed information on to another php page (i.e. preview page).

snippet of the script for the form is:


<form action="<?php echo $_SERVER['PHP_SELF'];?>?step=2" method="post" name="frmCheckout" id="frmCheckout" onSubmit="return checkShippingAndPaymentInfo();">
<div align="left">
<blockquote>
<?php
$cartContent = getCartContent();
$numItem = count($cartContent);
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
extract($cartContent[$i]);
$subTotal += $pd_price * $ct_qty;
?>
<table width="607" border="0" cellpadding="5" cellspacing="1" class="entryTable">
<tr class="entryTableHeader">
<td colspan="2">
<span style="font-weight: 700">
<font face="Verdana" style="font-size: 9pt" color="#006600">
<?php echo $pd_code;?> - <?php echo $pd_coursedate;?> - Delegate Details</font></span><font face="Verdana" style="font-size: 9pt; font-weight: 700" color="#006600"></font></td>
</tr>
<?php
for ($h = 0; $h < $ct_qty; $h++)
{
?>
<tr>
<td class="content" width="227"><font face="Verdana">
<span style="font-size: 9pt">
Delegate Name<br>
<input name="delname[]" type="text" class="box" id="delname" size="31" maxlength="50" style="border: 1px solid #006600"></span></font></td>
<input name="hidprodid" type="hidden" id="hidprodid" value="<?php echo $pd_id;?>">
</td>
<td class="content" width="171"><font face="Verdana">
<span style="font-size: 9pt">
Delegate Date of Birth<br>
<input name="deldob[]" type="text" class="box" id="deldob" size="22" maxlength="50" style="border: 1px solid #006600"></span></font></td>
</td>
<td class="content" width="176">
<font face="Verdana" style="font-size: 8pt">Previously
attended a City &amp; Guilds course with OTT?<br>
<select size="1" name="delbefore[]" style="border: 1px solid #006600">
<option>No</option>
<option>Yes</option>
</select></font></td>
</tr>

<?php
}
?>
</table>
<?php
}
?>

This code has a for loop which looks for how many events have been booked and displays each detail. Within this for loop is a nested for loop which then looks for how many places have been booked on the event and displays a text input box for each place on each course.

So what i need to do on the preview page is show the event the person has booked and show the name of the person attending the specific event. For example if someone has booked 3 events with 2 people attending each event - it should have:

Event Name: 1
Name of attendee: Bob
Name of attendee: Fred

Event Name: 2
Name of attendee: Chris
Name of attendee: John

etc etc

Anyone have a simple way of doing this - sorry if this doesn't make much sense but I am a beginner when it comes to php etc

Thanks in advance

tomda

9:43 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo your $_post variables to see what's going on...
print_r($_POST);
this you return an array

Personnally, I would change the name of the input so that array returned is indexed by $p_id

<input name="hidprodid" type="hidden" id="hidprodid" value="<?php echo $pd_id;?>">

<input name="<?php echo $pd_id;?>[]" type="text" class="box" id="delname" size="31" maxlength="50" style="border: 1px solid #006600">

<input name="<?php echo $pd_id;?>[]" type="text" class="box" id="deldob" size="22" maxlength="50" style="border: 1px solid #006600">

<select size="1" name="<?php echo $pd_id;?>[]" id="delbefore" style="border: 1px solid #006600">

By doing this, you should get something like
array([0]=>array([0]=>1,[1]=>Bob,[2]=>10/05/1962,[3]=>Yes), [1]=>array([0]=>1,[1]=>Fred,[2]=>22/07/1982,[3]=>No), etc...

chrisneedham

10:31 am on Jul 12, 2007 (gmt 0)

10+ Year Member



Thanks - I tried your suggestion and echoed the variables and got the following:


Array ( [hidprodid] => 35 [45] => Array ( [0] => Bob [1] => 10/05/1962 [2] => Yes ) [35] => Array ( [0] => Fred [1] => 22/07/1982 [2] => No )

What i need to be able to do is to create a display table that shows the pd_id of the event and loop through the names/dob etc that are assigned to that pd_id (i.e names filled in the text box below that event tite) and display the name date of birth and previously attended in a rows. to get something like this:

45: PHP Workshop Event on 12th July 2007
Name Date of Birth Previous attend event
Bob 10/05/62 Yes

35: PHP Workshop Event on 13th July 2007
Name Date of Birth Previous attend event
Fred 22/07/1982 No

Do you see where I am going? any help or suggestions to do this a different way would be very gratefully received!

Cheers again

chrisneedham

10:34 am on Jul 12, 2007 (gmt 0)

10+ Year Member



for reference this is part of the code from the "preview booking" page that the booker is sent to after submitting the form.


<?php
$numItem = count($cartContent);
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
extract($cartContent[$i]);
?>
<table width="550" border="0" cellpadding="5" cellspacing="1" class="infoTable">
<tr>
<td colspan="4"><span style="font-weight: 700">
<font face="Verdana" style="font-size: 9pt" color="#006600">
<?php echo $pd_id;?> - <?php echo $pd_coursedate;?> - Delegate</font></span><font face="Verdana" style="font-size: 9pt; font-weight: 700" color="#006600">
Details</font></td>
</tr>
</table>
<table width="550" border="0" cellpadding="5" cellspacing="1" class="infoTable">
<tr>
<td colspan="2" bgcolor="#E2E2E2">
<font face="Verdana" style="font-size: 9pt; ">Name</font></td>
<td bgcolor="#E2E2E2" width="146">
<font face="Verdana" style="font-size: 9pt; ">Date of Birth</font></td>
<td width="149" bgcolor="#E2E2E2">
<font face="Verdana" style="font-size: 9pt; ">Previous C&amp;G with OTT</font></td>
</tr>

So i need to display what i mentioned in above post in this section of the code.

Hope it makes sense

Chris

tomda

10:52 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Starting from the array you get
Array ( [hidprodid] => 35 [45] => Array ( [0] => Bob [1] => 10/05/1962 [2] => Yes ) [35] => Array ( [0] => Fred [1] => 22/07/1982 [2] => No ) )

A/ Remove the first element
unset($_POST['hidprodid']);
to get
Array ([45] => Array ( [0] => Bob [1] => 10/05/1962 [2] => Yes ) [35] => Array ( [0] => Fred [1] => 22/07/1982 [2] => No ))

B/ Then, just loop through the array

foreach($_POST as $element) {
echo '<tr><td colspan="2">'.$element[0].'</td>
<td width="146">'.$element[1].'</td>
<td width="149">'.$element[2].'</td></tr>';
}

Done on the fly, so not tested...

chrisneedham

12:37 pm on Jul 12, 2007 (gmt 0)

10+ Year Member



Ok still having problems.

Now getting this error message for each element it repeats...:

Notice: Uninitialized string offset: 0 in /home/ottltd/public_html/booking_forms/include/checkoutConfirmation.php on line 132

And i'm still getting both attendees listed in both events rather than one in one event and one in the other. Also getting some a random letter listed in each row too!?

This is where the code is now:


<?php
$numItem = count($cartContent);
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
extract($cartContent[$i]);
?>
<table width="550" border="0" cellpadding="5" cellspacing="1" class="infoTable">
<tr>
<td colspan="4"><span style="font-weight: 700">
<font face="Verdana" style="font-size: 9pt" color="#006600">
<?php echo $pd_id;?> - <?php echo $pd_coursedate;?> - Delegate</font></span><font face="Verdana" style="font-size: 9pt; font-weight: 700" color="#006600">
Details</font></td>
</tr>
</table>
<table width="550" border="0" cellpadding="5" cellspacing="1" class="infoTable">
<tr>
<td colspan="2" bgcolor="#E2E2E2">
<font face="Verdana" style="font-size: 9pt; ">Name</font></td>
<td bgcolor="#E2E2E2" width="146">
<font face="Verdana" style="font-size: 9pt; ">Date of Birth</font></td>
<td width="149" bgcolor="#E2E2E2">
<font face="Verdana" style="font-size: 9pt; ">Previous C&amp;G with OTT</font></td>
</tr>
<?php
foreach($_POST as $element) {
echo '<tr><td colspan="2">'.$element[0].'</td>
<td width="146">'.$element[1].'</td>
<td width="149">'.$element[2].'</td></tr>';
}
?>

</table>
<?php
}
?>

I'm obviously doing something wrong or going about things in the wrong way.

tomda

1:04 pm on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, my computer is down so I can't help you now.
I hope it will fix tomorrow so that I can give you a nice loop.

Anyway, you forgot to unset the first element of the array.

<?php
// print_r ($_POST);
unset($_POST['hidprodid']);
// print_r ($_POST);
foreach($_POST as $element) {
echo '<tr><td colspan="2">'.$element[0].'</td>
<td width="146">'.$element[1].'</td>
<td width="149">'.$element[2].'</td></tr>';
}
?>

chrisneedham

3:12 pm on Jul 12, 2007 (gmt 0)

10+ Year Member



Ok thanks for your help upto know anyway.
Just to let you know that i have put the unset code in but makes no difference. still getting the errors.

chrisneedham

1:04 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



Sorted it :)

<?php

//print_r ($_POST);
unset($_POST['hidprodid']);
//print_r ($_POST);
foreach ($_POST as $key =>$element){
if ($key == $pd_id){
echo '<tr><td colspan="2" bgcolor="#FFFFFF">'.$element[0].'</td>
<td width="146" bgcolor="#FFFFFF">'.$element[1].'</td>
<td width="149" bgcolor="#FFFFFF">'.$element[2].'</td></tr>';
echo '<tr><td colspan="2" bgcolor="#FFFFFF">'.$element[3].'</td>
<td width="146" bgcolor="#FFFFFF">'.$element[4].'</td>
<td width="149" bgcolor="#FFFFFF">'.$element[5].'</td></tr>';
}
}
?>

Thanks for getting me to the right place tho :)

chrisneedham

2:56 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



My next job is to get the array into a database table.

So up to know I have managed to get the users details into an array and the print the array on the next page. The user then submits the form to the database (i.e. submitting the order).

So the code that prints the array is on previous post. The following code is what i am already sending to the table:


if ($orderId) {
// save order items
for ($i = 0; $i < $numItem; $i++) {
$sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty)
VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})";
$result = dbQuery($sql);
}

So i now have 3 extra colums in the table (delname, deldob, delbefore) - i need to extract the info from the array and insert it into the database. I.E. $element[0] will go to delname, $element[1] will go to deldob and $element[2] will go to delbefore and so on...

Any ideas?