I am trying to make a contact form in Blogger. The default contact form widget exists and works like a charm, but it only provides 3 fields. Name, email and message. I needed more fields added. Adding more fields to the widget is impossible, since there are specific tags that the widget accepts, that correspond to the three fields "data:contactFormNameMsg" for the name, "data:contactFormEmailMsg" for the email and "data:contactFormMessageMsg" for the message.
我正在尝试在Blogger中创建联系表单。默认的联系表单小部件存在并且像魅力一样工作,但它只提供3个字段。姓名,电子邮件和消息。我需要添加更多字段。向窗口小部件添加更多字段是不可能的,因为窗口小部件接受特定标记,对应于名称的三个字段“data:contactFormNameMsg”,电子邮件的“data:contactFormEmailMsg”和“data:contactFormMessageMsg”信息。
So, the solution was to create my own form. I did not want to use the action=mailto because I don't want windows jumping up, Outlook or Mail starting and tabs getting opened. So, I had to use .php to control the sending of the form. I wrote my .php code. So, since Blogger does not provide the option to host your .php file, I had to look elsewhere for hosting. I googled it. And I was directed to the use of Google Drive as the best option to host a .php file for Blogger. So I tried it. I tried the steps described here (and everywhere else on the web), but as this page says (and it must be a new addition, because all other unofficial -and most probably outdated-sites claim to be able to host their .php files on Google Drive)
所以,解决方案是创建自己的表单。我不想使用action = mailto,因为我不希望Windows跳起来,Outlook或Mail启动和标签打开。所以,我不得不使用.php来控制表单的发送。我写了我的.php代码。因此,由于Blogger没有提供托管.php文件的选项,因此我不得不寻找其他地方进行托管。我用Google搜索了一下。我被指示使用Google云端硬盘作为托管Blogger的.php文件的最佳选择。所以我试了一下。我尝试了这里描述的步骤(以及网络上的其他地方),但正如这个页面所说的那样(并且它必须是一个新的补充,因为所有其他非官方的 - 并且很可能是过时的网站声称能够托管他们的.php文件在Google云端硬盘上)
"Google Drive does not support web resources that make use of server-side scripting languages like PHP."
“Google云端硬盘不支持使用PHP等服务器端脚本语言的网络资源。”
I tried Dropbox. But, as answered here, "Dropbox doesn't support server-side execution of your PHP scripts" either. All these must be new, because every blogger suggests Dropbox and Google Drive as the best options for hosting .php files.
我试过Dropbox。但是,正如这里所回答的那样,“Dropbox也不支持服务器端执行PHP脚本”。所有这些都必须是新的,因为每个博客都建议将Dropbox和Google Drive作为托管.php文件的最佳选择。
Then, I tried 000webhost. It worked. My .php script ran and I received the form in my email, BUT (big big BUT) on clicking the button that triggered the action and the .php, the user is also redirected to the first page of 000webhost.
然后,我尝试了000webhost。有效。我的.php脚本运行了,我收到了我的电子邮件中的表单,但是在点击触发操作的按钮和.php的时候(大的大),用户也被重定向到000webhost的第一页。
I tried hosting it on another free web hosting service, but the same thing happened. The moment the user clicked the "Send" button on my form, they were redirected to the first page of the free webhosting service.
我尝试在另一个免费的网络托管服务上托管它,但同样的事情发生了。当用户点击表单上的“发送”按钮时,他们被重定向到免费虚拟主机服务的第一页。
I thought of adding the php code inline html (?php) ---code--- (/php) but I did not know how to refer to it.
我想添加php代码内联html(?php)---代码---(/ php),但我不知道如何引用它。
So, I have two questions:
所以,我有两个问题:
-
How could I refer to an inline part of php code, in my html code? Remember, we are on Blogger, that little is allowed. I want to refer to the php code from the "form action=" part.
我怎么能在我的html代码中引用php代码的内联部分?请记住,我们在Blogger上,很少被允许。我想从“form action =”部分引用php代码。
-
Why does the "Send" button redirects the user to the front page of the free webhosting service? Is it the "Free webhosting service"'s way of advertising itself? I mean, is it implemented in the service? Is there something wrong with my code?
为什么“发送”按钮会将用户重定向到免费虚拟主机服务的首页?它是“免费虚拟主机服务”的广告方式吗?我的意思是,它是否在服务中实施?我的代码有问题吗?
My php code is this:
我的PHP代码是这样的:
<?php
$errors = '';
$myemail = 'mymail@mymail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
}
?>
And the HTML part where I call the .php file is this:
我调用.php文件的HTML部分是这样的:
<form action='http://myusername.mywebhost.com/contact-form-handler.php' method='POST' name='contact-form'>
So, that's all. If anybody has encountered a similar problem or has needed to use a .php form handler in Blogger RECENTLY, you will be of great help.
所以,就是这样。如果有人遇到类似的问题或者需要在Blogger中使用.php表单处理程序,那么你将会有很大的帮助。
Thank you very very much in advance!
非常感谢你提前!
1 个解决方案
#1
What you can do is send the form via AJAX to whatever host you want - the user won't be redirected there. Here is some example code, you can customize it further
您可以做的是通过AJAX将表单发送到您想要的任何主机 - 用户不会被重定向到那里。这是一些示例代码,您可以进一步自定义它
$("#theform").submit(function(e) {
e.preventDefault();
$.post('http://example.com/contact.php', $('#theform').serialize());
}
serialize
is used to collect the data from inputs and POST it to the given url.
serialize用于从输入中收集数据并将其POST到给定的URL。
AJAX序列化文档
#1
What you can do is send the form via AJAX to whatever host you want - the user won't be redirected there. Here is some example code, you can customize it further
您可以做的是通过AJAX将表单发送到您想要的任何主机 - 用户不会被重定向到那里。这是一些示例代码,您可以进一步自定义它
$("#theform").submit(function(e) {
e.preventDefault();
$.post('http://example.com/contact.php', $('#theform').serialize());
}
serialize
is used to collect the data from inputs and POST it to the given url.
serialize用于从输入中收集数据并将其POST到给定的URL。
AJAX序列化文档