Reputation: 157
I have a list of three option values in a table. 'Approved' 'Pending' and 'Disapproved'. When a user selects an option I want a different email to be send to that person. I have another field name 'Email Address' on the same row (next to the drop down box). I was thinking I could capture the specific email address in the cell and send an email based on the selection. Would this be achieved by using an If else statement for each option value and then sending a specific mailto to the email address in the table? or would it be better to have a submit button which sends the email once the choices have been selected?
Upvotes: 1
Views: 2956
Reputation: 4307
It's almost always better to have a submit button once the choices have been selected. This gives people a chance to be sure about their action before it's taken for them. For instance, they may accidentally click the wrong drop down choice, and want to change it. It's just better to let the drop down be the action for making the choice, and the submit button be the action for sending the email. This also avoids user confusion (confuse the user, and they'll never want to use your application again, guaranteed).
But anyway, if you want to send an email when the drop down value is selected, you'll need to do it in JavaScript. This is because you're dealing with a front end control, and performing such an action isn't typical behavior for a select HTML element.
I'll show you how to do it in jQuery using AJAX, since it's easy and straightforward (http://jquery.com)
<script type="text/javascript">
$('#email-dropdown').on('change', function() {
$.ajax({
url: <some_file_that_handles_emailing>,
type: 'post',
dataType: 'json',
data: { 'selected': $(this).val() },
success: function(response) {
alert(response.msg);
}
});
);
</script>
<select id="email-dropdown">
<option value="accepted">Accepted</option>
<option value="pending">Pending</option>
<option value="disapproved">Disapproved</option>
</select>
That'll work to send an email once the value is changed. Also, I didn't specify a default value, because if you have a default selection then there won't be a "change" event for that value, and the user wouldn't be able to trigger the email unless he clicked another value first and then changed back (thus triggering two emails).
If you want to have a submit button instead:
<form id="email-form" action="<some_file_that_handles_emailing>">
<select id="email-dropdown" name="email-dropdown">
<option value="accepted">Accepted</option>
<option value="pending">Pending</option>
<option value="disapproved">Disapproved</option>
</select>
<input type="submit" value="Send Email" />
</form>
And that's it!
Upvotes: 2
Reputation: 29434
I would recommend a submit button.
What if the user selected the wrong option? You would then send two emails!
Also, if you don't have a submit button, you will have to call an AJAX request in order to communicate with PHP.
Upvotes: 1