I am making a bill, when I add more than one product, then the price of all the products is being same. Because of this, I cannot even get the total of all these But i am not able do it , but i am struggling with this. please check my code and tell me its answer.
bill.php
<?php
include 'head.php';
?>
<div>
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Amount</th>
<th><input class="addrow" type="button" name="" value="Add"></th>
</tr>
</thead>
<tbody>
<tr>
<td><select class="form-control">
<option value="">--select--</option>
</select></td>
<td style="width: 100px"><input class="form-control quantity" type="number" name=""></td>
<td style="width: 100px"><input class="form-control price" value="" disabled type="text" name=""></td>
<td style="width: 200px"><input class="form-control amount" value="" disabled type="text" name=""></td>
<td><input class="deleterow" type="button" name="" value="Delete"></td>
</tr>
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
$.ajax({
url:"webservice.php",
type:"post",
dataType:'json',
success:function(data){
$('.table tbody select').append(data);
},
error:function(){
console.log("ERROR IN AJAX");
}
});
});
$(document).on('change','select',function(){
let product_value = $(this).val();
$.ajax({
url:"productprice.php",
type:"post",
data:{product_value},
dataType:'json',
success:function(data){
$('.price').val(data);
},
error:function(){
console.log("ERROR IN AJAX");
}
});
});
$(document).on('click','.addrow',function(){
$('.table tbody tr:first-child').clone().appendTo('.table tbody');
});
$(document).on('click','.deleterow',function(){
if($('.table tbody').children().length == 1){
alert("minimum 1 Product required");
}else{
$(this).closest('tr').remove();
}
});
$(document).on('change','.quantity',function(){
let quantity = $('.quantity').val();
let price = $('.price').val();
$('.amount').val(quantity * price);
});
</script>
webservices.php
<?php
include 'connection.php';
$sql = "SELECT * FROM `products`";
$result = mysqli_query($db,$sql);
// $output ="";
while ($row = $result->fetch_assoc()) {
$output[] = "<option value=$row[product_id]>$row[product_name]</option>";
}
echo json_encode($output);
productprice.php
<?php
include 'connection.php';
$sql = "SELECT * FROM `products`";
$result = mysqli_query($db,$sql);
// $output ="";
while ($row = $result->fetch_assoc()) {
$output[] = "<option value=$row[product_id]>$row[product_name]</option>";
}
echo json_encode($output);
In your current code you are targetting all element with classprice
whenever your select gets changes . Instead , you can use$(this).closest('tr').find('.price')
hereclosest()
method will get closest tr where change has been occur then using.find()
method get required price input only . Same do for quantity change event .
Demo Code :
//just for demo...
var data = "<option value=1>A</option><option value=2>B</option><option value=3>C</option>"
$('.table tbody select').append(data);
$(document).on('change', 'select', function() {
let product_value = $(this).val();
//declare this outside ajax call..
var selector = $(this).closest('tr').find('.price')
/*$.ajax({
url: "productprice.php",
type: "post",
data: {
product_value
},
dataType: 'json',
success: function(data) {*/
selector.val(34); //just for demo.. use .val(data)
/*},
error: function() {
console.log("ERROR IN AJAX");
}
});*/
});
$(document).on('click', '.addrow', function() {
var cloned = $('.table tbody tr:first-child').clone();
$(cloned).find("select").val(""); //empty select
$(cloned).find("input:not(:last)").val(""); //empty input values
$(cloned).appendTo('.table tbody');
});
$(document).on('click', '.deleterow', function() {
if ($('.table tbody').children().length == 1) {
alert("minimum 1 Product required");
} else {
$(this).closest('tr').remove();
}
});
$(document).on('change', '.quantity', function() {
let quantity = $(this).val(); //get qty value where change occur
let price = $(this).closest('tr').find('.price').val(); //get price only from required row..
//add result only on same row..
$(this).closest('tr').find('.amount').val(quantity * price);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Amount</th>
<th><input class="addrow" type="button" name="" value="Add"></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="form-control">
<option value="">--select--</option>
</select>
</td>
<td style="width: 100px"><input class="form-control quantity" type="number" name=""></td>
<td style="width: 100px"><input class="form-control price" value="" disabled type="text" name=""></td>
<td style="width: 200px"><input class="form-control amount" value="" disabled type="text" name=""></td>
<td><input class="deleterow" type="button" name="" value="Delete"></td>
</tr>
</tbody>
</table>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.