Reputation: 11084
I have a MySQL database of my movie collection and I'm working on a little website for searching and editing the database. The whole project is more of learning thing then just trying to get it done the easiest way. So I am writing it all from scratch, just plain text files, and I am using mostly PHP.
So I want a page where I can edit the information about a movie. Adding new information is fairly straightfoward, but what about removing information. I have several checkboxes for genres. When the form comes up, any genre currently related to the movie is checked. I want to be able to uncheck a box and then that genre will be dropped from that movie. The only way I can think to do this is create an initial array of values when the form opens, and get the POST array when the form is submitted. Then delete any genres in the first but not the second array. This seems kind of messy, and I feel like there should be a more elegant way to do this. This seems like a pretty standard thing to so maybe people have a nice way to do this?
Upvotes: 3
Views: 610
Reputation: 127
You can use php if statement to check the value in database column and display checked
<input name="column" type="radio" value="1" <?php if($table['column'] = 1) echo "checked" ?>>
<input name="column" type="radio" value="0" <?php if($table['column'] = 0) echo "checked" ?>>
OR if you are using checkbox
<input name="column" type="checkbox" value="1" <?php if($table['column'] = 1){ echo "checked" }elseif($table['column'] = ){ echo "" } ?>>
i hope that will work for you
Upvotes: 1
Reputation: 174957
The best way I can think of is to have a many-to-many relationship between genres and movies (meaning a movie can have multiple genre and one genre can appear on many movies).
This involves adding two more tables, one to define the genres, and one to associate them with movies:
genres
---------
id | name
Here's the slightly tricky part:
genres-movies
-------------------
movie_id | genre_id
Where movie_id
is an actual ID from the movies
table, and genre_id
is an actual ID from the genres
table.
For instance, genre with the ID of 1 is Science Fiction
, and the movie with the ID of 42 is Hitchhiker's Guide to the Galaxy
(see what I did there?), this means that if I have the entry 42 | 1
at the genres-movies
, will mean that Hitchiker's Guide to the Galaxy is a Science Fiction. Note that neither movie_id
nor genre_id
are unique, both can appear multiple times, so the following is possible:
movie_id | genre_id
---------+---------
1 | 2
1 | 3
2 | 2
3 | 1
To find all the genres of a movie:
SELECT `genre_id` FROM `genres-movies` WHERE `movie_id` = <ID>;
After all the background, to your question. With that approach, you need multiple queries, remove all genres from a movie, then each to add the ones you selected. You have to do that because checkboxes that were not selected are not sent to the server (pseudo code)
DELETE FROM `genres-movies` WHERE `movie_id` = <ID>; --Remove all genres
foreach ($genres_from_checkboxes as $genre) {
INSERT INTO `genres-movies` (`movie_id`, `genre_id`) VALUES (:movie_id, :genre_id)
}
NOTE! Always sanitize (or better yet prepare!!!) queries with data that came from the user!
Phew! That was long, hope you learned a thing or two :)
Upvotes: 1
Reputation: 187
You can use input checkbox click event to call an ajax action.
Using jquery (http://api.jquery.com/jQuery.ajax/):
$(document).ready(function() {
$("#form input[type=checkbox]").click(function() {
$.ajax({
url : '/sample/'+$(this).attr('any_id')+'/'+$(this).attr('other_id'),
error: function(){alert('error')},
success : function(data){alert('success')}
});
});
});
And than parse the url to get what you need, and on success function update your form and/or page information using jquery functions like $('element').html() and others.
Another option if you prefer pass the serialized data option for this same function:
$(document).ready(function() {
$("#form.checkboxes input[type=checkbox]").click(function() {
$.ajax({
url : '/sample-form-action',
type: "POST",
data: $("#form.checkboxes").serialize(),
error: function(){alert('error')},
success : function(data){alert('success')}
},
});
});
you can create the form tag around the checkboxes, in this case you can ajax post just the information you really need:
<form name="checkboxes" class="checkboxes">
<input type="checkbox" />
</form>
Upvotes: 1