Single player table - two drop-down groups - how not to get double players (mysql, php, ajax)

I have a single player table that stores all the players. When will I choose the players for the home team and the guest team. I have two different sql queries in the players table.

<? $select_query = mysql_query("SELECT * FROM $player_table_2016 WHERE active = 1 ORDER by tname ASC"); echo "<select data-placeholder='Spieler auswƤhlen...' class='chosen-select' style='width:220px;' multiple tabindex='4' name='homespieler[]' required>"; while ($row = mysql_fetch_array($select_query)) { echo "<option value='".$row['id']."'>" .$row['tname']."</option>"; </option>"; } echo "</select>"; ?> 

after that I select the guest team with a different request:

 $select_query = mysql_query("SELECT * FROM $player_table_2016 WHERE active = 1 ORDER by tname ASC"); echo "<select data-placeholder='Spieler auswƤhlen...' class='chosen-select' style='width:220px;' multiple tabindex='4' name='awayspieler[]' required>"; while ($row = mysql_fetch_array($select_query)) { echo "<option value='".$row['id']."'>" .$row['tname']."</option>"; </option>"; } echo "</select>"; 

The problem is that I can’t check the team selection if the player is already selected for the home team. It would be great to choose only those players who do not play in the home team in the guest team.

How is this possible?

+5
source share
2 answers

Javascript Solution One Page

After choosing your home player, you need to trigger an event to exclude the player from the second drop-down list. You can do this using JavaScript, for example.

In your options, add an identifier, for example:

 <select id='homeTeamPlayer'> <select id='awayTeamPlayer'> 

In the first <select> add the onchange javascript method as follows:

 <select onchange="excludePlayer()"> 

This method should disable the option in the second drop-down list:

 function excludePlayer() { var homeTeamPlayerId= document.getElementById("homeTeamPlayer").value // Get all options within <select id='homeTeamPlayer'>...</select> var op = document.getElementById("awayTeamPlayer").getElementsByTagName("option"); for (var i = 0; i < op.length; i++) { // lowercase comparison for case-insensitivity (op[i].value == homeTeamPlayerId) ? op[i].disabled = true : op[i].disabled = false ; } } 

(I copied the disabled code here: How to disable <option> in <select> based on its value in JavaScript? )

You must also implement this for a second fall.

 <html> <head> <script> function excludePlayer() { var homeTeamPlayerId= document.getElementById("homeTeamPlayer").value // Get all options within <select id='homeTeamPlayer'>...</select> var op = document.getElementById("awayTeamPlayer").getElementsByTagName("option"); for (var i = 0; i < op.length; i++) { // lowercase comparison for case-insensitivity (op[i].value == homeTeamPlayerId) ? op[i].disabled = true : op[i].disabled = false ; } } </script> </head> <body> <select id='homeTeamPlayer' onchange="excludePlayer()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <select id='awayTeamPlayer'> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> </html> 

Two-Page SQL Solution

The first time you have one drop-down list to select a home team player. The generated HTML should look like this:

 <form method="post" action="page2.php"> <select data-placeholder='Spieler auswƤhlen...' class='chosen-select' style='width:220px;' multiple tabindex='4' name='homespieler' required> <option ...> ... </option> <option ...> ... </option> </select> <input type=submit /> </form> 

On your second page, you can now get the value of the selected home player as follows: $POST_['homespieler']

You can do this in your request as follows:

 SELECT * FROM $player_table_2016 WHERE active = 1 AND ID != $POST_['homespieler'] ORDER by name ASC 
+1
source

You can use ajax for such scenarios. Once the user selects all the players for the home team, use the jQuerys .blur event and pass the entire id to php. And how can you use the select * from table where id not in ($id) where $ id is the value obtained using the POST or GET method

+1
source

Source: https://habr.com/ru/post/1238007/


All Articles