I am trying to send multiple data with jQuery.ajax. The combobox product1 and the textbox price are working. But when I try to send the text of quantity something goes wrong.
index.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' onChange='getPrice(this.value)' name='product1' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row["id"]. "'>" . $row["naam"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
<html>
<body>
<!-- Your text input -->
<input id="quantity" type="text" placeholder="quantity">
<!-- Your text input -->
<input id="product_name" type="text" placeholder="product_name">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getPrice() {
// getting the selected id in combo
var selectedItem = jQuery('#product1 option:selected').val();
// Do an Ajax request to retrieve the product price
jQuery.ajax({
url: 'get.php',
method: 'POST',
data: 'id=' + selectedItem + ', quantity:document.getElementById('quantity').value,
success: function(response){
// and put the price in text field
jQuery('#product_name').val(response);
jQuery('#quantity').html(response); },
error: function (request, status, error) {
alert(request.responseText);
},
});
}
</script>
</body>
</html>
get.php
<?php
// Turn off all error reporting
error_reporting(0);
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error)
{
die('Connection failed: ' . $conn->connect_error) ;
}
else
{
$product1 = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT) ;
$quantity = filter_input(INPUT_POST, 'html', FILTER_SANITIZE_NUMBER_INT) ;
$query = 'SELECT price * ' . $quantity . ' FROM forms WHERE id=' . $product1 . ' ';
$res = mysqli_query($conn, $query) ;
if (mysqli_num_rows($res) > 0)
{
$result = mysqli_fetch_assoc($res) ;
echo $result['price'];
}else{
echo "0 results";
}
}
?>
Can someone help me with this?
SELECT price * ' . $quantity . ' FROM forms WHERE id=' . $product1 . 'but nothing is happening.