...

View Full Version : Retrieve All Unknown Post Data



Pennimus
11-16-2006, 11:45 PM
Stay with me 2 seconds, a little background: I have a script which retrieves a list of words from a database and puts them into an array called $tags. I then use the count function to count them and echo them out in a form, where each individual word is assigned a checkbox.



<?php for ($i=1; $i < $numtags; $i++) { ?>
<li>
<input
type="checkbox"
name="<?php echo $tags[$i];?>"
value="<?php echo $tags[$i];?>"
/>
<?php echo trim($tags[$i]);?>
</li>
<?php } ?>


When the form is submitted I do of course need to establish which checkboxes have been selected.

Unfortunately since I have no way of knowing which tags actually exist beforehand (as they are created by the user) a problem is thrown up. How to check for post data when you don't know what it will be called?

My initial idea was to run through the same for loop again, assigning each tag to a new array if it happens to have been selected:



for ($i=1; $i < $numtags; $i++) {
if ($_POST[$tags[$i]] == $tags[$i]) {
$tagarray .= $array[$tags[$i]];
}
}


Although this script appears not to have any errors it is returning blank when I echo the array.

As such I've hit a brick wall. :confused: Has anyone got any idea how this can be achieved? Either as an elaboration on my method, or maybe there is a really simple way for PHP to retrieve all post data automatically?

firepages
11-17-2006, 12:16 AM
change your input tags into an array ....


<input type="checkbox" name="tags[<?=$i;?>]" value="<?=$tags[$i];?>" />

then...
<?
foreach($_POST['tags'] as $k=>$v){
echo "$k = $v <br />";
}
?>

Pennimus
11-17-2006, 12:32 AM
Thanks for your attempt, unfortunately it's still turning up a blank page.

The checkboxes are now named tags1, tags2, tags3 etc...

I've reduced the processing script to just your foreach loop to ensure nothing else is interfering with it and it's still blank.

And before you ask, yes I did select some checkboxes before running the script :thumbsup:

I tried using the GET method on the form purely to see if the data was being sent, this gives ?tags1=Word1&tags2=Word2, so it's being sent okay.

Any other ideas?

firepages
11-17-2006, 06:29 AM
no not tags1 tags2 etc

tags[1] tags[2] , this creates an array called $_POST['tags'] which the foreach loop reads

So the HTML output of your form should look something like ...
<input type="checkbox" name="tags[1]" value="tagvalue1" />tagvalue1
<input type="checkbox" name="tags[2]" value="tagvalue2" />tagvalue2

Pennimus
11-17-2006, 03:01 PM
Apologies, I missed out the [ ]

Works fine now thanks :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum