Vidya Namasivayam

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromVidya Namasivayam
ToMe
SubjectNeed Information
Date03 June 2003 16:23
Sir,
 
I would like to know how a form can host multiple submit buttons in the
same form, each should function so that the user can choose whicever to
e-mail the form to different persons.
 
Thanking You,
 
Sincerely
Vidya
FromMe
ToVidya Namasivayam
SubjectRe: Need Information
Date04 June 2003 13:36
Forms can have as many submit buttons as you want. All of them will submit
the form. As with other types of inputs, submit buttons can also have a
name and value, so these can be used by a server side script to select the
correct recipient. For example:

<form action="sendTheMail.php">
	<p>
		<textarea name="message">
			Put the message body here ...
		</textarea>
	</p>
	<p>
		Send to:<br>
		<input type="submit" name="whoTo" value="person1@ourdomain.com"><br>
		<input type="submit" name="whoTo" value="person2@ourdomain.com"><br>
		<input type="submit" name="whoTo" value="person3@ourdomain.com"><br>
		<input type="submit" name="whoTo" value="person4@ourdomain.com">
	</p>
</form>

... now, in sendTheMail.php ...

$permittedRecipients = Array(
	'person1@ourdomain.com',
	'person2@ourdomain.com',
	'person3@ourdomain.com',
	'person4@ourdomain.com'
);
//MUST make sure they cannot send to recipients that you don't want them to!
if( $whoTo && in_array( $whoTo, $permittedRecipients ) ) {
	mail( $whoTo, 'Feedback', $message, 'From:someone@whatever.com' );
}


Of course, this is only an example. You could change this to suit your own needs, maybe:

<input type="submit" name="whoTo" value="Thomas">
<input type="submit" name="whoTo" value="Fred">

...

$permittedRecipients = Array(
	'Thomas' => 'person1@ourdomain.com',
	'Fred' => 'person2@ourdomain.com'
);
if( $whoTo && $permittedRecipients[$whoTo] ) {
	mail( $permittedRecipients[$whoTo], 'Feedback', $message, 'From:.....' );
}



If you need to do this without server side scripts like PHP, ASP, JSP, Perl
etc., you can also do something like this with JavaScript. It won't be
anything like as good as with server side scripts though, because it relies
on the browser supporting JavaScript, the browser supporting mailto: as a
protocol in forms, the user's email client being installed properly, the
email client understanding the use of subject and body, and the user still
has to click send in their email client, after being warned by the browser
that they might not want to. You might just as well let them click a mailto
link and let them fill it out there ...


<form action="mailto:theDefaultEmailAddress@ourdomain.com">
	<p>
		<input type="text" name="subject">
	</p>
	<p>
		<textarea name="body">
			Put the message body here ...
		</textarea>
	</p>
	<p>
		Send to:<br>
		<input type="submit" onclick="this.form.action='mailto:person1@ourdomain.com';"
			value="Thomas"><br>
		<input type="submit" onclick="this.form.action='mailto:person2@ourdomain.com';"
			value="Fred">
	</p>
</form>

NOTE: this technique is not very good in the real world.



Hope this helps

Tarquin
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.