Updating database with ajax on checkbox click
Rate Topic:

having successfully created a dependent dropdownlist, coding with ajax/jquery under Yii last week, I'm now trying to do the same with a checkbox, but for the moment I'm not as much successful and some help would be greatly appreciated !

What I'm trying to do :

When I check/uncheck the checkbox, I would like the corresponding boolean database field to update and the checkbox to change its display state.

So you're saying that puting ajax directly in the options of the checkbox won't work because of that ? But why did it work with a dropdownlist ?

Yes, i assumed this ain't going to work since there is no trigger for your action(I.E: how is the ajax request will be triggered, by who?)
Maybe on your dropdown you would have bound the event to "onclick" and in this case you could do the same and bind it "onchange"?

[*]Error : no error raised. The display state of the checkbox is well changed when I click on it, but the database doesn't update, yet an ajax request is posted.

This is what i wanted to see
If the ajax request is sent, let's also see what post params this sends along.
Using firebug/developer tools on chrome, when you watch your ajax request, see the url where it goes and also by clicking in console on the request link you will get a bunch of details about the request.

L.E:
I attached a screenshot from chrome for you to see what data we need, please take a look.
In Form Data tab you should find the data that is sent by your ajax request.

Using firebug/developer tools on chrome, when you watch your ajax request, see the url where it goes and also by clicking in console on the request link you will get a bunch of details about the request.

Yes that's what I've been looking at during all afternoon yesterday....without finding the light

In Form Data tab you should find the data that is sent by your ajax request.

So the data sent is User[est_valide]:0. Knowing that to send this request, I clicked on a previously unchecked checkbox, shouldn't it be the contrary ?
When I click on an already checked checkbox, the data sent is 1.
So it seems that the data sent is the same as the already recorded one in the database...
Seeing that yesterday, I tried to change my sql request in the controller, writing

if you see "User[est_valide]:0" then $_POST['User']['est_valide'] should be 0 in your controller, but why not do a var_dump there and see the result? [ exit(var_dump($_POST)) ]

However, if your est_valide var is one time 0 then the next time is 1 it should change in the database, no matter what. So, who is $u in your actionUpdateUserEst_valide method? i don't see it instantiated anywhere, do you have error reporting set to E_ALL so that you can see all the errors and warnings?

if you see "User[est_valide]:0" then $_POST['User']['est_valide'] should be 0 in your controller, but why not do a var_dump there and see the result? [ exit(var_dump($_POST)) ]

Sorry I don't know where to put var_dump. Whether I put var_dump($_POST); or var_dump($_POST['User']['est_valide']); in the actionUpdateUserEst_valide function or in the view, nothing shows.

Quote

However, if your est_valide var is one time 0 then the next time is 1 it should change in the database, no matter what. So, who is $u in your actionUpdateUserEst_valide method? i don't see it instantiated anywhere,

Ok, the problem comes from there :
if I modify the function to WHERE user_id=6, the db updates. About $u, I took it from the view where is coded foreach( $users as $u ) to retrieve the current user id. Obviously I did wrong. But now I wonder what the syntax should be to get the current user id...

Quote

do you have error reporting set to E_ALL so that you can see all the errors and warnings?

You can retrieve the current user id with Yii::app()->user->getId(); but only if your auth implementation is following the Yii standards.
You should look at the login controller/methods and see how the user_id is stored in case your auth mechanism is something custom.

Other than that, if you fix this "get the user id" issue, everything works good right ?

L.E:
Also, if you need the user id for each user from the foreach loop, you can send it along with the est_valide variable, something like:

Actually the user I want to get the id from is not the current logged in user, it is the user whom I want to change the status est_valide, for who the name and email and role are on the same row as the checkbox in the datagrid on the view... am I clear ?

Edit : sorry I hadn't seen your edit yet. Gonna try what you last posted and come back...

see my last edit please(in #10), i assumed that you need the user id from the foreach loop.

Yes twisted1919, that's it !! Finally it works all good !

The thing is I'd like to understand how the syntax works... do you know a website or a tuto I could read to better understand ?
I specially wonder how it goes from array("data-user-id"=>$u['user_id']) to $(this).data("user-id")

Otherwise, as a last improvement, if I want to raise a confirmation box on the checkbox click, can you tell me where to put the confirm code, if not too complicated ?

For the "data" attribute accessing, this is jquery stuff. any html tag produced by CHtml class has the htmlOptions param, which allows you to pass additional data to the element.
Adding to data-* means that you can access it from jquery with $(elem).data(*).
Anyway, jquery website is the way where you can find real documentation about this

no problem, glad i could help.
Learning jquery will be a huge plus for you because Yii is using it and it will make your work with the framework(and not only) way easier so just go for it and don't look back