Tick Box, items into an Array for a Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Tick Box, items into an Array for a Variable

Post by simonmlewis »

Didn't know how to describe this query, but here goes.

I have a database table full of email addresses.
I want to be able to tick 'check boxes' by each address, so I can choose who gets emailed when I hit Submit at the end.

I've seen many times where people have a page where you click the Check Boxes - for example, phpmyAdmin MySQL screen where you can delete rows at a time.

How is this done?

Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Tick Box, items into an Array for a Variable

Post by AbraCadaver »

I don't know how detailed an answer you need, but in general: The checkboxes could be an array and each one would have an email address as its value. When the form is posted you'd loop through the checkbox array get the values to add to your mail function.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Tick Box, items into an Array for a Variable

Post by simonmlewis »

Mmmmmm not really sure how to do that.
I need to collate each entry's ID so that they have a "unsubscribe" link which would place their ID into the URL (feedback back to the web site and doing an automatic delete - that bit I can do).

But I've no idea how to "loop through" and get it all passed from the list, or indeed how to name the checkboxes as I have seen namehere[] style used. Not sure what that means.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Tick Box, items into an Array for a Variable

Post by AbraCadaver »

The variable[] syntax creates an array just like in PHP. The empty brackets [] create the next array element, variable[0], variable[1], etc... So you would have something like this (assuming you are looping through DB results):

Code: Select all

<input type="checkbox" name="email[]" value="me@example.com" />
<input type="checkbox" name="email[]" value="you@universe.net" />
etc...
Then to process it:

Code: Select all

foreach($_POST['email'] as $email) {
    //do something with the email address
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Tick Box, items into an Array for a Variable

Post by simonmlewis »

Sooo..

Code: Select all

<input type="checkbox" name="email[]" value="$row->email"  />
Thing is, I don't want to turn over the PHP mail script 200 times in a turn - I want to put them all........ hold on. I'd have to if I was putting the unsubscribe URL in, otherwise it just wouldn't do it.

I was thinking of putting the Emails into a BCC, but can't. I'd have to have it run the PHP Mail like 200 times or however many ticks I selected.

Sound about right?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Tick Box, items into an Array for a Variable

Post by AbraCadaver »

I'm not sure I'm following you, but the BCC option is doable. This builds one long BCC header for one mail call. Not sure if there is a length limit:

Code: Select all

$bcc = "Bcc: " . implode(',', $_POST['email']) . "\r\n";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Tick Box, items into an Array for a Variable

Post by simonmlewis »

I need to find a way to enter something into a database so when we DO send the emails out, they get marked as sent on that date, so when we go to select say, 100 more, it doesn't show those we have just sent.

Basically we are trying to build our own Email Marketing system from our rather large database of email addresses.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Tick Box, items into an Array for a Variable

Post by simonmlewis »

When I run the page, with this in it, the page doesn't ever load anything.
If I remove this section of code, the page runs. This code has an error somewhere, but I just cannot see it.

Can you?

Code: Select all

if ($update == 'go' && $status == 'live') 
{
  foreach($_POST['id'] as $id) 
    {
    mysql_query ("UPDATE usercomments SET status = '$status' WHERE id = '$id'");
$result = mysql_query ("SELECT email, prodname FROM usercomments WHERE id = '$id'");
    while ($row = mysql_fetch_object($result))
      {
      if ($row->email != NULL) 
        {
       $emailsent = "yes";
       $to = "$row->email";
       $subject =  "Comment Added";
       $headers = "From: noreply@site.co.uk";
       $body = "** THIS EMAIL HAS BEEN AUTOGENERATED.  IT IS NOT A MONITORED ADDRESS. **
 
We thought you would like to know that your comment on the $row->prodname has been made live.
 
Check out the latest stock on our STOCK page, linked at the top of the site.
 
Best wishes
";
      mail ($to, $subject, $body, $headers);
        }
      }
      mysql_free_result($result)
    }
echo "<div class='admincompletedbox'><b>Those comments were made live.</b>";
    if ($emailsent == "yes") { echo "&nbsp; An email was sent - user supplied address.";}
    echo "</div>";
}  
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply