duanlian1960 2016-03-02 18:02
浏览 120

在html onclick上运行javascript和php

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);
?>  
  • 写回答

2条回答 默认 最新

  • douzhimao8656 2016-03-02 18:14
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler