0

I have a dropdown list in HTML, with element name staff1, containing names fetched from the database. I need a Send Email button beside the list and once the button is clicked, an email must be sent to the selected option in the list before even submitting the page or form.

Now, I understand that HTML and JavaScript are on the client side, and PHP is on the server side. With my js, I can fetch in real-time the selected value in the dropdown. I came up with below to be able to fetch the selected value via javascript and pass it to PHP for the email function. All of these are in the same file.

<script>
    function sendEmail()
    {
        var val = document.getElementByName("staff1").value;
    }
</script>

<?php
    $to = "<script>document.writeln(val);</script>";
    $subject = "This is a test email";
    $txt = "test body"; 
    $headers = "From: aa@123.com";
    mail($to,$subject,$txt,$headers);
?>

I would need to call all these in the onclick event of my html a href button. Here is my html:

<a href="#" onclick="sendEmail()" class="button">Send Email</a>

I'm not so sure how all of these three can be integrated together, and I'm still about to learn through AJAX as some other posts suggest. For now, I was hoping a quick solution or workaround would do. I got below but it's not sending anything, even when I temporarily define the $to parameter with a static value. Thanks!

<?php
    echo '<a href="#" onclick="sendEmail()" class="button">Send Email</a><br/><br/>';
    echo '<script> function sendEmail() { var val = document.getElementByName("staff1").value; } </script>';
    $to = "<script>document.writeln(val);</script>";
    $subject = "This is a test email";
    $txt = "test body"; 
    $headers = "From: aa@123.com";
    mail($to,$subject,$txt,$headers);
?>  
Lane
  • 366
  • 1
  • 2
  • 11
  • There's no quick solution or workaround for learning AJAX. That's what you *have* to learn to make this work. PHP only runs on the server-side, Javascript runs on the client-side. So you need to make a request to the server (via AJAX, in this case) to run any PHP code. – Mike Cluck Mar 02 '16 at 18:07
  • 1
    Got it. Will do as suggested. Thanks, @MikeC! – Lane Mar 02 '16 at 18:08
  • Once page has loaded , you can't use php. You can do that using ajax request – AliN11 Mar 02 '16 at 18:09

2 Answers2

1

I tried to give quick solution. Try it .

<script>
function sendEmail(){
var to = document.getElementByName("staff1").value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
  // // 
}
};
xhttp.open("GET", "send_mail.php?to="+to, true);
xhttp.send();
}
</script>

send_mail.php

<?php
$to = $_GET["to"];
$subject = "This is a test email";
$txt = "test body"; 
$headers = "From: aa@123.com";
mail($to,$subject,$txt,$headers);
?>
Hiren patel
  • 971
  • 8
  • 25
  • It's probably worth at least *mentioning* that your solution requires loading an entirely separate library that the OP currently makes no mention of using. (And is kind of overkill just for a single AJAX request.) – David Mar 02 '16 at 18:24
  • In your answer , you also mentioned to use Ajax. I am not getting you point , how this is kind of overkill just for a single AJAX request. – Hiren patel Mar 02 '16 at 18:29
  • In my answer I made no mention of jQuery. In your answer you assume the use of jQuery, where no such use was ever mentioned in the question. What happens when someone tries this and gets an error that `$` is undefined? And yes, loading *the entire jQuery library* is overkill just to make a single AJAX request. That's like bringing an entire toolbox when all you need is a thumbtack. – David Mar 02 '16 at 18:32
  • Now , is it fine ? no use of jquery library . – Hiren patel Mar 02 '16 at 18:37
  • Hi @hirenpatel. In the above solution that you presented, is the javascript function sendEmail() called from the html file similarly using the same onclick event? I am still not getting any email, though. Or should I place "send_mail.php" under href? Thanks! – Lane Mar 02 '16 at 20:30
  • It is called on onclick event .you need to place send_mail.php under same directory as your current HTML file. I have given solution according to info you provided . – Hiren patel Mar 03 '16 at 02:17
  • http://stackoverflow.com/a/8804171/3553279 You need to setup SMTP to send mail using mail() function. Check your configuration . – Hiren patel Mar 03 '16 at 02:53
0

but it's not sending anything

That's because this isn't an email address:

"<script>document.writeln(val);</script>"

So the mail server is probably returning an error telling you that a block of JavaScript code wrapped in HTML isn't anything remotely close to an email address.


You have two options. Either post the form value to a page or use AJAX to make the request to the server. And both of these are, well, pretty broad for a single Stack Overflow question/answer. But let's see if I can at least describe the processes at a high level to help you out...

Post a Form: In this approach your client-side HTML might have something like this:

<form method="POST" action="somePage.php">
    <input type="text" name="email" />
    <input type="submit" value="Send Email" />
</form>

Clicking on that submit button would post that email value to somePage.php, which would have code to use that value. You still have a variety of options to customize this. For example, you might have a hidden input instead of a text input and you might populate that hidden input dynamically from JavaScript.

But ultimately what you would have is a form which posts to a page. That page would perform the server-side action you need and would either respond directly with a new UI for the user or would redirect the user to another page. This, as you can imagine, involves reloading the entire page.

AJAX: But what if you don't want to reload the entire page? What if you want the user to stay right here and not have to navigate around the site? That's fine, you'd use AJAX for that. AJAX is basically using JavaScript to make a request to a page in the background. It's a bit more complex than the form post, but from the perspective of the server-side code the functionality is the same.

There are tons of examples online, this one seems like a reasonable place to start. Essentially you'd use JavaScript to attach a click handler to a button or link (or any element, really), and that JavaScript would make an AJAX HTTP request to the PHP page which performs the action. That request can include the information gathered from the client-side input. The PHP page being requested would perform the action and respond, ideally with perhaps JSON data instead of HTML. The client-side code would then react to that response, possibly showing the user a success or failure message.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • Hi @David. That was enlightening, thanks! To clarify, I mentioned in my post that the dropdown is a list of names. There is an additional process there of fetching email addresses from an SQL table based on their names, which would be easy to do. I did try hard-coding the value of variable **val** with a correct and valid email address to try if sending email works. I'm certainly looking at AJAX since I don't want to reload the entire page, given that there are other fields to be edited by the user after clicking on the send email button. I have also implemented posting a form in other pages. – Lane Mar 02 '16 at 18:22