在html onclick上运行javascript和php

我在HTML中有一个下拉列表,其元素名称为 staff1 </ strong>,其中包含从中获取的名称 数据库。 我需要列表旁边的“发送电子邮件”按钮,单击该按钮后,必须先将电子邮件发送到列表中的选定选项,然后再提交页面或表单。 </ p>

现在,我知道HTML和JavaScript都在客户端,而PHP则在服务器端。 使用我的js,我可以在下拉列表中实时获取所选值。 我在下面想出了能够通过javascript获取所选值并将其传递给PHP以获取电子邮件功能。 所有这些都在同一个文件中。</ p>

 &lt; script&gt; 
function sendEmail()
{
var val = document.getElementByName(“staff1”)。 值;
}
&lt; / script&gt;

&lt;?php
$ to =“&lt; script&gt; document.writeln(val);&lt; / script&gt;”;
$ subject =“这是 测试电子邮件“;
$ txt =”test body“;

$ headers =“From:aa@123.com”;
邮件($ to,$ subject,$ txt,$ headers);
?&gt;
</ code> </ pre>
\ n

我需要在我的html a href按钮的onclick事件中调用所有这些。 这是我的html:</ p>

 &lt; a href =“#”onclick =“sendEmail()”class =“button”&gt;发送电子邮件&lt; / a&gt; 
< / code> </ pre>

我不太确定如何将这三者集成在一起,我还要通过其他一些帖子建议的AJAX学习。 目前,我希望快速解决方案或解决方法。 我得到了下面但它没有发送任何东西,即使我暂时用静态值定义$ to参数。 谢谢!</ p>

 &lt;?php 
echo'&lt; a href =“#”onclick =“sendEmail()”class =“button”&gt;发送电子邮件&lt; / a&gt;&lt; br /&gt;&lt; br /&gt;';
echo'&lt; script&gt; function sendEmail(){var val = document.getElementByName(“staff1”)。value; }&lt; / script&gt;';
$ to =“&lt; script&gt; document.writeln(val);&lt; / script&gt;”;
$ subject =“这是一封测试电子邮件”;
$ txt = “测试体”;

$ headers =“From:aa@123.com”;
邮件($ to,$ subject,$ txt,$ headers);
?&gt;

</ code> </ pre>
</ div>

展开原文

原文

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

dshp9580656
dshp9580656 页面加载后,你不能使用PHP。你可以使用ajax请求来做到这一点
4 年多之前 回复
duanbo7517
duanbo7517 得到它了。会按照建议做。谢谢,@MikeC!
4 年多之前 回复
douyi1939
douyi1939 学习AJAX没有快速的解决方案或解决方法。这就是你必须学会​​使这项工作。PHP仅在服务器端运行,Javascript在客户端运行。所以你需要向服务器发出请求(在这种情况下通过AJAX)来运行任何PHP代码。
4 年多之前 回复

2个回答



我试图给出快速解决方案。 试试吧。</ p>

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

send_mail.php </ p>

 &lt;?php 
$ to = $ _GET [“to”];
$ subject =“这是一封测试邮件”;
$ txt =“测试体”;

$ headers =“From:aa@123.com”;
mail($ to,$ subject,$ txt,$ headers);
?&gt;
</ code> </ pre>
</ DIV>

展开原文

原文

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

dotcraq3249
dotcraq3249 stackoverflow.com/a/8804171/3553279您需要设置SMTP以使用mail()函数发送邮件。 检查您的配置。
4 年多之前 回复
duandan5471
duandan5471 它在onclick事件上调用。你需要将send_mail.php放在与当前HTML文件相同的目录下。 我根据您提供的信息给出了解决方案。
4 年多之前 回复
dousi1970
dousi1970 嗨@hirenpatel。 在您提供的上述解决方案中,是否使用相同的onclick事件从html文件调用的javascript函数sendEmail()? 不过,我仍然没有收到任何电子邮件。 或者我应该在href下放置“send_mail.php”? 谢谢!
4 年多之前 回复
doucang2831
doucang2831 现在,好吗? 没有使用jquery库。
4 年多之前 回复
doumiebiao6827
doumiebiao6827 在我的回答中,我没有提到jQuery。 在你的答案中,你假设使用jQuery,在问题中没有提到过这样的用法。 当有人尝试这个并且得到$未定义的错误时会发生什么? 是的,加载整个jQuery库只是为了发出一个AJAX请求。 这就像带来一个完整的工具箱,当你需要的只是一个图钉。
4 年多之前 回复
duanjue2560
duanjue2560 在您的回答中,您还提到了使用Ajax。 我不是说你的意思,这对于单个AJAX请求来说是多么的过分。
4 年多之前 回复
dragon87836215
dragon87836215 至少值得一提的是,您的解决方案需要加载一个完全独立的库,OP目前没有提及使用它。 (并且对于单个AJAX请求来说有点过分。)
4 年多之前 回复

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.

duanan6043
duanan6043 嗨@David。 这很有启发性,谢谢! 为了澄清,我在帖子中提到下拉列表是一个名单。 还有一个额外的过程是根据它们的名称从SQL表中获取电子邮件地址,这很容易做到。 我尝试使用正确有效的电子邮件地址对变量val的值进行硬编码,以尝试发送电子邮件。 我肯定在看AJAX,因为我不想重新加载整个页面,因为用户在点击发送电子邮件按钮后还有其他字段需要编辑。 我还实现了在其他页面中发布表单。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐