I have a combo box with 6 options

Choose an option
1
2
3
4
5

and I want the user to rate it from 1 to 5, but when user selects option "Choose an option" I want an error message to be displayed saying that you have to select from 1 to 5. How do I force the user to select these options 1 to 5?

    you can javascript or php itself, here just an example

    <form action="act.php">
    <select name="s_name">
    <option value="-1"> Choose an option </option>
    <option value="1"> 1 </option>
    <option value="2"> 2 </option>
    <option value="3"> 3 </option>
    <option value="4"> 4 </option>
    <option value="5"> 5 </option>
    <input type="submit" name="btn_submit" value="go">
    </form>
    

    then on act.php

    if (isset($_POST['btn_submit']) && ($_POST['btn_submit']))
    {
         if ($_POST['s_name']=="-1")
        { 
                echo "please choose the option";
                exit();
        }     
    else { // another action } }

    hope that help.

      It helped thanks. Now its working the way I wanted.

        //javascript for validation ,put in header
        <script language="javascript">
        function validateSelect(form1)
        {
        if(form1.s_name.options[0].selected)
        {
        alert("Please select your rate!");
        form1.s_name.focus();
        return 0;
        }

        SubmitIt(form1)
        return 1;
        }
        </script>

        // code in body
        <form name="form1" action="act.php" method="post" onSubmit="if(!validateSelect(this)) return false">
        <select name="s_name">
        <option selected="selected" value="-">Choose an option </option>
        <option value="1"> 1 </option>
        <option value="2"> 2 </option>
        <option value="3"> 3 </option>
        <option value="4"> 4 </option>
        <option value="5"> 5 </option>
        <input type="submit" name="btn_submit" value="go">
        </form>

        Codes above use javascript for validation,if 1-5 is selected,then will proceed to another/same file based on the php file you stated in action of form. Else, cursor will focus on the field to force user to choose a rate!
        cheers😛

          boris_ph wrote:

          Codes above use javascript for validation,if 1-5 is selected,then will proceed to another/same file based on the php file you stated in action of form. Else, cursor will focus on the field to force user to choose a rate!
          cheers😛

          And if I stop JavaScript from my browser, I can submit without ratting ...

            Write a Reply...