Jump to content

Find inputs that are NOT selected


Recommended Posts

I have a form with several text fields.

For example (sorry, no code insert option on my phone)

Quote

<form method="post"> Enter value1 :<input type="text" name="str1"><br/> Enter value2 :<input type="text" name="str2"><br/> <input type="submit" >

I would like to scan the INPUTS to determine which fields were left empty.

Using !isset (to the best of my knowledge) would require that I list each input individually.

Is there a PHP alternative that, similar to JavaScript, would allow me to evaluate every INPUT or TEXT field to then list those left empty?

Link to post
Share on other sites

 

23 minutes ago, phppup said:

would require that I list each input individually.

no. the parameters and values in code can come from any expression, such as a literal string, a variable, the return value from another function call, a math/logical operation, ...

you would define a data structure (array or database table) that contains a list of the expected form field names (types, validation tests...) you would then loop over this defining structure to dynamically test/validate the inputs. the field name, which would be in a variable, would be used in place of any hard-coded associative field name in the code.

except for unchecked check-boxes and radio-buttons, all other form field types will be set once the form has been submitted. after you have detected that a post method form has been submitted, you would only use isset() for check-boxes and radio-buttons. for all of the 'always set' field types, you should test if they are equal (or not) to an empty string.

Link to post
Share on other sites

Name your inputs as name='str[1]', name='str[2] etc instead os str1, str2 ...

Then it's a simple loop when you process the posted data

foreach ($_POST['str'] as $i => $value) {
    if (trim($value) == '') {
        echo "$i is blank</br>"; // or whatever you want to do with empty ones
    }
}

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.