Link to home
Start Free TrialLog in
Avatar of scrathcyboy
scrathcyboyFlag for United States of America

asked on

Checkboxes conflict -- javascript vs. PHP

A form with 12 checkboxes, and an INPUT field associated with each, like --

form.elements [0]   checkbox   1         form [1]     input % associated with checkbox 1
form.elements [2]   checkbox   2         form [3]     input % associated with checkbox 2         ...up to 12 ckboxes

I wanted to NAME each checkbox different, with a value = 0, so I can test with javascript and set the VALUE of each checked checkbox to 1.   This way, when the form gets to PHP, I can look through the checkboxes for a value of 1, and if so, I can email the NAME of the checkbox and the input % associated with it -- e.g.

'The name called "ABC choice" has been assigned 40%'  and
'The name called "BBC choice" has been assigned 60%.'

That is the goal of the PHP.  Without a unique name for each checkbox, the input percents mean nothing.

But in PHP they say to NAME all checkboxes the same -- name="choice[]" -- square brackets puts them in an array, and the value is the name.  This method has problems, if nothing is checked, PHP returns a visible error, which is unacceptable (No return to the page, it all gets emailed to me).

WHAT I WOULD LIKE TO DO IS -- keep my original plan of names, and find a way in PHP to easily loop through all the checkboxes and related % inputs, finding only the ones with value=1, and email those results and percents as above.

The logic of how best to handle these checkboxes in PHP and working PHP code is my main concern here.
Can anyone help withPHP code to do it?   I've read most relevant links, so just ideas and code, please!  
Avatar of und3ath
und3ath

can you please post your code?
Avatar of scrathcyboy

ASKER

"Can anyone help withPHP code to do it?"
That's what I am looking for, the PHP code to do the above...
how you can loop through chboxes in php when you dont know their names? in php you will get array of values ($_GET or $_POST) which contains only variable names and their values in previously submited form.

You should name they for example: ch_0, ch_1,...., textboxes: t_0,t_1,... and then loop:
for($x=0;$x<$ch_count;$ch++)
     if($_POST[ch_.$x]==1)
         $text_to_mail.="Chbox ch_$x has value:${t_$x}.' ';
correction:
$text_to_mail.="Chbox ch_$x has value:".${t_$x}.' ';
SOLUTION
Avatar of jopie916
jopie916

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"how you can loop through chboxes in php when you dont know their names?"
"Are the checkbox values unknown or generated "on the fly"? "

All the checkboxes are named in the form -- abc, bbc, cbc, whatever.  In my plan they all have different names, rather than giving all the same "name[ ]"  and then having an array in PHP that could be empty.

jopie916, your suggestion looks good, but I read that if the checkboxes are not checked, they don't even get submitted to PHP -- only the checked ones do -- however, I find this hard to believe !

See hielo's discussion here, it does not answer my question, but it gives the basic code if all checkboxes have the SAME name, and if you use the value as an identity (but then I can't check them in javascript)

https://www.experts-exchange.com/questions/23191137/how-to-get-check-box-array-value-in-php-file.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, but I don't need to know the checked count.  As I said in the question, IF a checkbox is checked, then the NEXT field in the form is its relevant input box, and I need to associate that with the check box --

so if form element [0] is checked, then I need to get form element [1], the input value for that item;
if form element [4] is checked, I need to get form element [5] , the input value for that item .... etc.

They all have names, if I cannot address them in PHP by the position of the element in the form.  Can you adjust your code to get the associated input box value for each checked box ?  and output it like --

$msg = "The box " . $ckbx1 . " was checked, and the percent value of it is " . $input1;

There are no "groups" of my checkboxes, they all have names, so just use any name as an example.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that looks about right xakem, let me plug it in and get back with you ....
xakem -- your code worked GREAT !!  Most of all, it taught me that you don't have to pre-declare variables in PHP -- you can pull them right out of the POST values without wasting code on pre-declared variables.  That is a REAL plus.  I am giving some points to jopie, as he did some of this first, but your solution was A +++

Thanks.
Thanks for good working code xakem
I think my solution that I posted up is much more better and nicer. xakem wrote the same that I wrote
scrathcyboy,
I'm glad I could help you!
sorry und3ath -- I did not understand yours, perhaps it was the same, but for each test, I needed to print out a different text anyway, so it was not conducive to a loop.