Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#155459 - 15/04/2003 10:43 Forms Question
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
I need an algorithm.

Say I have a form. The form is merely a list of checkboxes next to people's names. Those who are checked will receive an email.

Okay, so I know how to output all these names from my database and I can put their email addresses in the name field of the checkboxes, but I need a way to connect these boxes and the mail() function that will be carried out once the boxes are checked and the email form filled. I guess I just need to know how to put all the selected addresses into a string. Any advice on how to do that?
_________________________
Matt

Top
#155460 - 15/04/2003 11:25 Re: Forms Question [Re: Dignan]
Anonymous
Unregistered


Alright I just took a crack at it. I haven't tested it, so there could be a bug in it. I wasn't aware of the mail() method in javascript, but I guess it is just a custom function? Anyways, I think this is what you're looking for.

function whatever() {


for(x = 0; x < document.name_of_form.elements.length; x++) {
if (document.name_of_form.elements[x].type == 'checkbox') {
if (document.name_of_form.elements[x].value == 1) {
mail(document.name_of_form.elements[x].name);
}
}
}

Top
#155461 - 15/04/2003 12:29 Re: Forms Question [Re: ]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
Sorry, I really should have mentioned that I was doing this in PHP
_________________________
Matt

Top
#155462 - 15/04/2003 16:10 Re: Forms Question [Re: Dignan]
David
addict

Registered: 05/05/2000
Posts: 623
Loc: Cambridge
In the form, avoid putting the email address in the name or value tags of the checkbox, as there is the possibility of someone using your script to send emails to unintended recipients. Use the ID from the database to identify the recipients.
<input type="checkbox" name="<?=$result['id']?>" value="1">
In the script that handles the form, call the list of email addresses again and use a while loop to progress through them. Check if the box was checked and then build a long comma-separated string to put in the mail command.
if($$result['id']==1) {

$to.=$result[name].' <'.$result['email'].'>, ';
}
The trailing comma and space on that string might cause a problem, so trim it:

$to=substr($to, 0, -2);
Then call the mail command:
mail($to, 'subject', 'body'); 

Top
#155463 - 15/04/2003 18:21 Re: Forms Question [Re: David]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
Once again, thanks David. That makes more sense than the stuff I was trying to do. I'm having problems though.

This is in a section that's not open to the public, so it doesn't really matter if the addresses are in there, but I'm doing it your way anyway.

Anyway, the problem is that the loop seems to find false for every entry in the database. I had it echo somethng if the if statement failed, and I got one for every entry. I think I have my variables mixed up somewhere.
_________________________
Matt

Top
#155464 - 15/04/2003 18:59 Re: Forms Question [Re: Dignan]
David
addict

Registered: 05/05/2000
Posts: 623
Loc: Cambridge
First thing to check: in 'if($$result['id']==1)' the double dollar is intentional.

Top
#155465 - 15/04/2003 19:46 Re: Forms Question [Re: David]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
I was about to write a long reply with the code that seemed to be giving me trouble, and in the process of pasting it in here, I noticed that the variable name in the checkboxes wasn't the same that I was using in the mail function. D'OH!

Now it works again.

David, I am in your debt. You've helped me out so much. I'll make sure everyone involved knows I got some great help with this

This stuff is just so cool. I hated the last programming I did. For some reason PHP is more fun to do. Now to learn more....
_________________________
Matt

Top
#155466 - 15/04/2003 19:59 Re: Forms Question [Re: David]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
Actually, I have one last question for this application:

How do I make one of those checkboxes that will check other checkboxes? You know, sometimes there will be a box at the top that, when checked, will check all the boxes beneath it.

Can this be done with PHP?
_________________________
Matt

Top
#155467 - 16/04/2003 00:50 Re: Forms Question [Re: Dignan]
David
addict

Registered: 05/05/2000
Posts: 623
Loc: Cambridge
Unless you want to refresh the page or have a 'send to all' checkbox that doesn't select all the other checkboxes for you, that's a javascript thing. Set an onclick for the checkbox that controls them all that goes and selects the rest.
function toggleAll()

{
for(var i=0; i<document.myform.elements.length; i++)
document.myform.elements(i).checked=document.myform.tog.checked;
}
The BBS markup is being a nuisance - the (i) above should be square brackets.HTML for the master toggle checkbox:
<input type=checkbox name=tog onclick="javascript:toggleAll();">

Top
#155468 - 16/04/2003 09:36 Re: Forms Question [Re: David]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
So this will toggle all the checkboxes on the page? I suppose that's fine. Is there a way to make it check a certain group of boxes?

I'll try this out now, thanks!
_________________________
Matt

Top
#155469 - 16/04/2003 09:43 Re: Forms Question [Re: David]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
Hmmm. Doesn't seem to be working. I get an error saying that an object is expected, and it's on a line in the form section where the PHP code starts printing its part of the form.

*edit*
I forgot to put script tags around the script. It goes in the head, right? well now I'm still getting errors that say "document.myform.elements is null or not an object"


Edited by DiGNAN17 (16/04/2003 10:14)
_________________________
Matt

Top
#155470 - 16/04/2003 10:15 Re: Forms Question [Re: Dignan]
JBjorgen
carpal tunnel

Registered: 19/01/2002
Posts: 3584
Loc: Columbus, OH
replace myform with the name of the form you are working with
_________________________
~ John

Top
#155471 - 16/04/2003 10:30 Re: Forms Question [Re: JBjorgen]
Dignan
carpal tunnel

Registered: 08/03/2000
Posts: 12341
Loc: Sterling, VA
Of course! Thank you for pointing that out. That was dumb.

It works now
_________________________
Matt

Top