checkbox - how to read values for correponding names?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oliur
Forum Commoner
Posts: 29
Joined: Tue May 26, 2009 3:43 am

checkbox - how to read values for correponding names?

Post by oliur »

To keep it simple, I am posting a little snippet of an example code that I have written to figure out my problem. If anyone can help me with these questions I would be very grateful.

Code: Select all

 
 
if(isset($_POST['update'])){
for($i=1; $i<=$_POST['colCount'];$i++){
 
        echo "<br/> value for ".$i." is ".$_POST[$i];
    }
}
<form action="test.php" method="post">
 
<input type="hidden" name="1" value="no"/>
<input type="checkbox" name="1" value="news" />News
 
<input type="hidden" name="2" value="no"/>
<input type="checkbox" name="2" value="rss" />Rss
 
 
<input type="hidden" name="colCount" value="2"/>
<input type="submit" name="update" value="submit" />
 
</form>
 
1. In the above example you cant tell which checkboxes have been clicked. Even if checkboxes are listed as a group (i.e <input type="checkbox" name="chk[]".....) There is no way I can find out which checkboxes have been clicked and which haven't as all the checkboxes has the same name.

So, if I do

Code: Select all

 
foreach($_POST['chk']) as $checkbox){
...code here to print values...(no/yes)
}
 
It doesnot say which particular check boxes have been clicked, does it?


Ideal case would be to have something similar to this

Code: Select all

 
<input type="hidden" name="news_title" value="no">
<input type="checkbox" name="news_title" value="yes">
<input type="hidden" name="news_des" value="no">
<input type="checkbox" name="news_des" value="yes">
 
So, upon form submission I can process the form data and update my database accordingly.

Code: Select all

 
$_POST['news_title'];
$_POST['news_des'];
 
But in cases when you are not sure how many checkboxes are there gonna be (loaded from the db table) you cant use fixed name for those. So, the only option left is to use a group name. Am I right?

In this case, how do I update a database table based on mere values when I dont know which check boxes have been clicked (their name) ?

Do I make any sense?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: checkbox - how to read values for correponding names?

Post by Eric! »

I think I understan what you want. You want a form with a variable number of checkboxes as defined by a db. Then you want your script to handle the data.

Are you generating the form dynamically from the database? It would seem to me you could build your form with the checkboxes using field names from the database, then display it for the user. Your php script would then dump all the fields from the db table into an associated array and you could just loop through it to extract the values.
oliur
Forum Commoner
Posts: 29
Joined: Tue May 26, 2009 3:43 am

Re: checkbox - how to read values for correponding names?

Post by oliur »

I guess if I give you an example it would be easier for you to grasp what I am after:

http://www.pinedacovalin.co.uk/lab/1806 ... ration.php

This is working just fine, you have users and you can assign them access to different columns of a table. The columns names are ofcourse loaded from the database but think about these scenarios

1. For user Simon pedra, I am ticking in no 2 and 3 and leaving 1 and 4 unticked.

What would you do as a programmer? Your first job is to find out -
a. all the ticked values.
for each ticked values you
i. check user_grant table to see if those values have already been assigned to this user
ii. if no changes made, you dont do anything
ii. if an additional columns is granted for this user, you insert that value into the user_grant table for that user
iv. if any column is revoked, update the table accordingly.

And to do this you need to find out which values have been ticked and which are not ticked and update your user_grant table with every update made.

Makes more sense?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: checkbox - how to read values for correponding names?

Post by califdon »

What's the purpose of the hidden elements with the same name as the matching elements? I think that will cause a problem. Why can't you do as Eric! suggested, or even simpler, just name each checkbox like "chkbx1", "chkbx2", etc., which is easy to do in your PHP loop that generates the HTML, then similarly when you read the POST values. Maybe I don't see what you're trying to do.
oliur
Forum Commoner
Posts: 29
Joined: Tue May 26, 2009 3:43 am

Re: checkbox - how to read values for correponding names?

Post by oliur »

If I name it checkbox1, checkbox2 I can read which checkboxes are ticked.

But in that case how would you find out which checkboxes havenot been ticked?

That's where hidden values come in. if not ticked they give me "no".

I need to know which checkboxes are ticked and which are not.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: checkbox - how to read values for correponding names?

Post by Eric! »

I don't get it. Doesn't it just return null or notset if it isn't checked?
oliur
Forum Commoner
Posts: 29
Joined: Tue May 26, 2009 3:43 am

Re: checkbox - how to read values for correponding names?

Post by oliur »

no, it returns absolutely nothing. do a google and thats how i got this idea of using hidden values.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: checkbox - how to read values for correponding names?

Post by califdon »

That's true, but you just check isset(checkbox1). You don't need to go through all the extra hidden boxes.
Post Reply