phppup Posted November 19, 2020 Share Posted November 19, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311736-find-inputs-that-are-not-selected/ Share on other sites More sharing options...
mac_gyver Posted November 19, 2020 Share Posted November 19, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311736-find-inputs-that-are-not-selected/#findComment-1582496 Share on other sites More sharing options...
Barand Posted November 19, 2020 Share Posted November 19, 2020 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/311736-find-inputs-that-are-not-selected/#findComment-1582497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.