treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Contact Form Help!!

  • I was looking the new tutorials for Website Change Request Form, and I try to change a little bit for checkbox area. I want the checkbox area can POST the multi-select array, and I was search online for the solution which I am stuck. For example, if I select the TEST2 & TEST4 how do I make the post script to send out the two result instead of only one result. I am total a newbie.

    Someone please help me out!!

    Thank you!!

    Here is one of the checkbox example:


    <div class=\"rowElem\">
    <div id=\"changeTypeArea\">

    <input type=\"checkbox\" name=\"test\" id=\"c1\" value=\"Test1\" />
    <label for=\"c1\">Test1</label>

    <div class=\"clear\"></div>

    <input type=\"checkbox\" name=\"test\" id=\"c2\" value=\"Test2\" />
    <label for=\"c2\">Test2</label>

    <div class=\"clear\"></div>

    <input type=\"checkbox\" name=\"test\" id=\"c3\" value=\"Test3\" />
    <label for=\"c3\">Test3</label>

    <div class=\"clear\"></div>

    <input type=\"checkbox\" name=\"test\" id=\"c4\" value=\"Test4\" />
    <label for=\"c4\">Test4</label>

    <div class=\"clear\"></div>

    <input type=\"checkbox\" name=\"test\" id=\"c5\" value=\"Test5\" />
    <label for=\"c5\">Test5</label>
    </div>
    </div>


    My post script:


    $message .= array();
    if (isset($_POST[\"test\"])){
    $message .= $_POST['test'] . \"\n\n\";
    }
  • You are having problems because you have called all your checkboxes "test" and thus the value of the variable is being overwritten.

    If you instead name them "test[]" you will receive a PHP array which contains the values of the selected checkboxes. For example, if the user has marked boxes 2 and 3, you will receive the following array:

    $_POST["test"] = Array ( [0] => Test2 [1] => Test3 )