Hello
I have a user input form where I need to ensure that a certain field has been completed before adding the results to the database.
I am checking to see if this field is empty but cannot get the code to work properly;
The field on the form is just

<input type="text" name="MachineId[]" size="3">

then after the rest of the form, a submit button

<input type="submit" name="submit" value="Submit">

the form is processed onto to another files where the data is assessed and added to the database
at the beginning of this code I have added

IF ($_POST['MachineId'] == " ")
{echo "<font face=\"Arial, Helvetica, sans-serif\">Please ensure you have added the <b>Machine Code </b>";

// then the code goes on to verify the user input information 
// and then add it to the database

firstly the code isn't picking up on whether this field is empty

secondly how do I get the code to say, if this field is empty, go back to the form and enter the correct information whilst keeping what the user has already input in the other fields, but if this empty has data in it OK carry on with the rest of the code........

TIA

    Firstly, the obvious error is that you are not checking whether the field is empty.

    Your input name should be MachineId - not MachineId[]

    therefore

    <input type="text" name="MachineId[]" size="3">

    should be

    <input type="text" name="MachineId" size="3">

    Secondly, your test for being empty is testing for a space - not an empty string. I suggest using

    if (trim($_POST['MachineId']) == "")

      thanks

      The reason I have got

      <input type="text" name="MachineId[]" size="3">

      is because it is an array and there are more than one MachineId records in the form and therefore more than one MachineId fields that have to be checked

      I have also tried the

      if ($_POST['MachineId']) == ""

      with there being no space (i.e. "" not " ") and it still doesn't work

        if you define MachineId[] like this, $_POST['MachineId'] will be an array, so it will not equal an empty string (or a space).

        if you used $_POST['MachineId'][0] (providing that your input field was the first one named MachineId[] in the form), you'd probably still want to compare it with an empty string rather than a space as in your code.

          To check that the MachineId field is an array, has elements and that none of the elements are empty.

          if(is_array($_POST['MachineId'])  && count($_POST['MachineId'])>0) {
            $empty=false;
            foreach($_POST['MachineId'] as $value) {
              if($value=='') {
                $empty=true;
                break;
              }
            }
            if($empty) {
              echo('One or more of the elements was empty');
            }
          } else {
            echo('The field was not an array or the array was empty');
          }
          

          HTH
          Bubble

            Ok. I never use arrays in inputs but create dynamic input names as in <input type="text" name="MachineId$a">

            When you get the machine id as an array you will need to loop through the array...

            for($a=0; $a< count($POST['MachineId']); $a++) {
            if (trim($
            POST['MachineId'][$a]) == "") {
            // code here
            }
            }

            Hope this helps...

              thank you everyone

              Bubblenuts suggestion worked when I changed the Else clause to

              else{if (!$empty)
              { // the rest of my code to check and input the data into mysql
              }
              }
              

                You might also want to look at using [man]empty[/man].

                  Write a Reply...