SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Wellington, NZ
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Associative array and checkboxes

    Hi,

    I've created a list of email addresses from my database and I want to add a checkbox next to each email address to be able to subscribe or unsubscribe them from a mailing list.

    I've successfully created the list and checkboxes (checked if they're subscribed otherwise unchecked), but I'm stuck on how to create the correct array and process it.

    My form looks something like this:

    HTML Code:
    <form action="newsletter.php" method='post'>
      <table class='admin'>
       <tr>
        <th>Name</th>
        <th>Username</th>
        <th>Email</th>
        <th>Subscribed</th>
       </tr>
    
       <tr>
        <td>berts company</td>
        <td>bert</td>
        <td>bert@bert.com</td>
        <td><input type='checkbox' name='row[bert]' value='0' /></td>
       </tr>
    
       <tr>
        <td>Richard<td>
        <td>jetskirich</td>
        <td>rich@test.com</td>
        <td><input type='checkbox' name='row[jetskirich]' value='1' /></td></tr>
    
    </table>
    <input type='submit' value='Submit' />
    </form>
    I think i'm creating an associative array with the username (row[bert]) as the key and it's associated value (0 in this case as unsubscribed).

    With this in mind here's my php code to insert the values into the database:

    PHP Code:
    $row $_POST['row']; 
      
     foreach (
    $row as $username => $newsletter) {
     
      
    $sql mysql_query("UPDATE users SET
                        newsletter = '
    $newsletter'
                        WHERE username = '
    $username'
                        "
    );
      } 
    I get an error message saying Warning: Invalid argument supplied for foreach() in newsletter.php on line x.

    Can anyone point me in the right direction? Am I going about this the right way?

    The second problem is that if i want to unsubscribe an email address, then when a checkbox is unchecked, will the value of 0 be passed or will it be ignored?

    Thanks

    Richard.

  2. #2
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Before your foreach, have you tried this:

    echo "<pre">;
    print_r($_POST);
    echo "</pre">;

    It will let you see what data you're really working with. I'm pretty sure the checkbox form elements don't work as you think they do.

    Give it a shot and it should give you the information you need to get over this hump
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Wellington, NZ
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it seems as though i've made a right pigs ear of this, the result I get from printing $_POST['row'] is

    Code:
    Array
    (
        [row] => Array
            (
                [bert] => 0
                [jetskirich] => 1
            )
    
        [submitnewsletter] => Submit
    )
    and it ignores the fact that i've unchecked another checkbox (as i thought).

    I've tried a few different things, but i'll still have the same problem where checkboxes are unchecked where they were checked originally. Is there a better angle I can attack this from?

    Richard.

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    <input type='checkbox' name='row[bert]' value='0' />

    should be

    <input type='checkbox' name='row[bert]' value='1' />

    I would think..

    If its 0, even if its checked or unchecked it'll be 0.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Wellington, NZ
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, yes, how obvious was that! The values were taken from my database where another form had inserted the users, a value of 0 being used for not subscribed, and 1 for subscribed. The checkboxes are checked depending on whether this subscribed value was greater than 0.

    This could easily be changed to 2 for not subscribed I suppose.

    I really don't know where to go from here, if a box isn't checked, then no variables are passed, so this isn't going to work for unsubscribing emails.

    Any ideas how I can start again?

  6. #6
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The "value=x" of a checkbox determine the value of the checkbox.

    0 for unchecked.

    x for checked.

    It has no relevance to what its representing, that's up to you as a coder to determine that.

    Therefore this is how you would want to go about handling this.

    HTML Code:
    <input type='checkbox' name='row[bert]' value='1'  <?php if ($row['bert']) echo "checked='checked'; ?> />
    This means when the form is posted, if its checked its 1 - which can be subscribed, or 0 if its not checked, which can mean ununsubscribed.

    When you pull its value(1 or 0) out of the db, the checkbox's "value=1" has no relation to the value from db (1 or 0). that relation is between (1 or 0) and "checked" and unchecked

    Hope that makes sense.


    Maybe an example is easier to understand.


    PHP Code:

    <form action="newsletter.php" method='post'>
      <table class='admin'>
       <tr>
        <th>Name</th>
        <th>Username</th>
        <th>Email</th>
        <th>Subscribed</th>
       </tr>

       <tr>
        <td>berts company</td>
        <td>bert</td>
        <td>bert@bert.com</td>
        <td><input type='checkbox' name='row[bert]' value='1' <?php if ($row['bert']) echo "checked='checked'"?> /></td>
       </tr>

       <tr>
        <td>Richard<td>
        <td>jetskirich</td>
        <td>rich@test.com</td>
        <td><input type='checkbox' name='row[jetskirich]' value='1' <?php if ($row['jetskirich']) echo "checked='checked'"?>/></td></tr>

    </table>
    <input type='submit' value='Submit' />
    </form>
    Note the value=x is always value=1, it can be any value but it's simply a on or off flag.

    now the checked='checked' actually determines if the checkbox will have 1 or 0.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Wellington, NZ
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, i think i've confused matters here a little.

    I simplified my code a little in the original post. Here's how i created my form:

    PHP Code:
    $res = mysql_query('SELECT username, name, email, newsletter FROM users ORDER BY username ASC');

     if (!$res) {
      echo '<p class="error">Error retrieving user and newsletter details: '.mysql_error().'</p>';
     }

    ?>
     <form class='newsletter' action='<?echo $self;?>' method='post'>
      <table class='admin'>
       <tr>
        <th>Name</th>
        <th>Username</th>
        <th>Email</th>
        <th>Subscribed</th>
       </tr>
    <?
     
    while (list($username$name$email$newsletter) = mysql_fetch_array($res)) {
      
      echo 
    "<tr><td>$name</td>";
      echo 
    "<td>$username</td>";
      echo 
    "<td>$email</td>";
       if (
    $newsletter 0) {
      echo 
    "<td align='center'><input type='checkbox' checked='checked' name='row[username]' value='$newsletter' /></td>";
       } else {
      echo 
    "<td align='center'><input type='checkbox' name='row[username]' value='$newsletter' /></td>";   
       }

      echo 
    "</tr>\n";
      
     }


    ?>
     </table>
     
    <input type='submit' name='submitnewsletter' value='Submit' />
    </form>
    Where $newsletter was taking the values of either 0 or 1 (subscribed or unsubscribed, i guess i could have made it n and y).

    I understand that the checkbox will only pass it's value to the array when it's checked, but am I passing those values in the right way, i.e. is name='row[username]' with value='$newsletter' the right way to create the array?

    Thanks

    Richard.

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Wellington, NZ
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have totally confused myself, with my above code, even if checkboxes are checked, they will only pass the variables I have already set them, which is stupid.

    I would be better off giving them values of 1 or 0 (or y or n), but then how do i get the unchecked boxes to pass 0 or n to unsubscribe users? I could set all values to n and then update all checked boxes to y, but if I have a long list and it's split into pages, that wouldn't be ideal.

  9. #9
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    change value='$newsletter' to value='1' and you're all set.

    If that checkbox is unchecked when its submitted, it will hold a value of 0, if its checked, it will hold a value of 1.

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Wellington, NZ
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, that works now, thanks for being patient with me.

    Richard.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    NL, Rotterdam
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems solved but anyway:

    If I understand the problem right, in these case I usually do something like:
    Code HTML4Strict:
    <input type="checkbox" name="subscribers[]" value="{$intUserId}" {$strChecked} /><label>{$strUserName}</label>

    Then in your post you have an array of subscriber-id where you can do whatever you want with...


Bookmarks

Posting Permissions

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