Hi all,
I am fairly sure there is a way to do this but not certain.
How can I detect the data sent via $_POST if I am not sure what the field names will be?
The concept is that I'll have a lot of checkboxes with various names and I need to see what has been clicked. The names of the checkboxes will not be the same twice.
Thanks
Form data
Moderator: General Moderators
-
mattpointblank
- Forum Contributor
- Posts: 304
- Joined: Tue Dec 23, 2008 6:29 am
Re: Form data
You can loop through $_POST like you would an array, then assign the data to another array using the field keys?
Code: Select all
foreach($_POST as $key=>$value) {
echo "$key = $value";
}
Re: Form data
Superb, that works but it's not quite what I am after.
My form has 6 check boxes names are imaginatively enough they are 1-6. If I click 1 2 and 6 the value comes through as 6
My form has 6 check boxes names are imaginatively enough they are 1-6. If I click 1 2 and 6 the value comes through as 6
Re: Form data
Fixed it.
I have cheated outragously and the purists will hate me.
What I did was name my checkbox "check" then the rowID of the query. This means that instead of having 6 checkboxes all with the same name I have 6 uniquly named boxes.
I can the do the update through the for loop.
Posted my dirty code for others to use.
Form2.php code
I have cheated outragously and the purists will hate me.
What I did was name my checkbox "check" then the rowID of the query. This means that instead of having 6 checkboxes all with the same name I have 6 uniquly named boxes.
I can the do the update through the for loop.
Posted my dirty code for others to use.
Code: Select all
<?php
include('../inc.conn.php'); //Connect to DB
$tbl_name = "colours";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
echo "No of rows = " . $count;
?>
<form name="form1" method="post" action="form2.php">
<?php
while($rows=mysql_fetch_array($result)){
?>
<input name="checkbox<? echo $rows['id']; ?>" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>">
<?php
}
?>
<input type="submit">
</form>
</td>
</tr>
</table>Code: Select all
<?php
include('../inc.conn.php'); //Connect to DB
foreach($_POST as $key=>$value) {
echo "$key = $value";
echo "<br/>";
Echo $value;
mysql_query("DELETE FROM colours WHERE id='$value'");
}
?>