[英]Nothing happens after I click on submit button, in my form built in Ajax and PHP
[英]Nothing happens when i click on submit button in my contact form
當我單擊提交按鈕時,即使設置了PHP,也沒有任何反應。 起初我以為我的代碼顯然是錯誤的。 但是現在我什至從這里復制/粘貼了代碼,將其檢查為正確,但沒有任何反應。
這是該代碼:
<?php
if(isset($_POST['submit'])){
$to = "zezesurla@gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
</div>
這可能是服務器中的問題,而不是實際的代碼嗎? 因為我嘗試了在各種教程中發現的其他幾個示例,並且發生了相同的問題。
還是我做錯了什么?
至少,我將對您的代碼進行以下更改:
<?php
$msg='';
if (isset($_POST['submit']))
{
...
if (!mail($to,$subject,$message,$headers))
error_log(($msg='Send form submission to "'.$to.'" failed'));
else if (!mail($from,$subject2,$message2,$headers2)) // sends a copy of the message to the sender
error_log(($msg='Send copy of form submission to user "'.$from.'" failed'));
else $msg='Mail Sent. Thank you ' . $first_name . ', we will contact you shortly.';
...
<body>
<?php if ($msg)
{ ?>
<p><?php echo $msg ?></p>
<?php
} ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
這些更改不會解決表單易於發送SPAM的問題,但會解決以下問題:
mail()
調用錯誤,它將被記錄並顯示一條消息(這還將確認所涉及的電子郵件地址) "\\n"
)時使用,否則應使用單引號 <head>
部分之前將文本寫入瀏覽器,您將不會生成錯誤的HTML action
來處理表格
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.