简体   繁体   English

单击提交按钮后的php emailer链接

[英]php emailer link after submit button is clicked

How does one link to the submit button, I am thinking of embedding a or just using href or actually have the entire php emailer code embedded somehow inside the submit section? 我正在考虑嵌入一个或仅使用href的一个链接,或者实际上是将整个php emailer代码以某种方式嵌入到“提交”部分中的链接?

I can submit the information supplied by a user to a MySQL database and log it there; 我可以将用户提供的信息提交到MySQL数据库并在那里记录; however, I was just wondering if there is a more elegant way to send email using the information saved in my database by the user after clicking on submit, or would I have to just script some php emailer and use the code below to link to it, so that when the user clicks submit, I have a copy on my database and the other copy is sent to my email in box? 但是,我只是想知道是否有一种更优雅的方式来使用用户单击提交后使用用户保存在数据库中的信息来发送电子邮件,还是我只需要编写一些php emailer脚本并使用下面的代码链接到它即可,以便用户单击“提交”时,我的数据库上有一个副本,其他副本发送到我的“邮箱”框中?

More code can be supplied as requested, thank you for not flaming the newb. 可以根据要求提供更多代码,谢谢您不要燃烧新手。

<label for="submit">Submit</label>
<input id="submit" type="submit" value="Send Email!" /><br />
</p>
</form>

How does one link to the submit button 一个链接到“提交”按钮的方式
On a webpage, an input element with 'submit' as its 'type' attribute inside a form element will submit the form to the URL specified in the form's 'action' attribute and the response will be loaded in the window supplied in the form's 'target' attribute. 在网页上,在表单元素内具有“提交”作为其“类型”属性的input元素会将表单提交到表单的“操作”属性中指定的URL,并且响应将加载到表单的“目标”属性。

<form action='process.php' target='_blank' id='emailForm'>
    <input type='text' name='email' />
    <input type='submit' value='Send Email!' />
</form>

The code above will submit what the user enters (upon clicking the 'Send Email!' button) in the 'email' text field to 'process.php' and the page it returns will be shown in a new window. 上面的代码会将用户在“电子邮件”文本字段中输入的内容(单击“发送电子邮件!”按钮)提交到“ process.php”,并且它返回的页面将显示在新窗口中。

I am thinking of embedding a or just using href 我正在考虑嵌入a或仅使用href
If you don't want to use a submit button like the example above, the code below will produce the same results as the submit button. 如果您不想像上面的示例那样使用提交按钮,则下面的代码将产生与提交按钮相同的结果。

<a href='javascript:document.getElementById("emailForm").submit();'>
    <img src='images/submit.jpg' />
</a>

have the entire php emailer code embedded somehow inside the submit section 在提交部分内以某种方式嵌入了整个php emailer代码
Unless you're talking about AJAX, there's a clear distinction between the capability of code executed on the client side (JavaScript) and server side (PHP). 除非您在谈论AJAX,否则在客户端(JavaScript)和服务器端(PHP)执行的代码的功能之间存在明显的区别。

JavaScript code is executed from the client side and lets you check for potential errors (and stop a form being submitted) prior to submitting a form, like a space in an email address or letters where the entry is supposed to be numeric. JavaScript代码是从客户端执行的,可让您在提交表单之前检查潜在的错误(并停止提交表单),例如电子邮件地址中的空格或应输入数字的字母。

PHP code is executed from the server side and saves the email address (or whatever else needs saving) in a database. PHP代码是从服务器端执行的,并将电子邮件地址(或其他需要保存的内容)保存在数据库中。 It is also capable of sending emails. 它还能够发送电子邮件。 For security reasons, you should not depend solely on JavaScript for error-checking and the same checks, like spaces in email addresses and letters where the entry's supposed to be numeric, should be carried out in the PHP too. 出于安全原因,您不应仅依赖JavaScript进行错误检查,并且同样的检查(例如电子邮件地址和字母中的空格,应将条目假定为数字)也应在PHP中进行。

I can submit the information supplied by a user to a MySQL database and log it there; 我可以将用户提供的信息提交到MySQL数据库并在那里记录; however, I was just wondering if there is a more elegant way to send email using the information saved in my database 但是,我只是想知道是否有一种更优雅的方式来使用数据库中保存的信息发送电子邮件
If I understand you correctly, you now have a form that submit to a PHP file which in turn records the contents of that form (an email address) to a MySQL database which you check from time to time. 如果我对您的理解正确,那么您现在拥有一个提交给PHP文件的表单,该文件又将该表单的内容(电子邮件地址)记录到MySQL数据库中,您可以不时检查该数据库。 Now your objective is to have the email address sent to your inbox in addition to being recorded in the MySQL database, is that right? 现在您的目标是将电子邮件地址除了记录在MySQL数据库中之外,还要发送到收件箱,对吗? All you need to do is to append this code to your existing PHP file that currently writes to your MySQL database. 您需要做的就是将此代码附加到当前写入MySQL数据库的现有PHP文件中。

$to = "my_address@email.com";
$subject = "New email address!";
$body = "Hi,\n\nYou have a new email address:\n" . $_REQUEST['email'];
if (mail($to, $subject, $body)) {
    echo("<p>Message successfully sent!</p>");
} else {
    echo("<p>Message delivery failed...</p>");
}

Different hosting service providers may have different rules governing sending of emails like requiring a from field, or the from field needs to contain an email address matching the domain the form is hosted on, etc. Check with your service provider if you have problems sending mail. 不同的托管服务提供商可能具有不同的电子邮件发送规则,例如要求“发件人”字段,或者“发件人”字段需要包含与托管表单的域相匹配的电子邮件地址,等等。如果您在发送邮件时遇到问题,请咨询服务提供商。

Isn't that the whole point of having an action attribute in your form ? 这不是在form中具有action属性的全部意义吗?

Use this way: 使用这种方式:

<form method="post" action="your_page_where_you_send_a_mail"> ... </form>

You don't link to a button element. 您不链接button元素。 By clicking a button you trigger an action that is executed in the client (if Javascript is involved) and for the most part on the server. 通过单击按钮,您可以触发在客户端(如果涉及Javascript)中执行的操作,并且在服务器上大部分执行。 Everthing written in PHP is executed on the server, not in the client. 用PHP编写的所有内容都在服务器上执行,而不是在客户端上执行。

So, yes, you'd write PHP code to save data to a database, send an email, redirect the browser to a success page, and everything else necessary to handle the request triggered by clicking the button. 因此,是的,您将编写PHP代码以将数据保存到数据库,发送电子邮件,将浏览器重定向到成功页面,以及处理通过单击按钮触发的请求所需的所有其他操作。

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

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