Page 1 of 2

PHP Form with "Add Item", need help...

Posted: Thu Jul 23, 2009 5:42 pm
by WithHisStripes
Heya,
So I'm working on this form: http://www.pro-ad-co.theportlandco.com/ ... t-form.php

You'll notice there is an "Add Item" button - I know how to send that information via email, but once I add a second item, that information isn't sent, and I'm not sure how to accomplish that using PHP. Any thoughts? Thanks!

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 3:28 am
by DaiLaughing
I've always done it by giving the new rows unique names so they come in the POST as separate variables. I believe the correct way is to use arrays but someone else will need to step in there.

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 6:04 am
by jackpf
You need to call them all the same name, with [] afterward.

This will make php put all the data into an array, and you can just loop through it with a foreach loop.

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 11:51 am
by WithHisStripes
I think I understand, and I believe I'm already using the brackets in the way you've mentioned. But here's my code:

Code: Select all

$copy_other_colors = $_REQUEST['copy-other-colors'];
Now I suppose my question now is how do I create a foreach. I don't have much experience writing those, mostly just while loops for pulling information from a database.

Thanks!

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 12:27 pm
by jackpf
Well then it would be something like

Code: Select all

foreach($copy_other_colors as $key => $value)
{
//do whatever with $value: it now contains the value for each $copy_other_color as it loops through
}

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 12:35 pm
by WithHisStripes
Can you explain to me what "key" and "value" are exactly? I understand them somewhat but I'm definitely confused on their meaning and purpose. Thanks!

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 12:48 pm
by jackpf
Ok, take this example:

Code: Select all

 
$array = array(
'this_is_the_key' => 'this_is_the_value'
);
 
foreach($array as $key => $value)
{
echo 'Key: '.$key.'; Value: '.$value;
}
 

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 12:59 pm
by WithHisStripes
Ah okay that makes sense now.

Thank you very much! I will give that a try right now. Thanks!

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 1:06 pm
by WithHisStripes
Now, I'm not sure how to implement the foreach:

Code: Select all

 
    $array = array (
        'inches' => $inches,
        'number-of-colors' => $number_of_colors,
        'material' => $material,
        'copy-other-colors' => $copy_other_colors
    );
 
into the $message variable:

Code: Select all

 
$message = "
    Customer Name: " . $customer . "
    Attention: " . $attention . "
    Address: " . $address . "
    City: " . $city . "
    Phone: " . $phone . "
    Fax: " . $fax . "
    Email: " . $email . "
    Inches: " . $inches . "
    Number of Colors: " . $number_of_colors . "
    Material: " . $material . "
    Copy Other Colors: " . $copy_other_colors
;
 

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 1:14 pm
by jackpf
Oh right, well if you're just emailing it then you can just do something like this:

Code: Select all

 
$message .= 'Number of colours: '.implode(', ', $number_of_colors);
 

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 1:31 pm
by WithHisStripes
Okay, so here's what I've got:

Code: Select all

    $customer = $_REQUEST['customer'];
    $attention = $_REQUEST['attention'];
    $address = $_REQUEST['address'];
    $city = $_REQUEST['city'];
    $phone = $_REQUEST['phone'];
    $fax = $_REQUEST['fax'];
    $email = $_REQUEST['email'];
    $inches = $_REQUEST['inches']; 
    $number_of_colors = $_REQUEST['number-of-colors'];
    $material = $_REQUEST['material'];
    $copy_other_colors = $_REQUEST['copy-other-colors'];
 
    $array = array (
        'inches' => $inches,
        'number-of-colors' => $number_of_colors,
        'material' => $material,
        'copy-other-colors' => $copy_other_colors
    );
 
    $message = "
    Customer Name: " . $customer . "
    Attention: " . $attention . "
    Address: " . $address . "
    City: " . $city . "
    Phone: " . $phone . "
    Fax: " . $fax . "
    Email: " . $email . " ";
    $message .= "
    Inches: " . implode(", ", $inches);
    $message .= "
    Number of Colors: " . implode(", ", $number_of_colors);
    $message .= "
    Material: " . implode(", ", $material);
    $message .= "
    Copy/Other Colors: " . implode(", ", $copy_other_colors);
I'm not sure how to pass the variable noted in the array into the implode statement though... ?

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 3:01 pm
by jackpf
Ok, well you don't have to put them into an array; they should already be an array if you've postfixed the name of the input with two square brackets.

You just need implode(', ', $number_of_color);

Take this example:

Code: Select all

 
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
echo '<form method="post">
<input type="text" name="array[]" />
<input type="text" name="array[]" />
<input type="text" name="array[]" />
<input type="text" name="array[]" />
<input type="text" name="array[]" />
<input type="submit" />
</form>';
}
else
{
echo implode(', ', $_POST['array']);
}
 

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 3:22 pm
by WithHisStripes
I'm not sure what I'm missing, I'm still having trouble (thanks a lot for your help by the way).

Code: Select all

 
    $customer = $_REQUEST['customer'];
    $attention = $_REQUEST['attention'];
    $address = $_REQUEST['address'];
    $city = $_REQUEST['city'];
    $phone = $_REQUEST['phone'];
    $fax = $_REQUEST['fax'];
    $email = $_REQUEST['email'];
 
    $message = "
    Customer Name: " . $customer . "
    Attention: " . $attention . "
    Address: " . $address . "
    City: " . $city . "
    Phone: " . $phone . "
    Fax: " . $fax . "
    Email: " . $email . "
    Inches: " . implode(", ", $_POST['inches']) . "
    Number of Colors: " . implode(", ", $_POST['number_of_colors']) . "
    Material: " . implode(", ", $_POST['material']) . "
    Copy/Other Colors: " . implode(", ", $_POST['copy_other_colors']);
 
    mail( "spencerhill@theportlandco.com", "Someone has requested a quote.", $message, "From: $email" );
    echo "<p style='width: inherit;'>Thank you, we'll be in touch with you shortly.</p>";
} else {
 

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 3:26 pm
by jackpf
Looks like you're missing a bracket on line 20.

You should really turn on error reporting on your development box.

Re: PHP Form with "Add Item", need help...

Posted: Fri Jul 24, 2009 3:27 pm
by WithHisStripes
Sorry - I wasn't referring to that error - I just updated that code. I was just referring to how I'm implementing the array.