When the combobox is select, i'm trying to display first n items from database
- i'm trying to call the PHP function "produse($_POST)" with the value of the selected item in the select tag, the code is correct?
- the limit in the SELECT SQL can be the one in the code? ("$sql = "SELECT * FROM table LIMIT $number";")
<body>
<select name="n" onchange="document.write('<?php produse($_POST); ?>')">
<option disabled selected value> -- select an option -- </option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
<br> <br> <br>
<?php
function produse($number){
$servername = "localhost:3306";
$username = "root";
$password = "";
$dbname = "produse";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$number = intval($_GET['number']);
$sql = "SELECT * FROM table LIMIT $number";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id_produs: " . $row["id_produs"]. " - Denumire: " . $row["Denumire"]. " - Pret " . $row["Pret"]. " - Descriere" . $row["Descriere"] ."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
?>
</body>