Grow your CSS skills. Land your dream job.

[Solved] Need Help with PHP code for using an array checkbox

  • # September 8, 2009 at 9:43 am

    I know almost nothing about PHP. A friend helped me to customize an existing submit.php file for my website to use to submit a HTML form ("Contact Us") via email. The form submits fine and comes to me via email, and is formatted mostly fine except for the checkbox arrays. Instead of listing each of the items that the user checked, it simply lists "Array."

    The HTML portion of the form containing the checkboxes looks like this:

    Code:

    Which room(s) in your home is your project for? (Check all that apply.)


    Kitchen Floor  


    Kitchen Backsplash


    Sunroom


    Master Bath (wall or floor)


    Hall Bath (wall or floor)&


    Powder Room


    Laundry Room


    Other

    I know that my .php is missing the appropriate code for the array, but have no idea how to fix it. I *think* I need some kind of "foreach" code but don’t know the proper way to write it, or where to put it.

    Here’s my .php code:

    Code:
    < ?php
    if (!isset($_POST) && isset($_POST)) {
    $_POST = $_POST;
    }

    if (isset($_POST["submit"]) && ($_POST["submit"]=="Submit")
    && isset($_POST['subject']) && empty($_POST['subject'])) {

    $sendto = "my.email@email.com";
    $subject = "Inquiry from Website: Please Contact Me";
    $successurl = "http://www.mywebsite.com/submit_success.html";
    $SpamReplaceText = "*content removed*";
    $SpamErrorMessage = "

    It appears you entered an invalid value

    Click here to go back.

    “;

    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $email = $_POST['email_address'];
    $phone = $_POST['phone_number'];
    $contact = $_POST['contact'];
    $times = $_POST['times'];
    $room = $_POST['room'];
    $otherroom = $_POST['other_room'];
    $project = $_POST['project'];
    $squarefootage = $_POST['square_footage'];
    $timing = $_POST['timing'];
    $anythingelse = $_POST['anything_else'];

    $headers = “From: $emailn”;
    $headers . “MIME-Version: 1.0n”
    . “Content-Transfer-Encoding: 7bitn”
    . “Content-type: text/html; charset = “iso-8859-1″;nn”;

    // Build the email body text
    $emailcontent = ”
    —————————————————————————–
    $subject
    —————————————————————————–

    Customer’s Name: $firstname $lastname

    Customer’s Email Address: $email

    Customer’s Phone Number: $phone

    Customer prefers to be contacted via: $contact

    Best time to call the Customer: $times

    Room(s) that the project is for: $room

    Room not in the list: $otherroom

    Is this project new construction, a remodel, or a room update? $project

    Approximate square footage of the project: $squarefootage

    When the project will start: $timing

    Is there anything else we should know about your project? $anythingelse
    “;
    // Check that the email address entered matches the standard email address format
    if (!eregi(“^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,6}$”, $email)) {
    echo “

    It appears you entered an invalid email address

    Click here to go back.

    “;
    }

    elseif (!trim($firstname)) {
    echo “

    Please go back and enter your First Name

    Click here to go back.

    “;
    }

    elseif (!trim($lastname)) {
    echo “

    Please go back and enter your Last Name

    Click here to go back.

    “;
    }

    elseif (!trim($email)) {
    echo “

    Please go back and enter your Email Address

    Click here to go back.

    “;
    }
    elseif (!trim($phone)) {
    echo “

    Please go back and enter your Phone Number

    Click here to go back.

    “;
    }

    // Sends out the email or will output the error message
    elseif (mail($sendto, $subject, $emailcontent, $headers)) {
    header( “Location: $successurl” );
    exit ;
    }
    }
    else {
    header( “Location: $successurl” );
    exit ;
    }
    ?>

    If anyone can give me very clear instruction how to fix my .php form, I’d greatly appreciate it.

    ~bexterinni

    # September 10, 2009 at 9:48 pm

    $rooms=$_POST;
    $roomchoice=”;
    foreach($rooms as $key=>$value){
    if($roomchoice!=”){ $roomchoice.=’, ‘; }
    $roomchoice.=$value;
    }

    That will display all the selected values in a string separated by a comma.

    # September 11, 2009 at 8:21 am

    Thank you holyhttp. Unfortunately, that code also returned "Array" on my email.

    The friend who was helping me with the original PHP code was able to write the "foreach" code into the PHP. It is now working and displaying correctly when it’s returned via email.

    Here is the corrected code that works for my site:

    Code:
    < ?php

    if (!isset($HTTP_POST_VARS) && isset($_POST)) {
    $HTTP_POST_VARS = $_POST;
    }

    if (isset($HTTP_POST_VARS["submit"]) && ($HTTP_POST_VARS["submit"]=="Submit")
    && isset($HTTP_POST_VARS['subject']) && empty($HTTP_POST_VARS['subject'])) {

    $sendto = "my.email@email.com";
    $subject = "Inquiry from Website: Please Contact Me";
    $successurl = "http://www.mywebsite.com/submit_success.html";
    $SpamReplaceText = "*content removed*";
    $SpamErrorMessage = "

    It appears you entered an invalid value

    Click here to go back.

    “;

    $firstname = $HTTP_POST_VARS['first_name'];
    $lastname = $HTTP_POST_VARS['last_name'];
    $email = $HTTP_POST_VARS['email_address'];
    $phone = $HTTP_POST_VARS['phone_number'];
    $contact = $HTTP_POST_VARS['contact'];
    $times = $HTTP_POST_VARS['times'];
    $room = $HTTP_POST_VARS['room'];
    $otherroom = $HTTP_POST_VARS['other_room'];
    $project = $HTTP_POST_VARS['project'];
    $squarefootage = $HTTP_POST_VARS['square_footage'];
    $timing = $HTTP_POST_VARS['timing'];
    $anythingelse = $_POST['anything_else'];

    $headers = “From: $emailn”;
    $headers . “MIME-Version: 1.0n”
    . “Content-Transfer-Encoding: 7bitn”
    . “Content-type: text/html; charset = “iso-8859-1″;nn”;

    // Build the email body text
    $emailcontent = ”
    —————————————————————————–
    $subject
    —————————————————————————–

    Customer’s Name: $firstname $lastname

    Customer’s Email Address: $email

    Customer’s Phone Number: $phone

    Customer prefers to be contacted via: $contact

    Best time to call the Customer: $times

    Room(s) that the project is for: “;

    foreach($room as $value) { $emailcontent .= “$value, “; }

    $emailcontent .= ”

    Room not in the list: $otherroom

    Is this project new construction, a remodel, or a room update? “;

    foreach($project as $value) { $emailcontent .= “$value, “; }

    $emailcontent .= ”

    Approximate square footage of the project: $squarefootage

    When the project will start: $timing

    Is there anything else we should know about your project? $anythingelse
    “;

    // Check that the email address entered matches the standard email address format
    if (!eregi(“^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,6}$”, $email)) {
    echo “

    It appears you entered an invalid email address

    Click here to go back.

    “;
    }

    elseif (!trim($firstname)) {
    echo “

    Please go back and enter your First Name

    Click here to go back.

    “;
    }

    elseif (!trim($lastname)) {
    echo “

    Please go back and enter your Last Name

    Click here to go back.

    “;
    }

    elseif (!trim($email)) {
    echo “

    Please go back and enter your Email Address

    Click here to go back.

    “;
    }
    elseif (!trim($phone)) {
    echo “

    Please go back and enter your Phone Number

    Click here to go back.

    “;
    }

    // Sends out the email or will output the error message
    elseif (mail($sendto, $subject, $emailcontent, $headers)) {
    header( “Location: $successurl” );
    exit ;
    }
    }
    else {
    header( “Location: $successurl” );
    exit ;
    }
    ?>

    # September 13, 2009 at 7:04 am

    if this is solved, please amend the title

    # September 13, 2009 at 7:08 am

    How do I do that?

    # September 13, 2009 at 7:24 am

    its done, don’t worry.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".