Reputation: 55
I am writing a programme i am printing from mysql with php in a table i want to get input field value by the help of jquery how can i get each input value using jquery.
<?php
$sql = "SELECT id, name, price, qty FROM appetisers";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$quantity = $row["qty"];
if ($quantity == '' || $quantity == '0') {
$quantity = '1';
}
?>
<tr class="eachrow">
<td><?php echo $row["name"] ?></td>
<td class="price-amount">£ <?php echo $row["price"] ?></td>
<td><input type="text" name="amount" class="amount-type" value="<?php echo $quantity; ?>"/></td>
<td><a href="" class="add-cart">Add to cart</a></td>
</tr>
<?php }
}
?>
Upvotes: 0
Views: 703
Reputation: 2857
Try this:
$('.add-cart').click(function(e){
var val = $(e.target).closest('.eachrow').find('[ name="amount"]').val();
console.log(val);
})
Upvotes: 1
Reputation: 464
<script>
var TableData = new Array();
$('table tr.eachrow').each(function(row, tr){
TableData[row]={
"amount" : $(tr).find('td:eq(2)').val()
}
});
TableData.shift();
</script>
Upvotes: 0
Reputation: 221
Try this
$('.amount-type').each(function() {
console.log( this.val() );
});
Upvotes: 0
Reputation: 291
By using Jquery val()
you can get the value of text field like this
var Values = $( ".amount-type" ).val();
i think it will work fine
Upvotes: 0
Reputation: 7490
Firstly you'd need to give you inputs some sort of identifyer for example add and increment so your inputs can have id's such as
'appetisers_' . $increment
Then you can get these with jquery and get the value using
$('#appetisers_3').val();
At the moment you have a name on the inouts, but they will all have the same name
Upvotes: 0