Javascript 无法访问php脚本的联系人表单
我正在尝试配置我的“发送消息/提交”按钮,以便在单击时访问本地目录中的php文件,并执行代码以通过电子邮件发送相应的数据 HTML代码显示以下内容:Javascript 无法访问php脚本的联系人表单,javascript,php,jquery,html,css,Javascript,Php,Jquery,Html,Css,我正在尝试配置我的“发送消息/提交”按钮,以便在单击时访问本地目录中的php文件,并执行代码以通过电子邮件发送相应的数据 HTML代码显示以下内容: <section class="contact-us"> <div class="form-wrapper"> <div class="contact-form"> <form> <fieldset>
<section class="contact-us">
<div class="form-wrapper">
<div class="contact-form">
<form>
<fieldset>
<div class="control-group">
<input id="Field12" name="Field12" placeholder="Full Name" required="" tabindex="1" type="text" value="">
</div>
<div class="inline-groups">
<div class="control-group">
<input id="Field3" maxlength="255" name="Field3" placeholder="Email" required="" spellcheck="false" tabindex="2" type="email" value="">
</div>
<div class="control-group">
<input id="Field4" maxlength="255" name="Field4" placeholder="Phone number (optional)" tabindex="3" type="tel" value="">
</div>
</div>
<div class="control-group">
<textarea cols="50" id="Field5" name="Field5" placeholder="Let us know what we can do for you!" spellcheck="true" tabindex="4"></textarea>
</div>
<input id="idstamp" name="idstamp" type="hidden" value="r885sYVzp1O8HO4V4MdnZvkis7E3NZIfZ8CtHRCtCdY">
<button class="button" action="send_form_email.php" type="submit">Send message</button>
</fieldset>
</form>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "matty65622@hotmail.com";
$email_subject = "Greenmount Engineering | Customer Enquiry";
function died($error) {
// your error code can go here
echo "We are very 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['name']) ||
!isset($_POST['email']) ||
!isset($_POST['tel']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['tel']; // 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,$name)) {
$error_message .= 'The name you entered does not seem 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 .= "Name: ".clean_string($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 your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
PHP代码显示以下内容:
<section class="contact-us">
<div class="form-wrapper">
<div class="contact-form">
<form>
<fieldset>
<div class="control-group">
<input id="Field12" name="Field12" placeholder="Full Name" required="" tabindex="1" type="text" value="">
</div>
<div class="inline-groups">
<div class="control-group">
<input id="Field3" maxlength="255" name="Field3" placeholder="Email" required="" spellcheck="false" tabindex="2" type="email" value="">
</div>
<div class="control-group">
<input id="Field4" maxlength="255" name="Field4" placeholder="Phone number (optional)" tabindex="3" type="tel" value="">
</div>
</div>
<div class="control-group">
<textarea cols="50" id="Field5" name="Field5" placeholder="Let us know what we can do for you!" spellcheck="true" tabindex="4"></textarea>
</div>
<input id="idstamp" name="idstamp" type="hidden" value="r885sYVzp1O8HO4V4MdnZvkis7E3NZIfZ8CtHRCtCdY">
<button class="button" action="send_form_email.php" type="submit">Send message</button>
</fieldset>
</form>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "matty65622@hotmail.com";
$email_subject = "Greenmount Engineering | Customer Enquiry";
function died($error) {
// your error code can go here
echo "We are very 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['name']) ||
!isset($_POST['email']) ||
!isset($_POST['tel']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['tel']; // 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,$name)) {
$error_message .= 'The name you entered does not seem 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 .= "Name: ".clean_string($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 your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
我不确定这段代码是否能100%工作,因为我还没有尝试过,但正如您所看到的,尝试将HTML按钮链接到php脚本只会在发送时刷新页面,但当我检查电子邮件时,什么也看不到。
您必须替换一行代码
始终在表单标记中使用action,因此在标记中移动action=send_form_email.php,代码将运行。需要对代码进行一些调整: 将method=post属性添加到标记中,以便不获取所使用的方法 将action=send_form_email.php属性添加到标记,以便表单重定向到脚本 Add name=submit to your tag,这样send\u form\u email.php中的表达式ifisset$\u POST[submit]计算为true,代码块就会执行 根据$_POST['…']更改s的name属性,以便PHP识别输入值
你应该准备好了。&&Send。当点击按钮时,它会带我进入一个包含php代码的新网页,它似乎没有执行/通过电子邮件将此数据发送到目标电子邮件?你能回顾一下我的php并告诉我哪里出了问题吗?看到下面的评论,我猜你是想在没有安装Web服务器或php的情况下从自己的计算机上运行它,或者以c:///file.xxx而不是http://localhost/file.xxx @Revo我说的对吗?你忘了提到必须设置为post的method属性。它会将表单与get Other一起发送。感谢您的指点,我将进行编辑。谢谢当点击按钮时,它会带我进入一个新的网页,网页上有php代码,它似乎没有执行/通过电子邮件将这些数据发送到目标电子邮件?你能回顾一下我的php并告诉我哪里出了问题吗?name=submit或type=submit??另外,在:&&send中,&&显示为文本,而不是实际的功能代码。此处标记中需要名称和类型。&&只是让您知道如何进行这两种调整。它不起作用:/我的php代码错误,但我已对其进行了更新,请检查并帮助我解决为什么它继续在浏览器中打开代码而不发送到我的目标电子邮件?@Revo因为,您没有安装webserver/PHP/mail,或者您正在以c:///file.xxx而不是http://localhost/file.xxx. 这里很简单。