RLJ Posted October 2, 2010 Share Posted October 2, 2010 Hi, I have an HTML form with a "submit" button that sends form data to a PHP script. Firstly, the form doesn't seem to send the data to the PHP script, unless I include a line in the PHP script as follows: $var1 = $_REQUEST['var1']; Is this always the case or is there a way to automatically have the form send through data? The reason I ask is because the form includes elements that can be disabled, in which case (if element 'var1' is disabled) the above PHP line returns an error "Notice: Undefined index: var1" i.e. I want to only send through data from enabled elements and not from disabled elements. Alternatively, is there a line I can include in the PHP script something along these lines: if (defined($var1)==FALSE) {$var=" ";} so that when the PHP script tries to retrieve var1, which is undefined because the corresponding form element has been disabled, it sets it to a particular value. my HTML form is along these lines: <form action=something.php method=POST> <select id="var1" name="var1"> <option> </option> <option selected="selected"> option1</option> <option> option2</option> <option> option3</option> </select> <input type="submit" value="next"> </form> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/214974-retrieve-data-from-form-with-some-disabled-elements/ Share on other sites More sharing options...
jskywalker Posted October 2, 2010 Share Posted October 2, 2010 Firstly, the form doesn't seem to send the data to the PHP script, unless I include a line in the PHP script as follows: $var1 = $_REQUEST['var1']; it always send ALL data that you defined in the form, but NEVE the disabled elements... Alternatively, is there a line I can include in the PHP script something along these lines: if (defined($var1)==FALSE) {$var=" ";} so that when the PHP script tries to retrieve var1, which is undefined because the corresponding form element has been disabled, it sets it to a particular value. if (isset($_POST['var1])) { $var1 = $_POST['var']; This Quote Link to comment https://forums.phpfreaks.com/topic/214974-retrieve-data-from-form-with-some-disabled-elements/#findComment-1118275 Share on other sites More sharing options...
DavidAM Posted October 2, 2010 Share Posted October 2, 2010 Forms also do not send un-checked checkboxes (I wish it would -- I think it should -- but it doesn't). If your form method is POST form fields end up in the $_POST array keyed by the element's name. If the form method is GET they end up in the $_GET array. You may have seen old code that made them automatically into variables, but this feature has been depricated and is turned off by default now. While jskywalker's suggestion is correct; it will leave the variable undefined if the form field was disabled when the form was posted. So, expanding that solution you can use an else: if (isset($_POST['var1'])) { $var1 = $_POST['var1']; } else $var1 = ""; } or you can use the ternary operator: $var1 = (isset($_POST['var1']) ? $_POST['var1'] : ""); Quote Link to comment https://forums.phpfreaks.com/topic/214974-retrieve-data-from-form-with-some-disabled-elements/#findComment-1118294 Share on other sites More sharing options...
jskywalker Posted October 2, 2010 Share Posted October 2, 2010 Forms also do not send un-checked checkboxes (I wish it would -- I think it should -- but it doesn't). an unchecked checkbox is not checked, so there's no need to send it? for this checkbox you can do this: $var1 = (isset($_POST['var1']) ? true : false); Quote Link to comment https://forums.phpfreaks.com/topic/214974-retrieve-data-from-form-with-some-disabled-elements/#findComment-1118299 Share on other sites More sharing options...
RLJ Posted October 2, 2010 Author Share Posted October 2, 2010 cheers guys, that worked! Quote Link to comment https://forums.phpfreaks.com/topic/214974-retrieve-data-from-form-with-some-disabled-elements/#findComment-1118356 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.