Hi everyone I'm new here ! I'm wondering how I can insert the values of a printed row when it's specific checkbox is ticked.
here is my code:
<?php
$query = "SELECT * FROM phpmyreservation_forvalidation";
$result = mysql_query($query);
?>
<form action='insertrecords.php' method='post'>
<?php
echo "<table cellpadding='0' cellspacing='0' border='1'>";
echo "<tr> <th>reservation_id</th> <th>reservation_user_name</th> <th>reservation_day</th> <th>reservation_week</th>
<th> # </th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['reservation_id'];
echo "</td><td>";
echo $row['reservation_user_name'];
echo "</td><td>";
echo $row['reservation_day'];
echo "</td><td>";
echo $row['reservation_week'];
echo "</td><td align='center'>";
echo "</td>";
?>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?= $row['id'] ?>" ></td>
<?php
}
echo "</table>";
?>
<input type='submit' value='Submit' />
</form>
I'm not sure if what i've done is correct, I also don't have an insert records.php yet, I tried to make one but it didn't work and now I'm really confused. Does anyone know how I can do this ? Thank you !
additional notes: What I'm doing is a validation system the schedules that are not yet validated are on a separate table, when the admin validates them it gets inserted into the phpmyreservation_reservations table.
Thanks !
fetch the 'phpmyreservation_forvalidation' result into variables and insert it into your 'phpmyreservation_reservations' table when the form is submit.
In records.php:
$row_id=$_POST['checkbox'];
foreach ($row_id as $key=>$val)
{
//do insert here with $val
}
If I were you, I would use AJAX calls to obtain all the data needed from your database and also to insert the data to a new table. In the example below I used jQuery to do ajax calls.
my example web page code:
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Title of the page</title>
<script src="jquery-1.10.2.min.js"></script>
<script>
// Function beeing called when a checkbox is changed
function MyCheckboxChanged(sender)
{
// Send the data to be processed to the server if the checkbox is checked
if (sender.checked)
{
var MyObject = {};
MyObject.ValueToInsert = sender.value;
DoAJAXData("insert", MyObject);
}
}
// Function responsible for AJAX calls - requires jQuery
function DoAJAXData(command, MyObject)
{
$.post("phpReceiver.php",
{
command:command,
sendObject:JSON.stringify(MyObject)
})
.success(function(data)
{
// Process the data echoed from php
// In my case, should alert: "successfully inserted data: " + your selected checkbox value
alert(data);
})
.fail(function(error){
//alert("Unable to retrieve data from the server");
});
}
</script>
</head>
<body>
<?php
for ($i=0; $i < 5; $i++)
{
$var = <<<CHB
<input type="checkbox" onchange="MyCheckboxChanged(this);" name="$i" value="Bike number $i">I have a bike number $i</br>
CHB;
echo $var;
}
?>
</body>
</html>
and the phpReceiver.php code:
<?php
$command = $_POST['command'];
$object = json_decode($_POST['sendObject'], true);
if ($command == "insert")
{
$valueToInsert = $object['ValueToInsert'];
// Do you insertion to the database here
// Echo any results if wanted
echo "successfully inserted data: $valueToInsert";
}
?>