SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP form validation for multiple radio btns

    Hello,

    I'm new to PHP and am having problems getting PHP to validate a HTML form that contains multiple radio buttons and text fields.

    Basically, the form has 7 sections, each pertaining to an MP3 track. In each section the user has to select one of four radio buttons (poor, average, good, excellent) and also enter comments in a text field. Note that the user doesn't have to enter feedback on EVERY section (track). Even if he/she chooses ONE radio button on ONE track (with no comment), that is acceptable.

    In a nutshell, the PHP file has to determine whether ONE or MORE radio button/s are selected, or NONE at all. This then prompts it to confirm that the form is OK (and then processes it), or give an error message. Below is a sample of the HTML code for the radio buttons, and the PHP code I'm currently using which doesn't work:

    HTML:
    <input type="radio" name="Track1" value="poor">
    <input type="radio" name="Track1" value="average">
    <input type="radio" name="Track1" value="good">
    <input type="radio" name="Track1" value="excellent">

    PHP:
    <?php
    $to = 'myemail(AT)domain.com';
    $Track1 = $_POST['Track1'];
    $Track1Cmnt = $_POST['Track1Cmnt'];
    $Track2 = $_POST['Track2'];
    $Track2Cmnt = $_POST['Track2Cmnt'];
    $Track3 = $_POST['Track3'];
    $Track3Cmnt = $_POST['Track3Cmnt'];
    $Track4 = $_POST['Track4'];
    $Track4Cmnt = $_POST['Track4Cmnt'];
    $Track5 = $_POST['Track5'];
    $Track5Cmnt = $_POST['Track5Cmnt'];
    $Track6 = $_POST['Track6'];
    $Track6Cmnt = $_POST['Track6Cmnt'];
    $Track7 = $_POST['Track7'];
    $Track7Cmnt = $_POST['Track7Cmnt'];
    if (empty($Track1) || empty($Track1Cmnt) || empty($Track2) || empty($Track2Cmnt) || empty($Track3) || empty($Track3Cmnt) || empty($Track4) || empty($Track4Cmnt) || empty($Track5) || empty($Track5Cmnt) || empty($Track6) || empty($Track6Cmnt) || empty($Track7) || empty($Track7Cmnt)) {
    echo "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Submission error. Please enter some user input before pressing 'Submit'.</font>";
    echo "<br><br><a href='javascript:history.back(1);'><font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Back to main page</font></a>";
    }
    else {
    echo "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Thank you for sending your opinion/s on the selected track/s.</font>";
    echo "<br><br><br><br><a href='WEBPAGE' target='_parent'><font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Back to main page</font></a>";
    mail($to, $subject, $msg");
    }
    ?>

    I have a feeling that the processing part at the end (mail($to, etc.)) is wrong as well... any tips and advice will be greatly appreciated. Thanks in advance for your help!

    Toby

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want one or more items selected use checkboxes, or make sure that the radio input element has a different name. Giving the raidio input element the same name groups them and only one item in that group can be selected.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!


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
  •