checkbox - how to read values for correponding names?
Posted: Fri Jun 19, 2009 2:20 pm
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.
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
It doesnot say which particular check boxes have been clicked, does it?
Ideal case would be to have something similar to this
So, upon form submission I can process the form data and update my database accordingly.
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?
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>
So, if I do
Code: Select all
foreach($_POST['chk']) as $checkbox){
...code here to print values...(no/yes)
}
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">
Code: Select all
$_POST['news_title'];
$_POST['news_des'];
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?