Results 1 to 6 of 6

Thread: PHP & checkboxes (should be easy)

  1. #1
    Join Date
    Apr 2010
    Posts
    58
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default PHP & checkboxes (should be easy)

    So the problem I am having is probably easy to solve, I'm just not great with coding yet. I did some research and I can't find anything that will work with what I have set up. Anyway, I have between 3-6 check boxes an *the check boxes would be "Food"* It is set up as an array in the HTML Form name="Food[]". The problem is that I cannot get the value of the checked box to show up in the email. It will either show up blank "Food: " or it will show up as "Food: Array" I cannot seem to figure out why I can't get the value to show up.

    PHP Code:
    <?php
    $to 
    "email@internet" ;
    $from $_REQUEST['Email'] ;
    $name $_REQUEST['Name'] ;
    $headers "From: $from";
    $subject "NEW SUBJECT";

    $Food $_POST["Food"];

    $fields = array();
    $fields{"Name"} = "Name";
    $fields{"Company"} = "Company";
    $fields{"Email"} = "Email";
    $fields{"Phone"} = "Phone";
    $fields{"$Food"} = "FOOD";
    $fields{"Other"} = "Other";
    $fields{"Description"} = "Description";

    $body "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

    $headers2 "From: email@internet.com";
    $subject2 "Thank you for contacting us";
    $autoreply "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.website.com";

    if(
    $from == '') {print "You have not entered an email, please go back and try again";}
    else {
    if(
    $name == '') {print "You have not entered a name, please go back and try again";}
    else {
    $send mail($to$subject$body$headers);
    $send2 mail($from$subject2$autoreply$headers2);
    if(
    $send)
    {
    header"Location: http://www.website.com" );}
    else
    {print 
    "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; }
    }
    }
    ?>
    Any help would be appreciative.

    Thanks in advance

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    lots of things...

    Don't use $_REQUEST. You're submitting your form via POST, so use $_POST.

    You're not using the correct syntax to assign values to your array. It should look more like this:
    PHP Code:
    $fields = array();
    $fields['Name'] = $_POST['name'];
    //or, if you've already assigned all the POST variables to local variables
    $fields['Name'] = $name
    also, when you get to it, foreach($fields as $a => $b) will be "Name" => "myname" for each of the other values (they're all strings), but it will be "Food" => array() for your food inputs, because $fields['Food'] will look something like this:
    Code:
    $fields['Food'] = array(
       [0] => 'chicken'
       [1] => 'potatos'
       [2] => 'ice cream'
    )
    Deal with $fields['Food'] separately, like so:
    PHP Code:
    foreach($fields['Food'] as $food){
        echo 
    $food// or however you want to format it

    hope that helps.

  3. #3
    Join Date
    Apr 2010
    Posts
    58
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    So I changed a few things being

    PHP Code:
    <?php
    $to 
    "email@internet.com" ;
    $from $_POST['Email'] ;
    $name $_POST['Name'] ;
    $headers "From: $from";
    $subject "NEW SUBJECT";

    $name $_POST['Name'];
    $company $_POST['Company'];
    $email $_POST['Email'];
    $phone $_POST['Phone'];
    $other $_POST['Other'];
    $output $_POST['Output'];
    $description $_POST['Description'];
    foreach(
    $fields['Food'] as $food){
        echo 
    $food// or however you want to format it

    $fields = array();

    $fields['Name'] = $name;
    $fields['Company'] = $company;
    $fields['Email'] = $email;
    $fields['Phone'] = $phone;
    $fields['Food'] = array(
       [
    0] => 'Chicken'
       
    [1] => 'Potatos'
       
    [2] => 'Ice Cream'
    )
    $fields['Other'] = $other;
    $fields['Output'] = $output;
    $fields['Decription'] = $description;



    $body "We have received the following information:\n\n""Name" => "myname\n" "Company" => "Company\n" "Email" => "Email\n" "Company" => "Company\n" "Phone" => "Phone\n" "Service" => "Food\n" "Other" => "Other\n" "Output" => "Output\n" "Description" => "Description"$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
    Getting a syntax error when defining the food array ([1],[2],[3]). also had an error for the $body.

    I may not have made the corrections properly, like I said I am not really great with coding yet.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Okay, step by step:
    PHP Code:
    <?php

    // when defining $food, do it like so:
    $Food $_POST['Food'];

    // that takes the array from your form and puts it in the variable $Food.

    // what you do next depends on how you want to use it.
    // if you want to print it in your confirmation email, it will
    // be easiest to do it separately from the other items:

    $foodText "Food you chose:\n";
    foreach(
    $Food as $k => $v){
       
    $foodText .= 'Food: '.$v."\n";
    }

    //  and then, add it to your email text later.
    //  I don't use sprintf() often, but I'm not sure if you're
    //  applying it correctly or not.
    //  However, the errors are from syntax mistakes when you're 
    //  assigning values to $body:

    $body "We have received the following information:\n\n
       Name: 
    $name\n
       Company: 
    $company\n
       Email: 
    $email\n
       Company: 
    $company\n
       Phone: 
    $phone\n
       Service: 
    $foodText\n
       Other: 
    $other\n
       Output: 
    $output\n
       Description: 
    $description";

    // then you can use your sprintf() function on $body.
    // the $foodText line above shows one way you could include
    // the $Food array (that we processed into a string earlier).

    ?>

  5. The Following User Says Thank You to traq For This Useful Post:

    europe451 (05-10-2010)

  6. #5
    Join Date
    Apr 2010
    Posts
    58
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Thank you very much, I was able to get it to work.

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •