-1

我在 html 中创建了一个站点,但电子邮件表单不起作用。不知道是js还是php。我在 PHP 中尝试了 GET 方法而不是 POST 。我将电子邮件地址更改为现有的。当我输入<?php if(isset($_POST['contact_name']) != "contact_name"){它会发送没有姓名,电子邮件地址,电话或消息的电子邮件。订阅者也是如此

(function () {
		"use strict";
   function checkcontact(input)
		{
			var pattern1=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			if(pattern1.test(input))
			{
				return true;
			}
			else{
				return false;
			}
		}


		function validateSubscription(){


		var footer_name = document.getElementById("subscribe_name"); 	
		var footer_email_address = document.getElementById("subscribe_email");

		if(footer_name.value == ""){
		footer_name.className = "input error";
		return false;
		}
		else if(footer_email_address.value == ""){
		footer_email_address.className = "input error";
		return false;
		}
		else if(checkcontact(footer_email_address.value) == false){
		footer_email_address.className = "input error";
		return false;
		}		
		else{

		$.ajax({
		type: "POST",
		url: 'process.php',
		data: $("#subscribe_form").serialize(),
		success: function(msg)
		{
		if(msg == 'success') 
		{	
		footer_name.className = "input";
		footer_name.value = "";
		footer_email_address.className = "input";
		footer_email_address.value = "";

		$("#subscribe_form").hide();
		document.getElementById("subscribe_success").style.display = "block";
		document.getElementById("subscribe_success").innerHTML = "Thank You! We'll contact you shortly.";
		}else{
		document.getElementById("subscribe_error").style.display = "block";
		document.getElementById("subscribe_error").innerHTML = "Oops! Something went wrong while proceeding.";
		}		
		}

		});

		}
		}

		function removeChecks(){
			var footer_name = document.getElementById("subscribe_name"); 	
			var footer_email_address = document.getElementById("subscribe_email");

			if(footer_name.value != ""){
				footer_name.className = "input";
			}
			if(footer_email_address.value != "" && checkcontact(footer_email_address.value) == true){
				footer_email_address.className = "input";
			}

		}


		function validateContact(){
			var errors = "";

			var contact_name = document.getElementById("contact_name"); 	
			var contact_email_address = document.getElementById("contact_email");
			var contact_phone = document.getElementById("contact_phone");

			if(contact_name.value == ""){
				errors+= 'Please provide your name.';
			}
			else if(contact_email_address.value == ""){
				errors+= 'Please provide an email address.';
			}
			else if(checkcontact(contact_email_address.value) == false){
				errors+= 'Please provide a valid email address.';
			}		
			else if(contact_phone.value == ""){
				errors+= 'Please provide a Phone No.';
			}


			if(errors)
			{
				document.getElementById("error").style.display = "block";
				document.getElementById("error").innerHTML = errors;
				return false;				
			}

		else{

			$.ajax({
			type: "POST",
			url: 'process.php',
			data: $("#contact_form").serialize(),
			success: function(msg)
			{
			if(msg == 'success') 
			{	
			document.getElementById("error").style.display = "none";
			document.getElementById("contact_name").value = "";
			document.getElementById("contact_email").value = "";
			document.getElementById("contact_phone").value = "";
			document.getElementById("message").value = "";
			$("#contact_form").hide();
			document.getElementById("success").style.display = "block";
			document.getElementById("success").innerHTML = "Thank You! We'll contact you shortly.";
			}else{
			document.getElementById("error").style.display = "block";
			document.getElementById("error").innerHTML = "Oops! Something went wrong while proceeding.";
			}		
			}

			});

			}
		} 
<?php
if(isset($_POST['contact_name']) != ""){
			$to = "abc@gmail.com";
			$from =  $_POST["contact_email"];
			$message = "&nbsp;&nbsp;&nbsp;&nbsp;<strong>Name: </strong>".$_POST["contact_name"]."<br />";
			$message .= "&nbsp;&nbsp;&nbsp;&nbsp;<strong>Email Address: </strong>".$_POST["contact_email"]."<br />";
			$message .= "&nbsp;&nbsp;&nbsp;&nbsp;<strong>Phone: </strong>".$_POST["contact_phone"]."<br />";
			$message .= "&nbsp;&nbsp;&nbsp;&nbsp; <strong>Message: </strong>".$_POST["message"]."<br />";
			$subject = 'Contact Enquiry';		
			$headers = "From: ".$_POST["contact_email"]."\n";
			$headers .= "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
			
			$send =	mail($to,$subject,$message,$headers);
	
	
	
	if($send)
	{
		echo "success";
	}
	else
	{
		echo "error";
	}
}


if(isset($_POST['subscribe_name']) != ""){
			$to = "abc@gmail.com";
			$from =  $_POST["subscribe_email"];
			$message = "&nbsp;&nbsp;&nbsp;&nbsp;<strong>Name: </strong>".$_POST["subscribe_name"]."<br />";
			$message .= "&nbsp;&nbsp;&nbsp;&nbsp;<strong>Email Address: </strong>".$_POST["subscribe_email"]."<br />";
			$subject = 'Newsletter subscription';		
			$headers = "From: ".$_POST["subscribe_email"]."\n";
			$headers .= "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
			
			$send =	mail($to,$subject,$message,$headers);
	
	
	
	if($send)
	{
		echo "success";
	}
	else
	{
		echo "error";
	}
}


?>
<div class="col-md-6 col-sm-6 animate fadeInRight">
							<p class="success" id="success" style="display:none;"></p>
							<p class="error" id="error" style="display:none;"></p>
						
							<form class="contact-form" name="contact_form" id="contact_form" method="post" action="#" onSubmit="return false">
								<div class="row">
									<div class="col-md-6">
										<input type="text" data-delay="300" placeholder="Numele tau " name="contact_name" id="contact_name" class="input">
									</div>
									<div class="col-md-6">
										<input type="text" data-delay="300" placeholder="E-mail " name="contact_email" id="contact_email" class="input">
									</div>
								</div>
								<div class="row">
									<div class="col-md-6">
										<input type="text" data-delay="300" placeholder="Phone No" name="contact_phone" id="contact_phone" class="input">
									</div>
								</div>
								<textarea data-delay="500" class="required valid" placeholder="Mesajul tau" name="message" id="message"></textarea>
								<button class="btn btn-primary" name="" type="submit" data-text="Trimite" onClick="validateContact();">Trimite</button>
							</form>
						</div>
					</div>

4

2 回答 2

0

解决了 !问题出在 .htaccess 中。我换了

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$  $1.html [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$  $1.php [L]

RewriteCond %{REQUEST_URI} \.((html)|(php))$
RewriteRule ^(.*)\.([^.]+)$  $1 [R,L]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
于 2020-04-12T14:35:30.323 回答
-1

在用于 validateContact 的 Javascript 中,您将布尔值与字符串进行比较。

if(errors)

实际上应该是

if(errors != '')

这是因为错误永远不会是真或假,它会有一个值或没有一个值。试一试,它应该可以解决您的问题

于 2020-04-07T08:20:18.867 回答