Hi, i have a form with checkbox, textfield, and radio button.

But i duno how to do validation on that form, for example, i would like to make sure user have fill in certain checkbox, textfield and radio button before submit form. if not, a error message will prompt up.

I have a list a checkbox, let's say i wanna ask which model of car that user want, that user must fill in at least one checkbox, but how to make sure user have fill in at least 1 checkbox already before he/she can submit the form?

thank you for your help. 🙂

    The use of a checkbox is not correct here (if I understand you correctly), you need to use a radio group. The idea of a checkbox means you can choose to tick it or not. A radio group should have one option chosen.

    so when you post your page you could do something like:

    if (isset($_POST['submitbutton'])) {
    	$mychoice 	= $_POST['radiogroup'];
    
    if (empty($mychoice)) {
    	echo "You haven't chosen anything!!!";
    }
    }
    

    This is a very simple and basic approach of course but it will get you started.

      Hi, thanks for your explanation, but i was thinking to use checkbox is because for example the user may select 2 different model car in same time, if i use radio button, they only can select one car right?

      Thank you 🙂

        Ok, so your planning on showing an error message for each checkbox that is not selected?? Doesn't really make sense, but the way to detect if your checkboxes are ticked or not would be achieved via the following method:

        if (empty($_POST['checkbox1'])) {
        	$checkbox1val = 0;
        } else {
        	$checkbox1val = 1;
        }
        
        if (empty($_POST['checkbox2'])) {
        	$checkbox2val = 0;
        } else {
        	$checkbox2val = 1;
        }
        
        

        Just remember to rename the POST var to whatever you have called your checkboxes. If all POST vars are empty then you display your error message.

        Good luck.

          You could name all the checkboxes on your page the same using array syntax, i.e.

          <input type="checkbox" name="cartype[]" value="unique_value"/>
          

          Now in your PHP code, each box ticked will be part of the $_REQUEST['cartype'] array. If this has no elements, then the user did not tick any boxes:

          if(count($_REQUEST['cartype']) == 0)
          {
              // display error
          }
          else
          {
              for($i=0; $i<count($_REQUEST['cartype']); $i++)
              {
                   // each car type is in this loop as $_REQUEST['cartype'][$i]
              }
          }
          

          That should fix it.

            Hi, thank you kbc1 and Ashley, i will try to change the code inside my form and try it.

            Recently my external hard disk is corrupted, mainly is because i didn't click safely remove hardware before unplug, so i m thinking hw to get my data bck 1st.

            Thank you. 🙂

              Write a Reply...