简体   繁体   English

php 没有收到来自 ajax 类型帖子的任何请求,是否需要在邮件中提及用户名和密码

[英]php is not getting any request from ajax type post and is it require to mention username and password in mail

I'm working on contact page form to send emails.我正在处理联系页面表单以发送电子邮件。

I have written HTML form to get user details and performing form validations in contactform.js file.我已经编写了 HTML 表单来获取用户详细信息并在contactform.js文件中执行表单验证。 In the same JavaScript file I have written ajax POST call to call contact_mail.php .在同一个 JavaScript 文件中,我编写了ajax POST 调用来调用contact_mail.php

I have found many similar questions over the internet but the problem is - in my local machine on form Submit button php is getting called, but this is not happening in remote web server.我在互联网上发现了许多类似的问题,但问题是 - 在我的本地机器上,表单提交按钮php被调用,但这在远程 Web 服务器中没有发生。

If I avoid call to contactform.js by removing class="contactForm" from my form definition, php is getting called but I loose all the validation.如果我通过从表单定义中删除class="contactForm"来避免调用contactform.js ,则会调用php ,但我会丢失所有验证。 :( If i call contactform.js by specifying class="contactForm" in form definition then validation are performed nicely but nothing happens on submit since no call to PHP. :( 如果我通过在表单定义中指定class="contactForm"来调用contactform.js ,那么验证会很好地执行,但由于没有调用 PHP,因此提交时什么也没有发生。

I don't know what am I doing wrong... Here is my code...我不知道我做错了什么......这是我的代码......

HTML form looks like this: HTML 表单如下所示:

 <form action="contactform/contact_mail.php" method="post" role="form" class="contactForm"> <div class="form-group"> <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" /> <div class="validation"></div> </div> <div class="form-group"> <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" /> <div class="validation"></div> </div> <div class="form-group"> <input type="phone" class="form-control" name="phone" id="phone" placeholder="Your Contact" data-rule="phone" data-msg="Please enter a valid contact number" /> <div class="validation"></div> </div> <div class="form-group"> <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" /> <div class="validation"></div> </div> <div class="form-group"> <textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea> <div class="validation"></div> </div> <div class="text-center"><button type="submit">Send Message</button></div> </form> </div> </div>

This is my javascript contactform.js file这是我的 javascript contactform.js 文件

 jQuery(document).ready(function($) { "use strict"; //Contact $('form.contactForm').submit(function(e) { var f = $(this).find('.form-group'), ferror = false, emailExp = /^[^\\s()<>@,;:\\/]+@\\w[\\w\\.-]+\\.[az]{2,}$/i, phoneExp = ^\\+[0-9]{1,3}\\.[0-9]{4,14}(?:x.+)?$; /*phoneExp = /^\\d{10}$/;*/ f.children('input').each(function() { // run all inputs var i = $(this); // current input var rule = i.attr('data-rule'); if (rule !== undefined) { var ierror = false; // error flag for current input var pos = rule.indexOf(':', 0); if (pos >= 0) { var exp = rule.substr(pos + 1, rule.length); rule = rule.substr(0, pos); } else { rule = rule.substr(pos + 1, rule.length); } switch (rule) { case 'required': if (i.val() === '') { ferror = ierror = true; } break; case 'minlen': if (i.val().length < parseInt(exp)) { ferror = ierror = true; } break; case 'email': if (!emailExp.test(i.val())) { ferror = ierror = true; } break; case 'phone': if (!phoneExp.test(i.val())) { ferror = ierror = true; } break; case 'checked': if (! i.is(':checked')) { ferror = ierror = true; } break; case 'regexp': exp = new RegExp(exp); if (!exp.test(i.val())) { ferror = ierror = true; } break; } i.next('.validation').html((ierror ? (i.attr('data-msg') !== undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind'); } }); f.children('textarea').each(function() { // run all inputs var i = $(this); // current input var rule = i.attr('data-rule'); if (rule !== undefined) { var ierror = false; // error flag for current input var pos = rule.indexOf(':', 0); if (pos >= 0) { var exp = rule.substr(pos + 1, rule.length); rule = rule.substr(0, pos); } else { rule = rule.substr(pos + 1, rule.length); } switch (rule) { case 'required': if (i.val() === '') { ferror = ierror = true; } break; case 'minlen': if (i.val().length < parseInt(exp)) { ferror = ierror = true; } break; } i.next('.validation').html((ierror ? (i.attr('data-msg') != undefined ? i.attr('data-msg') : 'wrong Input') : '')).show('blind'); } }); if (ferror) return false; else var str = $(this).serialize(); var action = $(this).attr('action'); if( ! action ) { action = 'contactform/contact_mail.php'; } $.ajax({ type: "POST", url: action, data: str, success: function(msg) { // alert(msg); if (msg == 'OK') { $("#sendmessage").addClass("show"); $("#errormessage").removeClass("show"); $('.contactForm').find("input, textarea").val(""); } else { $("#sendmessage").removeClass("show"); $("#errormessage").addClass("show"); $('#errormessage').html(msg); } } }); return false; }); });

My contact_mail.php file我的 contact_mail.php 文件

 <?php /* send mail new*/ /* start - send mail */ require_once('contactform/class.phpmailer.php'); require_once('contactform/PHPMailerAutoload.php'); require_once('contactform/class.smtp.php'); if(isset($_POST["Submit"])) { $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $message=$_POST['message']; $body = '<html><body>'; $body .= '<h2>Customer Enquiry </h2><br>'; $body .= '<table rules="all" style="background-color: #EBF4FA;" cellpadding="10">'; $body .= "<tr><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>"; $body .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>"; $body .= "<tr><td><strong>Contact No. :</strong> </td><td>" . strip_tags($_POST['phone']) . "</td></tr>"; $body .= "<tr><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>"; $body .= "</table>"; $body .= "</body></html>"; $to = "receiver address"; $subject = " Enquiry Form"; $headers = "From: info@xyz.com" . "\\r\\n" . $headers .= "Content-Type: text/html; charset=UTF-8\\r\\n"; $res="OK"; if (mail($to,$subject,$body,$headers)) { return $res; } else { return false; } header( "refresh:0;url=index.html"); }?>

Actually, I am learning php language at beginner level.实际上,我正在初学者级别学习 php 语言。 What should do to send mail successfully and get success or failure message.应该怎么做才能成功发送邮件并获得成功或失败的消息。

It appears the submit form handler is not preventing the default submitted event.提交表单处理程序似乎没有阻止默认的提交事件。 Try adding '''e.preventDefault()''' at the start of the handler.尝试在处理程序的开头添加 '''e.preventDefault()''' 。

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

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