Calender with checkbox: How to insert information into database from checkbox
I am tr开发者_StackOverflowying to create a calender with a checkbox for each date. When a user clicks the submit button, I want to get those selected dates inserted into the database and those which are not selected would be deleted from the database if those already exist before. I have created a index.php page where I wrote the following code to generate dates:
index.php:
<form name="form1" method="post" action="show.php">
<?php
$year="2011";
$month="8";
$d=cal_days_in_month(CAL_GREGORIAN,1,2011);
$i=1;
while($i<=$d)
{
?>
<input type="checkbox" name="date[]" value="<? echo " $year-$month- " . $i . ""."<br>"; ?>">
<? echo " $year-$month- " . $i . ""."<br>"; ?>
<?
$i++;
}
?>
<input type="submit" name="Submit" value="Submit">
</form>
show.php:
<?
mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("database");
$media_array = $_POST['date'];
foreach ($media_array as $one_media)
{
$source .= $one_media.", ";
}
$media = substr($source, 0, -2);
$query = "INSERT INTO table1 (id,media) VALUES('', '$media')";
$result = mysql_query($query); }
?>
And then I created page another page named show.php where I tried to write codes for inserting information, but I failed. Would you please kindly help me to do this? I have tried my best but I couldn't make it happened. Thanks in advance.
The easiest way would be to make use of Ajax (going to use the JQuery library to make it a bit easier). So every time a checkbox changes values an Ajax request is triggered to updateDatabase.php and it either deletes or inserts a record.
So if you are showing your checkboxes on the page do:
<script>
$(document).ready(function() {
//Every time a checkbox changes (gets ticked or unticked) send n Ajax request
//to updateDatabase.php containing its value and a BOOLEAN checked/unchecked
$('input[type="checkbox"]').change(function() {
var checked = $(this).is(':checked');
$.ajax({
url: "updateDatabase.php",
type: "POST",
data: {value : $(this).val(),
checked : checked },
dataType: "html",
async:false,
success: function(msg) {
//Show the answer from the file in an alertbox.
//Use this to debug your code. Leave it out afterwards.
alert(msg);
}
});
});
});
</script>
So every time a checkbox is checked or unchecked it sends its value and the "checked" boolean value to updateDatabase.php.
In this file you can do something similar to:
updateDatabase.php
<?php
//Get POST variables
$value = mysql_real_escape_string($_POST['value']);
$checked = $_POST['checked'];
//If the checkbox was checked, INSERT its value into the database
//else you remove it.
if ($checked == true) {
$query = "INSERT INTO table (value) VALUES ('" . $value . "')";
}
else {
$query = "DELETE FROM table WHERE value = '" . $value . "'";
}
mysql_query($query);
?>
EDIT: Added the code to process the data after you click SUBMIT
If you do not want to do this on the flow If you want to update your database when the user clicks SUBMIT, you simply submit the form to show.php.
show.php
<?php
$dates = $_POST["date"];
echo "Dates chosen: " . count($dates) . "<br /><br />";
if (count($dates)>0) {
echo "You picked the following dates:<br />";
}
for ($i=0; $i<count($dates); $i++) {
echo ($i+1) . ") " . $dates[$i] . "<br />";
mysql_query("INSERT INTO table (value) VALUES ('" . mysql_real_escape_string($dates[$i]) . "')");
}
?>
Hope this helps you!
(I did not test this code so I'm sorry if there are some minor errors in there still. But I'm sure you catch the drift!)
精彩评论