tags:

views:

232

answers:

2

Hello Everyone,

I have 2 checkboxes in a form and onclick of these, some php code needs to be executed and based on the result of the code, the checkbox is checked or unchecked.

So i have written onclick = document.formName.submit(); Now it is triggering the same page and i am able to write the code. I am not able to differentiate which checkbox is checked.

I don't want to use the procedure of:- calling javascript and then storing the value of the checkbox in a variable and making this variable as invisible.

I would like to write something like document.formName.submit('checkbox1'). So that i should be able to handle the value of this or i dont know.

Please suggest me an alternative method or better approach.

A: 

You can't do it correctly using either saving values in database or passing it as parameter. you have to store somewhere so that when page is get refreshed it must come to know which checkbox is checked earlier.

Salil
Thanku. Other than stroing it in cookies and invisible html variable. Can i do it in any other way. Bcos now itself i have many invisble varables storing values of different actions.
lucky
A: 

What exactly are you doing in the PHP code? Is it validating whether or not they are able to check that box? Sounds like you're making an extra unnecessary step here... If you could describe your scenario a little better I'm sure someone could point you to a more efficient way to accomplish this.

Shawn Steward
Thanku for the suggestion:-) These determine the properties of my primary key. Onclick of anyone of the checkboxes, form is submitted and respective functions are executed. Now in my PHP code i am not able to differentiate which is checked. I am able to accomplish this by storing the value of checkbox in an invisible variable through javascript. I want to have an alternative method.
lucky
You can just check the value in your $_POST["checkboxName"] as mentioned above. If there is no value, it wasn't checked, if the value is what you set it to in the value attribute of the checkbox, that means it's checked. Make sure your checkboxes have unique Names. Otherwise, is there a reason you can't just check this once the form is submitted? If you must check this before submitting you should use AJAX instead.
Shawn Steward
BUt what if, if one the checkbox is already checked and i am checking the other checkbox now, which in turn subitting the form?But in POST it will have the values of both the checkboxes.
lucky
Sounds like you need to restructure the way this works, it sounds like you're going through a lot of unnecessary steps. Is there any reason you have to run the PHP code right when the checkboxes are checked? You can't perform the operation once the whole form is submitted? If you really want it to work immediately then you should use AJAX, otherwise you'll have to keep it how you have it now with the hidden variable in javascript.
Shawn Steward
Thanku shawn. Once the checkbox is checked, only the fields that are relevant to the checkbox are displayed, with default values in it and to fetch the default values i need to trigger php code. SO i can't perform the operation once the whole form is submitted. As the client i am working for does not support AJAX. I can't go for it. Coming to the invisible variable and storing the value of checkbox, in my code i have used couple of them so looking for an alternative. Hope now i made things a little clear.
lucky
Well in that case you are probably stuck keeping the hidden variable in Javascript. With your limitations there's not much else you can do.
Shawn Steward