Coding Thread, drop down menu in php in Coding and Web Development; Hi all
I was wondering if somebody could help me with a bit of code, we are currently using mrbs ...
I was wondering if somebody could help me with a bit of code, we are currently using mrbs and I have a bit of an issue with it at the moment when staff book a room they are not selecting there department and just leave it as default, the default entry is A in a list array which I have set to please select your department.
what I want to do is to force the them to select any entry apart from default by throwing up an error if not selected I have managed to get as far as getting the error on screen but I unable to link it with the entry type.
As far as I can see your approach is fundamentally wrong. You want to output the same HTML each time, whatever you are doing. Then on your postback, validate the form (this would include checks on the combobox select, obviously).
If the validation(s) fail then you need to mark them somehow - for example have an red asterisk alongside those which are wrong, and reseed the HTML Form controls with the data, so they don't have to type it in again.
What you have popped up on the thumbnail (the first one) is a Javascript Alert box (I think !). This is a Javascript function, not a PHP one, and whilst you can theoretically do this by outputting script in PHP this is a strange way of doing it. If you want alert() boxes (Javascript message box) then you should be client side validating your form in Javascript before it is Posted back to the server. If you google there are plenty of articles on client-side validation.
Don't assume this works however ; your server side PHP should still cope with bad values - even if it just throws an error if it gets a value that the validation says it shouldn't.
a quick thinkup php side goes somthing like this! presuming your passing information with the POST method?
$deptcheck = $_POST["<<INSERT FIELD NAME HERE>>"];
if $deptcheck == "Please Select your Department"
<<insert error / handle code here>>
Those 3 lines (as you have "Please Select your Department" as default) checks the information submitted to the <<INSERT FIELD NAME HERE>> field, to see if it matches, and if it does then it would mean that the user has not selected another option.
must admit though it would be usefull to validate it with javascript, as it saves strain on the server that way too. unfortunatly javascript i'm not too familiar with on that one, you would need to do some googleing ^_^
Thanks for your replies guys I appreciate it, So far I have managed to force the error please select your department by using a jquery script, but unfortunately I can only get it to work with a seperate save button.
I some how have to figure out how to link my script with the original save button.
Thanks for your replies guys, I have managed to do it now just by using a java script and a little modification in edit_entry.php with the drop down menu coding.
just a thought to add in. if this is part of a larger form, make sure your php feedback page carries valid form values over to the feedback page. if i fill in ten boxes then miss a drop down, i'd like the ten boxes to stay filled in or to be held in hidden inputs so i can just focus on correcting the dropdown. i guess you could rely on people clicking the back button but i've had that reset the form before (not sure if blanking the form was a browser thing or a "feature" of the page that had been coded in)