John Davidson

javascript - SQL query based on select option

0 comments
Message:


This is the select option I have:


<div class="selectTeam" style="float:right;color:black;">
<label for="teams">Choose Team:</label>
<select name="teams" id="teams">
<option value="both">Alle Teams</option>
<option value="2">Team 2</option>
<option value="3">Team 3</option>
</select>
</div>

This is how mu query looks like.


$stmt = $link -> prepare('SELECT id, ident, pp, date FROM sales WHERE status=? team=? ORDER BY date DESC LIMIT 5');
$status = "OK";
$stmt -> bind_param("si", $status, $team);

I know I can use document.getElementById("pplegal").value in JS to get the selected value, but how can I do this in PHP, and run the query based on what is selected?


If option value 2 = team=2
If option value 3 = team=3
If option value both = team=2 and 3


My Answer:
html



javascript
document.getElementById("selectOption").addEventListener("change", function() {
var selectedValue = this.value;

// SQL query based on selected option
var sqlQuery = "";

switch(selectedValue) {
case "1":
sqlQuery = "SELECT * FROM table WHERE column = 'value1'";
break;
case "2":
sqlQuery = "SELECT * FROM table WHERE column = 'value2'";
break;
case "3":
sqlQuery = "SELECT * FROM table WHERE column = 'value3'";
break;
default:
sqlQuery = "SELECT * FROM table";
}

console.log(sqlQuery);
});

In this code snippet, we are listening for the change event on the select element with the id "selectOption". When the user selects an option, we retrieve the selected value and based on that value, we construct a SQL query. The constructed SQL query is then logged to the console. You can modify the SQL queries based on your specific requirements.

Rate this post

4 of 5 based on 6327 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us