Forms Question

Posted by: Dignan

Forms Question - 15/04/2003 10:43

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?
Posted by: Anonymous

Re: Forms Question - 15/04/2003 11:25

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);
}
}
}
Posted by: Dignan

Re: Forms Question - 15/04/2003 12:29

Sorry, I really should have mentioned that I was doing this in PHP
Posted by: David

Re: Forms Question - 15/04/2003 16:10

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'); 
Posted by: Dignan

Re: Forms Question - 15/04/2003 18:21

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.
Posted by: David

Re: Forms Question - 15/04/2003 18:59

First thing to check: in 'if($$result['id']==1)' the double dollar is intentional.
Posted by: Dignan

Re: Forms Question - 15/04/2003 19:46

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....
Posted by: Dignan

Re: Forms Question - 15/04/2003 19:59

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?
Posted by: David

Re: Forms Question - 16/04/2003 00:50

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();">
Posted by: Dignan

Re: Forms Question - 16/04/2003 09:36

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!
Posted by: Dignan

Re: Forms Question - 16/04/2003 09:43

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"
Posted by: JBjorgen

Re: Forms Question - 16/04/2003 10:15

replace myform with the name of the form you are working with
Posted by: Dignan

Re: Forms Question - 16/04/2003 10:30

Of course! Thank you for pointing that out. That was dumb.

It works now