.PHP表单提交不起作用
我希望人们能够通过我在网站上的表格提交他们的联系信息。我自己也不知道如何编写PHP,所以我找到了一个“工作模型”,并进行了一些编辑以适应我的网站。以下是HTML代码:.PHP表单提交不起作用,php,html,forms,Php,Html,Forms,我希望人们能够通过我在网站上的表格提交他们的联系信息。我自己也不知道如何编写PHP,所以我找到了一个“工作模型”,并进行了一些编辑以适应我的网站。以下是HTML代码: <form name="contact" method="post" action="formsubmit.php"> <li> <input name="first_name" type="text" value="first name (Required)" onfoc
<form name="contact" method="post" action="formsubmit.php">
<li>
<input name="first_name" type="text" value="first name (Required)" onfocus="if(this.value == 'first name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'first name'; }" />
<input name="last_name" type="text" value="last name (Required)" onfocus="if(this.value == 'last name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'last name'; }" />
<input name="email" type="text" value="email (Required)" onfocus="if(this.value == 'email (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'email (Required)'; }" />
<input name="telephone" type="text" value="phone" onfocus="if(this.value == 'phone (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'phone'; }" />
<input name="comments" type="text" value="What are your goals?" onfocus="if(this.value == 'What are your goals?') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'What are your goals?'; }" />
<div class="g-recaptcha" data-sitekey="I am using site key here, just wasn't sure if the public should be privy to that info or not"></div>
</li>
</form>
<li>
<input type="submit" value="SUBMIT" class="submitbtn" form="contact" />
</li>
下面是.php文件
<!doctype html>
<html>
<head>
<!-- CSS -->
<link href="main.css" rel="stylesheet" type="text/css" />
<meta charset="UTF-8">
<title>We Will EnduroFit</title>
</head>
<body>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "someone@example.com";
$email_subject = "Message from website visitor";
function died($error) {
// your error code can go here
echo "I am sorry, but there were error(s) found with the form you submitted.";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('I am sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include success html here -->
<h2>Thanks! I will respond to you soon :)</h2>
<?php
}
?>
</body>
</html>
我们会忍耐的
谢谢我很快会回复你:)
如果有人能看看,告诉我我错过了什么。“提交”按钮的行为就像它的死机一样。它一点作用也没有。我知道它是作为一个模型工作的,所以我不知道我做了什么/没有改变来打破愚蠢的提交按钮。万分感谢 试试这个:我已经更改了您的结束表单标签的位置
<form name="contact" method="post" action="formsubmit.php">
<li>
<input name="first_name" type="text" value="first name (Required)" onfocus="if(this.value == 'first name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'first name'; }" />
<input name="last_name" type="text" value="last name (Required)" onfocus="if(this.value == 'last name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'last name'; }" />
<input name="email" type="text" value="email (Required)" onfocus="if(this.value == 'email (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'email (Required)'; }" />
<input name="telephone" type="text" value="phone" onfocus="if(this.value == 'phone (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'phone'; }" />
<input name="comments" type="text" value="What are your goals?" onfocus="if(this.value == 'What are your goals?') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'What are your goals?'; }" />
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxxx"></div>
</li>
<li>
<input type="submit" value="SUBMIT" class="submitbtn" form="contact" />
</li>
</form>
我不完全确定,但我认为提交按钮需要在标签内
这样它就知道要提交什么表单。我想你的问题就在这里,我自己也不知道如何编写PHP。也许你应该看看这个链接。这不是答案,但我建议使用轻量级的php表单类/库。它们通常是添加文本框之类的一行程序。我自己也不知道如何编写PHP好吧。是的。提交按钮需要在tagsI移动的内部,但无效。表单仍然不会提交:(我承认在编写PHP时不是很在行,但我确实觉得我应该能够查看其中的内容并确保输入名称匹配。我已经做了一次又一次……你认为这是HTML问题吗(注意,“表单”标记移动后仍然没有提交)?或者对提交按钮的无响应可能是因为它指向的.php有问题吗?@aldrin27-无任何错误。它完全没有响应。当我悬停时CSS会工作(“提交”会改变颜色),但当我单击时,它绝对没有任何作用。好的,作为测试。我改为“action=test.php”(即使没有action.php文件),我也会得到与提交按钮相同的无响应性。是不是HTML中缺少了一些东西,导致提交不知道应该执行什么操作?