SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2001
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I want to validate a form to make sure all field are entered before
    submission and I understand javascript is best way to do this.

    I have discovered how to do this for text input fields, but I need to
    have validation on drop down list, so at the top of my list I can have
    the option "select one" which is not able to be submitted.

    This is the code I have that I thought would have made it work. Any
    suggestions on what I have not done and what I could do to get it to
    work.

    Thanks

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Hide code from non-js browsers
    function validateForm()
    {
    formObj = document.sample;
    if ((formObj.place1text.value == "") ||
    (formObj.place2text.value == "") ||
    (formObj.day.value == ""))
    {
    alert("You have not filled in all the
    fields.");
    return false;
    }
    else
    return true;
    }
    // end hiding -->
    </SCRIPT>

    And parts of the HTML code

    <FORM ACTION="database51.php" NAME="sample" METHOD="POST"
    onSubmit="return validateForm()">

    <P>From:
    <SELECT NAME="place1text">
    <OPTION SELECTED VALUE="1">Select One
    <OPTION VALUE="">---------
    <?php
    while ($places = mysql_fetch_array($place1)) {
    $place1text = $places["Place"];
    echo("<OPTION VALUE='$place1text'>$place1text\n");
    }
    ?>
    </SELECT></P>

    <P>To:
    <SELECT NAME="place2text">
    <OPTION SELECTED VALUE="">Select One
    <OPTION VALUE="">---------
    <?php
    while ($places = mysql_fetch_array($place2)) {
    $place2text = $places["Place"];
    echo("<OPTION VALUE='$place2text'>$place2text\n");
    }
    ?>
    </SELECT></P>
    <P>Day:
    <SELECT NAME="day">
    <OPTION SELECTED VALUE="">Select One
    <OPTION VALUE="">---------
    <OPTION VALUE='Monday'>Monday
    <OPTION VALUE="Tuesday">Tuesday
    <OPTION VALUE="Wednesday">Wednesday
    <OPTION VALUE="Thursday">Thursday
    <OPTION VALUE="Friday">Friday
    <OPTION VALUE="Saturday">Saturday
    <OPTION VALUE="Sunday">Sunday
    </SELECT>
    </FORM>

    Thanks

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would check to see if the first option in the day dropdown list is selected, I am sure you can make it work into your existing code. BTW I noticed you are using PHP, you may want to do some server side validation, users cna turn javascript off

    if (formObj.day[0].selected == true)
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Member
    Join Date
    Jan 2001
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I want it to do is to realise that the first two options "Select One" and "---------" are not there to be submitted but just for show to help the user.

    I thought the javascript way would be the quickest to implement the function.

    I am quite new to MYSQL/PHP (as well as javascript) and although I think I have understood the basics, can you please point in the right direction of some help or a tutorial on validation in PHP when dealing with MYSQL databases.

  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Your JavaScript code looks fine. It checks to make sure that the drop-down is set to an option with a value other than "" before allowing the form to be submitted.

    Are you having any trouble getting it to work, or did you just want someone to give your code a quick once-over?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  5. #5
    SitePoint Member
    Join Date
    Jan 2001
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    As I said I could get the javascript to work but only when I was submitting from a text field. I wanted it to check for the value from a drop down list, but it wouldn't. It just kept accepting the value even if it was set to null (i.e. "").

    I have managed to solve my problem by investigating further the idea of getting PHP to validate my submitted form, and this works, so I am happy.

    However, thanks for taking an interest.

    Keith


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
  •