I have an input box wrapped in a while loop, so i displays the ID or items in the database. Thus i have input boxes as 001,002,003,004,005 ... and I have a button below each input box.
What i want
1) When i click on a button, i want JS to log the value of the corresponding input box for example if I click on the button below input box 003, js should display 003.
2) I also want to use Ajax to send that value to database via (sendajax.php) and on success it should display a div with a message.
What I'm Getting
I keep getting 001 no matter the button i click on.
The Code
<?php
$booksSql = "SELECT * FROM tblitems";
$getBooks = mysqli_query($con, $booksSql);
while($row = mysqli_fetch_assoc($getBooks)){
$id = $row["itemid"];
?>
<form>
<input id="itemID" name="itemID" type="text" value="<?php echo $id; ?>">
<!--it display 001, 002, 003, 004 in the input boxes as it should-->
<input id="submit" onclick="addCart();" type="button" value="Submit">
</form>
<?php
}
?>
<script>
function addCart(){
var input = document.getElementById('itemID').value;
console.log(input);
//but this keeps displaying 001, no matter which button i click
}
</script>
My current sendajax.php
include("dbconn.php");
$id = $_POST["id"];
if (isset($_POST['id'])) {
$send = "query to insert to db";
$query = mysqli_query($con, $send);
}