提交时触发php和javascript的HTML表单
我有一个简单的html表单,只有一个框供用户输入他们的电子邮件地址 按下提交按钮时,我希望提交的电子邮件地址为: 一封电子邮件给我 b自动添加到谷歌表单 我可以单独做这两件事。我可以使用以下方法执行一个任务:提交时触发php和javascript的HTML表单,javascript,php,html,Javascript,Php,Html,我有一个简单的html表单,只有一个框供用户输入他们的电子邮件地址 按下提交按钮时,我希望提交的电子邮件地址为: 一封电子邮件给我 b自动添加到谷歌表单 我可以单独做这两件事。我可以使用以下方法执行一个任务: <form id="input-form" action="email.php" method="POST"> 其中email.php是: <?php mail('me@gmail.com', $_POST['email'], "Add to Mailing L
<form id="input-form" action="email.php" method="POST">
其中email.php是:
<?php
mail('me@gmail.com', $_POST['email'], "Add to Mailing List");
header('Location: https://my_website.com/thanks.html');
?>
我用以下方法做b:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$('#input-form').on('submit',function(){
var inputq1 = encodeURIComponent($('#email').val());
var q1ID = "entry.1047793221";
var baseURL = 'https://docs.google.com/forms/d/e/1FAIpFLSe7dz1sfyEigQF3QGkTfKCieaqb8OgXoGzLOZPmhn6TvjqFg/formResponse?';
var submitURL = (baseURL + q1ID + "=" + inputq1);
console.log(submitURL);
$(this)[0].action=submitURL;
$('#input-feedback').text('Thank You!');
location.replace("https://my_website.com/thanks.html")
});
</script>
当我对javascript进行注释时,提交的信息会根据需要通过电子邮件发送给我,用户会被引导到我的感谢页面。但是当我加入javascript时,现在电子邮件不会到达,而是将他们输入的电子邮件地址添加到Google表单中。所以谷歌表单部分工作正常,但它似乎覆盖了发送电子邮件给我自己
此外,当包含javascript时,用户不再被重定向到我的感谢页面。相反,他们只是直接进入谷歌表单感谢页面:
请告诉我如何在提交后实现a和b?您可以使用ajax激活电子邮件。php脚本:
<script>
$('#input-form').on('submit',function(e){
e.preventDefault(); // this stops the form submit
var inputq1 = encodeURIComponent($('#email').val());
var q1ID = "entry.1047793221";
var baseURL = 'https://docs.google.com/forms/d/e/1FAIpFLSe7dz1sfyEigQF3QGkTfKCieaqb8OgXoGzLOZPmhn6TvjqFg/formResponse?';
var submitURL = (baseURL + q1ID + "=" + inputq1);
console.log(submitURL);
$(this)[0].action=submitURL;
var data = {
// fill with form input values
email: $('#email').val(),
};
// send ajax request to send your email
$.ajax({
url: '/email.php',
type: 'POST',
data: data,
success: function( response ) {
response = JSON.parse( response );
// check if email is sent successfuly and respond appropriately
if (response.status == 'success') {
$('#input-feedback').text('Thank You!');
location.replace("https://my_website.com/thanks.html")
} else {
$('#input-feedback').text('Ooops! Something went wrong.');
}
},
error: function (xhr, status, error) {
console.log( xhr.responseText );
}
});
});
</script>
PHP
非常感谢。很抱歉,我很傻,但是上面的部分必须在标记中吗?@user1551817是的,所有javascript/jquery代码必须在标记中或在扩展名为.jsSo的文件中。所以,我收到了电子邮件,但我没有进入感谢页面,而是看到了一个白色页面,上面写着{status:success}而且邮件似乎没有发送到我的谷歌表单中。email.php脚本与html和Javascript中的脚本是分开的吗?是的。我的html和javascript在index.html中,php在email.php中
<?php
$response = [
'status' => 'error',
];
// validate input values
if (empty($_POST['email'])) { // this is the most basic validation
echo json_encode($response); exit();
}
// check if mail was sent successfuly
if( mail('me@gmail.com', $_POST['email'], "Add to Mailing List") ) {
$response['status'] = 'success';
echo json_encode($response); exit();
}
// if email failed to send
echo json_encode($response); exit();