I am attempting to make a dynamic list of entries in a html table with buttons to move entries to an archive. I can get the button var to JS, I can execute a query in that function, but I can not use the JS var for that query.
With what should I replace $test in the query for it to use the JS var?
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<style>
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
$mysqli = new mysqli('localhost', 'un', 'pw', 'db');
$list = "<table class='table table-bordered'>";
$list.= "<center><h2>list</h2>";
$query = "SELECT * from table1 ORDER BY date ASC;";
$stmt = $mysqli->prepare($query);
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_all(MYSQLI_ASSOC);
foreach ($data as $row) {
$table_id = $row['id'];
$list.= "</tr><tr>";
$list.= "<tr><td>";
$list.= $row['name'];
$list.= "</td><td>";
$list.= "<form action='' method='post'>";
$list.= "<button value='$table_id' name='submit' id='arch' type='submit' onclick='dosomething(this.value)' class='btn btn-danger btn-xs'>move to archive</button>";
$list.= "</form>";
$list.= "</td></tr>";
}
$list .= "</table>";
echo $list;
$id = 5; //placeholder
if(isset($_POST['submit'])){
echo "phpres=".$table_id; // only gives the latest value in the list
$stmt = $mysqli->prepare("select * from archive where id = $id");
if($stmt->execute()){
$result = $stmt->get_result();
if($result->num_rows>0){
$msg = "<div class='alert alert-danger'>Already Copied</div>";
}else{
$stmt = $mysqli->prepare("INSERT INTO archive SELECT * FROM table1 WHERE id = $id");
$stmt->execute();
$msg = "<div class='alert alert-success'>Copied Successfull</div>";
$stmt->close();
$mysqli->close();
}
}
}
?>
</div>
</div>
</div>
<script>
function dosomething(val) {
alert(val); //does give correct value but can't be used
}
</script>
</body>
</html>