Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Need Help with PHP code for using an array checkbox

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #26031
    bexterinni
    Participant

    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:
    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

    #63780
    holyhttp
    Member

    $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.

    #62158
    bexterinni
    Participant

    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:
    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 ;
    }
    ?>

    #63953
    bexterinni
    Participant

    How do I do that?

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.