Page 1 of 1
Form data
Posted: Tue Jan 20, 2009 5:23 am
by lyleyboy
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
Re: Form data
Posted: Tue Jan 20, 2009 5:41 am
by mattpointblank
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
Posted: Tue Jan 20, 2009 6:56 am
by lyleyboy
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
Re: Form data
Posted: Tue Jan 20, 2009 7:35 am
by lyleyboy
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.
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>
Form2.php code
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'");
}
?>