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

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

WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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!
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

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

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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
}
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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;
}
 
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post by WithHisStripes »

Ah okay that makes sense now.

Thank you very much! I will give that a try right now. Thanks!
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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
;
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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);
 
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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... ?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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']);
}
 
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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 {
 
Last edited by WithHisStripes on Fri Jul 24, 2009 3:26 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

Looks like you're missing a bracket on line 20.

You should really turn on error reporting on your development box.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

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

Post 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.
Post Reply