Call PHP with JavaScript
Call PHP with JavaScript
我似乎总是有问题和搞砸。 大学没有教我们任何关于PHP的知识,我想用它来发送电子邮件。 谁能帮忙? 我正在使用JavaScript进行验证,然后使用PHP发送表单。 这是我所拥有的。
.PHP:
<?php
$to = 'jd20032007@hotmail.com';
$subject = 'Voice4Autism Inquiry';
$FirstName = $_POST['fname'];
$LastName = $_POST['lname'];
$eMail = $_POST['email'];
$message = <<<EMAIL
Hi!<br /><br/>
My name is $FirstName $LastName. I am interseted in your newsletter from Voice4Autism. Please add $eMail to your listserve.<br /><br />
Thank you,<br />
$FirstName $LastName
EMAIL;
$header = "From: $eMail'r'n";
$header = "Content-type: text/html'r'n";
mail($to, $subject, $message, $header);
?>
HTML/JavaScript
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script>
function checkforblank() {
var errormessage = "";
if (document.getElementById('fname').value ==""){
errormessage += "enter your first name 'n";
}
if (document.getElementById('lname').value ==""){
errormessage += "enter your last name 'n";
}
if (document.getElementById('email').value ==""){
errormessage += "enter your email 'n";
}
if (document.getElementById('confirmEmail').value ==""){
errormessage += "confirm your email 'n";
}
if (errormessage != ""){
alert(errormessage);
return false;
} else return true;
}
function verifyEmail() {
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+'.[A-Z]{2,4}$/i;
if (document.myForm.email.value.search(emailRegEx) == -1) {
alert("Please enter a valid email address.");
}
if (document.getElementById('email').value == document.getElementById('confirmEmail').value) {
alert("Thank you for your interest!");
status = true;
} else {
alert("Email addresses do not match. Please retype them to make sure they are the same.");
}
return status;
}
function confirmEmailAddresses(){
if (checkforblank() == true) {
if (verifyEmail() == true) {
document.getElementById("myForm").submit();
}
}
}
</script>
</head>
<body>
<div id="content">
<form name="myForm" action="#" method="get" enctype="application/x-www-form-urlencoded" onsubmit="">
<table width="377" height="96">
<tr>
<td style="text-align: right">First Name:</td>
<td><label for="FirstName"></label>
<input type="text" name="fname" id="fname"></td>
</tr>
<tr>
<td style="text-align: right">Last Name:</td>
<td><label for="LastName"></label>
<input type="text" name="lname" id="lname"></td>
</tr>
<tr>
<td style="text-align: right">E-mail:</td>
<td><input type="email" name="email" id="email"></td>
</tr>
<tr>
<td style="text-align: right">Confirm E-mail:</td>
<td><input type="email" name="confirmEmail" id="confirmEmail"></td>
</tr>
</table>
<input type="button" value="Send" onClick="confirmEmailAddresses()"><input type="reset" value="Reset Form">
</form>
</div>
</body>
</html>
JavaScript和HTML确实有效!!
这里绝对不需要AJAX,除非你想让页面持久化。只需将表单action
设置为将接收表单数据的文件并将其邮寄即可。
<form name="myForm" action="mailer.php" method="post" enctype="application/x-www-form-urlencoded" onsubmit="">
将表单方法设置为 POST。这样更好。现在在您的mailer.php
(您可以将其命名为任何名称,只需确保使用正确的操作名称)文件中处理$_POST
数组以获取表单数据。请注意,页面将刷新,您可以在表单提交后使用重定向到感谢页面。
将按钮更改为:
<input type="submit" name="send_mail" value="Send" onClick="confirmEmailAddresses()">
你的 PHP 文件可以是这样的:
<?php
if(isset($_POST) && !empty($_POST)){
if(isset($_POST['send_mail']) && $_POST['send_mail'] == 'Send'){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$confirm_email = $_POST['confirmEmail'];
//Additional validation here
//Use the mail function to send the email
//If mail sent, redirect to a thank you/confirmation page
}
}
?>
有关工作示例,请查看mail
文档。
对于邮件,你可以在这里查看
mail($to,$subject,$message,$header);
电子邮件地址应发送到$to
$subject只是邮件的简要描述
$message是您的信息
$header是像MIME这样的标头内容类型
我不确定你所有的代码都有效。但我注意到的一件事是您需要在 action=" 属性中提供目标 url(在这种情况下为 php 文件),该属性告诉浏览器将捕获的数据发送到何处进行处理。
<form name="myForm" action="#" method="get" enctype="application/x-www-form-urlencoded" onsubmit="">
自
<form name="myForm" action="your_php_program.php" method="get" enctype="application/x-www-form-urlencoded" onsubmit="">
我会使用AJAX。但首先要确保你的Web服务器支持使用PHP进行邮件。你可以使用 phpinfo() 来检查这一点但似乎您的 PHP 脚本根本没有发送邮件()......
相关文章:
- Modify Javascript with C#
- JavaScript with PHP SESSION
- Javascript with IE 11 issue
- javascript with()函数获胜'我不适合歌剧、Chrome或Brave
- Javascript with Heroku
- sqlite with javascript with phonegap using build.phonegap.co
- Including html+javascript with javascript
- .post javascript with PHP 以启用 select 语句返回
- Scrape query JavaScript with scrapy
- 正则表达式 JavaScript with punct,space 和 alnum
- writing JavaScript with PHP
- 将字符串从 C# 传递到 Javascript with Phonegap 和 Windows Phone 8
- Javascript with SJCL lib,在 GCM 模式下解密 AES
- Javascript with jquery datepicker using knockout
- Java to JavaScript with NodeJS convertion
- onMouseOver JavaScript with CSS list-style-image
- Appending on javascript with onclick
- javascript with html for stickynotes
- Scraping javascript with R
- Inserting javascript with jQuery .html