Phphineas Posted November 3, 2022 Share Posted November 3, 2022 I have some very simple code in a PHP web application that is something like this: <select name="my_select" id="my_select" value=""> <option value=""></option> <option value="1">Pass</option> <option value="0">Fail</option> </select> if (empty($_POST['myselect'])) { // if no select value is chosen, field value = current value $field_value = $_REQUEST['field_value']; } else { // else set field value to select value $field_value = $_POST['select_value']; } $sql = "update my table set field_value = :field_value"; If the user chooses a value from the select list, that value is used to update a table. If the select value is empty, the field value stays as is. A button triggers the update query. This has been working fine until today. Now, if the user selects a value, the field is updated to 1 / Pass. But if the button is clicked again and no value is chosen, the field is automatically updated to the opposite value, 0 / Fail. I can't figure out why this is suddenly happening after it worked correctly for so long. Any input you may have would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/315491-empty-function-no-longer-detects-an-empty-select-list/ Share on other sites More sharing options...
ginerjm Posted November 3, 2022 Share Posted November 3, 2022 Where does $_REQUEST have to do with this? You are apparently processing a POST method on a form so stick with that! And what do the fields "field_value" and "select_value" have to do with this topic? You are only showing us one form field and it is named "my_select". And you are trying to access a field named "myselect" but you are not showing us that field in your code example either. And - when one posts code here you should use the <> symbol at the top of you window to post the code into. Makes for nicer reading. And leave out the extra blank lines too if you can. Quote Link to comment https://forums.phpfreaks.com/topic/315491-empty-function-no-longer-detects-an-empty-select-list/#findComment-1602197 Share on other sites More sharing options...
requinix Posted November 3, 2022 Share Posted November 3, 2022 Also, note that "" and "0" are both considered empty(). Quote Link to comment https://forums.phpfreaks.com/topic/315491-empty-function-no-longer-detects-an-empty-select-list/#findComment-1602207 Share on other sites More sharing options...
maxxd Posted November 3, 2022 Share Posted November 3, 2022 Both answers are correct and need to be fixed. First, @requinix's answer is absolutely correct, but as @ginerjm pointed out, you've also named your field 'my_select' while you're checking $_POST for 'myselect'. Set the value of your blank option to -1 and check if $_POST['my_select'] >= 0. Quote Link to comment https://forums.phpfreaks.com/topic/315491-empty-function-no-longer-detects-an-empty-select-list/#findComment-1602209 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.