简体   繁体   English

在html onclick中运行javascript和php

[英]run javascript and php in html onclick

I have a dropdown list in HTML, with element name staff1 , containing names fetched from the database. 我在HTML中有一个下拉列表,元素名称为staff1 ,包含从数据库中获取的名称。 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. 现在,我知道HTML和JavaScript在客户端,而PHP在服务器端。 With my js, I can fetch in real-time the selected value in the dropdown. 使用我的js,我可以实时获取下拉菜单中的选定值。 I came up with below to be able to fetch the selected value via javascript and pass it to PHP for the email function. 我提出以下内容,以便能够通过javascript获取选定的值,并将其传递给PHP以获得电子邮件功能。 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. 我需要在html的onclick事件中将所有这些调用为href按钮。 Here is my html: 这是我的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. 我不确定这三者如何能够集成在一起,我仍然打算通过AJAX学习,就像其他一些帖子所建议的那样。 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. 我跌倒了,但是它什么也没发送,即使我用静态值临时定义了$ to参数。 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);
?>  

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 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);
?>

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. 因此,邮件服务器可能返回错误,告诉您用HTML包装的JavaScript代码块与电子邮件地址之间的距离并不遥远。


You have two options. 您有两个选择。 Either post the form value to a page or use AJAX to make the request to the server. 将表单值发布到页面上,或使用AJAX向服务器发出请求。 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: 发布表单:使用这种方法,您的客户端HTML可能具有以下内容:

<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. 单击该提交按钮会将该电子邮件值发布到somePage.php ,该代码将具有使用该值的代码。 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. 例如,您可能有一个hidden输入而不是text输入,并且可能从JavaScript动态填充了该hidden输入。

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. 该页面将执行您需要的服务器端操作,或者直接为用户提供新的UI响应,或者将用户重定向到另一个页面。 This, as you can imagine, involves reloading the entire page. 您可以想象,这涉及到重新加载整个页面。

AJAX: But what if you don't want to reload the entire page? AJAX:但是,如果您不想重新加载整个页面怎么办? 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。 AJAX is basically using JavaScript to make a request to a page in the background. AJAX基本上是使用JavaScript在后台向页面发出请求。 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. 本质上,您将使用JavaScript将点击处理程序附加到按钮或链接(或实际上是任何元素),并且JavaScript会对执行该操作的PHP页面发出AJAX HTTP请求。 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. 所请求的PHP页面将执行操作并做出响应,最好使用JSON数据而不是HTML。 The client-side code would then react to that response, possibly showing the user a success or failure message. 然后,客户端代码将对该响应做出反应,可能向用户显示成功或失败消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM