Fishcakes Posted January 21, 2022 Share Posted January 21, 2022 Hi So my webpage puts out a load of data formatted like the below for my forum So I'm currently working on the ModTools dropdown and I'd like to be able to select from the drop down then pick an item which runs a php command (to delete a forum thread Or lock the thread or delete and ban user this kind of thing) so at the moment I am working on the MODTOOLS dropdown box So I created this class and this function class ModTools extends GetModList { public function DisplayModTools($PostId, $User) { //If statement to check user is logged in if(isset($_SESSION['username'])){ $Output = " <form id='DropdownForm' action='' method='post'> <select name='modSelected' id='ModTool' onchange=’this.form.submit()'> <option value=''>ModTools</option> <option value=$PostId>Delete</option> <option value=$PostId>ShitLib</option> <option value='$PostId>Offtopic</option> </select> </form> "; return $Output ; } //endif statement }//end func } Then I have this php in my index.php file near the bottom if(isset($_POST["modSelected"])){ $Dropdownstatus=$_POST["Delete"]; echo "select dropdown modlist is => ".$Dropdownstatus; } But it doesn't seem to echo out the last echo command? Quote Link to comment https://forums.phpfreaks.com/topic/314444-ajax-and-php-want-to-create-a-dropdown-onchange-of-dropdown-i-want-to-execute-php/ Share on other sites More sharing options...
gw1500se Posted January 21, 2022 Share Posted January 21, 2022 Did you echo $_POST["modSelected"]) to make sure it is set and not just dropping past the 'if' block? Quote Link to comment https://forums.phpfreaks.com/topic/314444-ajax-and-php-want-to-create-a-dropdown-onchange-of-dropdown-i-want-to-execute-php/#findComment-1593514 Share on other sites More sharing options...
Phi11W Posted January 21, 2022 Share Posted January 21, 2022 29 minutes ago, Fishcakes said: <select name='modSelected' id='ModTool' onchange=’this.form.submit()'> <option value=''>ModTools</option> <option value=$PostId>Delete</option> <option value=$PostId>ShitLib</option> <option value='$PostId>Offtopic</option> </select> Ignoring the dodgy HTML syntax for a moment, remember that only the value of the selected option gets submitted in the HTTP form data, not the [inner text] description. With this in mind, how is your PHP code supposed to tell the difference between the 2nd, 3rd and 4th options (all of which will effectively submit "modSelected=$PostId")? Also, submitting a form on each change of the DropDownlist is likely to lose you major points on the "Accessibility" scale. People will have a very hard time trying to navigate your form using the keyboard. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/314444-ajax-and-php-want-to-create-a-dropdown-onchange-of-dropdown-i-want-to-execute-php/#findComment-1593515 Share on other sites More sharing options...
Fishcakes Posted January 21, 2022 Author Share Posted January 21, 2022 34 minutes ago, gw1500se said: Did you echo $_POST["modSelected"]) to make sure it is set and not just dropping past the 'if' block? Yes I did 6 minutes ago, Phi11W said: Ignoring the dodgy HTML syntax for a moment, remember that only the value of the selected option gets submitted in the HTTP form data, not the [inner text] description. With this in mind, how is your PHP code supposed to tell the difference between the 2nd, 3rd and 4th options (all of which will effectively submit "modSelected=$PostId")? Also, submitting a form on each change of the DropDownlist is likely to lose you major points on the "Accessibility" scale. People will have a very hard time trying to navigate your form using the keyboard. Regards, Phill W. Yes that was just me testing something I tried it here for instance and this works in a test file <html> <head> <title>Country</title> </head> <body> <form method="POST" action=""> Select Your Country <select name="country" onchange="this.form.submit()"> <option value="" disabled selected>--select--</option> <option value="india">India</option> <option value="us">Us</option> <option value="europe">Europe</option> </select> </form> <?php if(isset($_POST["country"])){ $country=$_POST["country"]; echo "select country is => ".$country; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/314444-ajax-and-php-want-to-create-a-dropdown-onchange-of-dropdown-i-want-to-execute-php/#findComment-1593516 Share on other sites More sharing options...
Phi11W Posted January 24, 2022 Share Posted January 24, 2022 On 1/21/2022 at 3:46 PM, Fishcakes said: I tried it here for instance and this works in a test file <form method="POST" action=""> Select Your Country <select name="country" onchange="this.form.submit()"> Any non-Indian Users that favour keyboard navigation are still going to be very frustrated by this design. As soon as they try to move "down" the list with the arrow keys, the Javascript code will submit the page, including the first option ("country=india"). This reloads the page and, sadly, your code doesn't cater for the list "remembering" which option was chosen previously and just resets itself to the first, "--select--" option. Pressing the down key again just goes round the same loop over and over again (and hits your PHP backend each and every time). Do you really need such real-time handling of a change in country? Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/314444-ajax-and-php-want-to-create-a-dropdown-onchange-of-dropdown-i-want-to-execute-php/#findComment-1593566 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.