Jump to content

Ajax and PHP - Want to create a dropdown > Onchange of Dropdown I want to execute PHP


Fishcakes

Recommended Posts

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?

image.png.8998d54f377a18ba2db080d0f909cd2d.png

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.