简体   繁体   English

如何通过以下方式提交表单:PHP发送电子邮件,使用Ajax进行屏幕刷新,然后在不进行屏幕刷新的情况下在表单旁边发布消息?

[英]How do I submit form via: PHP sending email, Using Ajax for no screen refresh, and post a message next to form with no screen refresh?

When the user clicks on the submit button I need: (1) The form data to be sent to me via email using PHP, without refreshing the page. 当用户单击“提交”按钮时,我需要:(1)使用PHP通过电子邮件发送给我的表单数据,而无需刷新页面。 (2) A thank you message to appear next to the form using Ajax. (2)使用Ajax在表单旁边显示一条感谢消息。

I've got most of it working. 我大部分工作了。 I'm not to sure what coding I need to make the page NOT refresh as the PHP script is sending the form data to me via email. 我不确定要使页面不刷新所需的编码,因为PHP脚本通过电子邮件将表单数据发送给我。 Also, once the PHP script is done running, how do I get the Ajax code to run? 另外,一旦PHP脚本运行完毕,如何使Ajax代码运行? I've done a ton of research and have found a lot of people use jQuery to resolve this issue. 我已经做了大量研究,发现很多人使用jQuery来解决这个问题。 However, I am not using jQuery for this just JavaScript. 但是,我不是仅将JavaScript用于jQuery。 I know I am close, but need some help. 我知道我已经接近了,但是需要一些帮助。 Here is my code thus far: 到目前为止,这是我的代码:

PHP - Just sends the form data to me via email: PHP-只需通过电子邮件将表单数据发送给我:

<?php
$persons_name = $_POST['fname'];
$email_address = $_POST['email'];
$comments_questions = $_POST['moreinfo'];

$to = 'fireplace_tea@yahoo.com';
$subject = 'Email - JulesMyers.com';
$msg = "$persons_name has sent an email.\n" .
"You can reply to $persons_name at: $email_address\n" .
"Question or Comment: $comments_questions\n";
mail($to, $subject, $msg, 'From: ' . $email_address);
?>

HTML Form: HTML表单:

<form id="contactMe" method="post" action="contact.php">
 <input type="text" class="inputElement initialColor" name="fname" id="fname">
 <input type="text" class="inputElement initialColor" name="email" id="email">
 <textarea rows="6" class="inputElement" name="moreinfo" id="moreinfo"> </textarea>
 <input type="submit" class="sendButton" id="submitbutton" value="Send">
</form> 
<div class="errormsg"></div>
<div class="formsent"></div>

Ajax: I have the Ajax working when I click the formsent div, but I need it to run after the PHP script does. Ajax:单击forment div时,我可以使用Ajax,但是我需要它在PHP脚本之后运行。 Is there an event I can listen for? 我可以听事件吗?

(function() {
  var httpRequest;
  var b = document.querySelector('.formsent');
  b.onclick = function() { makeRequest('thanks.txt'); };

  function makeRequest(url) 
  {
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }

    httpRequest.onreadystatechange = alertContents;
    httpRequest.open('POST', url);
    httpRequest.send();
  }

  function alertContents() {
    if (httpRequest.readyState === 4) 
    {
      if (httpRequest.status === 200) 
      {
        /*alert(httpRequest.responseText);*/
        b.innerHTML = httpRequest.responseText;
      } 
    }
  }
})();

Thanks for any help. 谢谢你的帮助。 :) :)

WooHoo! 哇噢! I figured it out without using jQuery...in other words I did it the long way. 我没有使用jQuery就知道了...换句话说,我做了很长一段路。 I did a ton of research and trying things out. 我做了很多研究并尝试了一些方法。 How it works is when the user clicks on the button labeled submit JavaScript\\DOM gets and stores the values from the three form fields, than XMLhttpRequest\\AJAX gets the values that JavaScript\\DOM stored and sends them to the PHP file, than the PHP file sends me an email with the form data, and finally httpRequest.responseText puts a thank you message next to the form for the user to see. 它的工作原理是,当用户单击标记为“提交”的按钮时,JavaScript \\ DOM从三个表单字段获取并存储值,然后XMLhttpRequest \\ AJAX获取JavaScript \\ DOM存储的值并将它们发送到PHP文件,而不是PHP。文件向我发送了一封电子邮件,其中包含表单数据,最后,httpRequest.responseText在表单旁边放置了一条感谢消息,供用户查看。 All of this gets done withOUT the page refreshing. 所有这些都无需刷新页面即可完成。 Yeah! 是啊! Here is the coding that works: 这是有效的编码:

HTML HTML

<form id="contactMe">
 <input type="text" class="inputElement initialColor" name="fname" id="fname">
 <input type="text" class="inputElement initialColor" name="email" id="email">
 <textarea rows="6" class="inputElement" name="moreinfo" id="moreinfo"> </textarea>
 <input type="button" class="sendButton" id="submitbutton" value="Send">
</form> 
<div class="errormsg"></div>
<div class="formsent"></div>

JavaScript JavaScript的

function makeRequest()
{
    var httpRequest;
    var personName = document.getElementById('fname').value;
    var emailAddress = document.getElementById('email').value;
    var comments = document.getElementById('moreinfo').value;

    if (window.XMLHttpRequest) { 
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }

    httpRequest.onreadystatechange = alertContents;
    httpRequest.open("GET", "contact.php?name="+personName+"&email="+emailAddress+"&comment="+comments, true);
    httpRequest.send();

    function alertContents() 
      {
        if (httpRequest.readyState === 4 && httpRequest.status === 200) 
        {
            var thankYou = document.querySelector('.formsent');
            thankYou.innerHTML = httpRequest.responseText;

            var clearForm = document.getElementById('contactMe');
            clearForm.reset();
        }
      }
}

PHP PHP

<?php
$name = $_GET['name'];
$email = $_GET['email'];
$comment = $_GET['comment'];

$to = 'email@fakeDomain.fake';
$subject = 'Email From Website Form';
$msg = "$name has sent an email.\n" .
"You can reply to $name at: $email\n" .
"Question or Comment: $comment\n";
mail($to, $subject, $msg, 'From: ' . $email); 

echo "Thank you " . $name . ".<br>";
echo "The form has been sent.";
?>

The hardest part was figuring out how to pass variables from JavaScript to PHP. 最困难的部分是弄清楚如何将变量从JavaScript传递到PHP。 It took time to find really good full examples, and to also find explanations of how it works. 花了一些时间找到非常好的完整示例,并找到了其工作原理的说明。 Here are a couple of resources I found very helpful: 以下是我发现非常有用的一些资源:
tutorials2learn.com/tutorials/scripts/ajax-scripts/submit-html-form-ajax.html tutorials2learn.com/tutorials/scripts/ajax-scripts/submit-html-form-ajax.html
javascriptkit.com/dhtmltutors/ajaxgetpost.shtml javascriptkit.com/dhtmltutors/ajaxgetpost.shtml
javascriptkit.com/dhtmltutors/ajaxgetpost2.shtml javascriptkit.com/dhtmltutors/ajaxgetpost2.shtml
openjs.com/articles/ajax_xmlhttp_using_post.php openjs.com/articles/ajax_xmlhttp_using_post.php
webcheatsheet.com/php/passing_javascript_variables_php.php webcheatsheet.com/php/passing_javascript_variables_php.php
stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor

A few notes: 一些注意事项:
* I used AJAX (XMLHttpRequest object) to open a communication line between JavaScript and PHP. *我使用AJAX(XMLHttpRequest对象)打开JavaScript和PHP之间的通信线路。
* This entire operation can be done in jQuery with less lines of coding. *整个操作可以用更少的代码行在jQuery中完成。 But, I wanted to know how it worked the long way. 但是,我想知道它如何长期发挥作用。 So, now when I look at the jQuery coding I understand what it is doing. 因此,现在当我看一下jQuery编码时,我了解了它在做什么。

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

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