Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts

    Retrieve All Unknown Post Data

    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 Code:
    <?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:

    PHP Code:
    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. 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?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    change your input tags into an array ....
    PHP Code:
    <input type="checkbox" name="tags[<?=$i;?>]" value="<?=$tags[$i];?>"  />

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

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    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

    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?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Apologies, I missed out the [ ]

    Works fine now thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •