|
Posted by Hilarion on 12/28/05 23:38
> i have a small webapplication where there is a table with a large number of
> rows - each having a checkbox. User can mark or unmark a checkbox.
> Now when this form is sent, I must deal with the new marked and the unmarked
> lines.
>
> insert the new marked into a database and
> delete the unmarked one from the database.
>
> There is a trivial solution to compare the send information from the form
> with the database
> those which are not in the database must be new one and those that should be
> in the database which are not listed must be deleted.
>
> But I want to know whether there is a more efficient method, cos when the
> user is actually clicking and changing the information is already there,
> right ?! But I'm not allowed to use javascript -
>
> Are there more HTML Tricks or sth. which I can use?
>
>
> I have tried so far :
> normally:
> e.g.
> <input type="checkbox" name="feed" value="cat">
>
> extended:
> <input type="checkbox" name="feed" value="cat #checked#">
> or
> <input type="checkbox" name="feed" value="dog ##">
>
> #checked# will be added when it's already in the database otherwise ##
> (## then means that it's available but not yet in the database)
>
> but since I get the information : "feed=dog ##" I know thats a new one which
> must be added to the database.
> and also doesnt need to check for 'cat'
>
> I hope somebody can understand what i mean..
>
> But I have no idea how to get the other information to delete which one.
I use something that adapted to your example would look like this:
<input type="hidden" name="feed[cat][was_there]" value="1" />
<input type="checkbox" name="feed[cat][selection]" value="1" />
<input type="hidden" name="feed[dog][was_there]" value="0" />
<input type="checkbox" name="feed[dog][selection]" value="1" />
and in the script that gets the data I do something like this:
foreach( $_REQUEST['feed'] as $key => $value )
{
if (intval( $value['was_there'] ))
{
// the entry specified by $key was in the DB
if (@intval( $value['selection'] ))
{
// is selected, so do nothing or update
}
else
{
// is not selected, so delete from DB
}
}
else
{
// entry specified by $key was NOT in the DB
if (@intval( $value['selection'] ))
{
// is selected, so insert it into DB
}
else
{
// is not selected, so do nothing or update
}
}
}
It gets more tricky when some other user will change the
data before this one submits the form. In this case the
script may "think" that the data is in the DB and not
insert it, when it's not there, because the other user
deleted it, or it may "think" that the data is not in
the DB and not delete it, when it's there, because the
other user inserted it.
If it's for something available only for one user
(eg. they modify their personal settings), then it should
work OK in most cases.
Hilarion
Navigation:
[Reply to this message]
|