Forms! Force selection of option?

Discussion in 'HTML & Website Design' started by scriptmakingman, Mar 16, 2009.

  1. #1
    Hello Reader!

    Just looking for some quick help. I'm hoping you can help. :)

    So, I'm building this really simple website. Basically one huge form.
    Now here's the question. On the site there is a part that goes like this...

    <form action="example.php">
    <select name="Home Owner">
    <option value="yes">Yes</option>
    <option value="no">No</option>
    </select>
    <input type="button" value="Submit!">
    </form>
    Code (markup):
    Okay, so basically if the user selects the "Yes" option, they will be able to submit the form. No problem.

    But if they select "No", a message will pop up that says something like you must be a Home Owner to submit this form.

    This is what I need to figure out. :confused:

    Ugh....hopefully you can help!

    Thanks,
    Dale
     
    scriptmakingman, Mar 16, 2009 IP
  2. andy10k

    andy10k Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I've done something similar using php and if statements. Instead of sending your data to "example.php" you send it back to the top of the form.

    At the top of the form you have an if statement testing for the value of your check box. If it is Yes then include the code to do whatever you want (I'm guessing your adding to a database or searching for something) and then use a refresh to go to the original page "example.php".

    If the value of the check box is "No" then print a quick error message or bounce them to a different page.

    I think a different way to do things might be to use javascript to test for a yes answer, but I'm not well versed in that.

    Hope that makes sense.
     
    andy10k, Mar 17, 2009 IP
  3. slash197

    slash197 Greenhorn

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    change your form html to this:

    <form action="example.php" name="myForm" onsubmit="return checkForm()">
    <select name="HomeOwner">
    <option value="yes">Yes</option>
    <option value="no">No</option>
    </select>
    <input type="button" value="Submit!">
    </form>

    then write a js function like this:

    function checkForm()
    {
    if (document.myForm.HomeOwner.value="yes") { return true; }
    else {alert("You need to be a home owner"); return false; }
    }
     
    slash197, Mar 17, 2009 IP
  4. scriptmakingman

    scriptmakingman Active Member

    Messages:
    280
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #4
    slash197, thats exactly what I needed! Thanks alot! :)
     
    scriptmakingman, Mar 17, 2009 IP
  5. slash197

    slash197 Greenhorn

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    glad that i could help you
     
    slash197, Mar 18, 2009 IP