javascript - 当文件超过一级深度时,htaccess 重写导致表单数据出现问题
问题描述
我一直在尝试学习 htaccess 来创建用户友好的 url。我的 url 工作正常,但我的联系表格是一个 php 页面超过一个级别,例如 http://www.example.com/category/item-1/没有将表格数据传递给邮件收件人 - 表格在http://www.example.com/contact/传递数据就好了。
我猜也许它调用 php 的 javascript 的一部分(url:“/php/form-process.php”,)正在被我的业余 htaccess 重写,从而导致问题?
如果有人可以就允许数据通过的最佳方式给出一些指示,那将不胜感激,因为我是 htaccess 的初学者!
我想我可能在这里找到了答案(似乎 POST 值在使用 .htaccess RewriteRule 时丢失。GET 值没问题。如何解决?)但是将解决方案添加到我的 htaccess 似乎并不能解决问题。
这是我在根目录中的 htaccess:
Options +MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=307,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L,R=307]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=307]
#configure 404
ErrorDocument 404 /404.php
#forward .es to .com
RewriteCond %{HTTP_HOST} ^example\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.es$
RewriteRule ^/?$ "http\:\/\/www\.example\.com\/es\/" [R=307,L]
这是我在“ http://www.example.com/category/item-1/ ”的 php 表单代码:
<form role="form" id="contactForm" data-toggle="validator" class="shake">
<div class="form-group">
<div class="controls">
<input type="hidden" id="reference" value="<?php echo htmlencode($propertyRecord['reference']) ?>" >
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" id="name" placeholder="Name (required)" required data-error="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="email" class="email" id="email" placeholder="Email (required)" required data-error="Please enter your email">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" id="msg_subject" placeholder="Phone (optional)">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="controls">
<textarea id="message" rows="7" placeholder="Message (required)" required data-error="Please enter your message message"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<button type="submit" id="submit" class="btn-system btn-small">Send</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</form>
<script type="text/javascript" src="/js/contact-form-script.js"></script>
这是“/js/contact-form-script.js”中的 javascript 文件:
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form...
formError();
submitMSG(false, "Did you fill in the form properly?");
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
function submitForm(){
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
var reference = $("#reference").val();
$.ajax({
type: "POST",
url: "/php/form-process.php",
data: "name=" + name + "&email=" + email + "&msg_subject=" + msg_subject + "&message=" + message + "&reference=" + reference,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
function formSuccess(){
$("#contactForm")[0].reset();
submitMSG(true, "- Message Sent -")
}
function formError(){
$("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$(this).removeClass();
});
}
function submitMSG(valid, msg){
if(valid){
var msgClasses = "h4 text-center fadeIn animated text-success";
} else {
var msgClasses = "h4 text-center text-danger";
}
$("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}
这是 php 文件“/php/form-process.php”:
<?php
$errorMSG = "";
$EmailTo = "me@example.com";
$Subject = "Website Enquiry // Ref: $reference // From $name - $email";
// prepare email body text
$Body = "";
$Body .= "Reference: ";
$Body .= $reference;
$Body .= "\n";
$Body .= "Name: ";
$Body .= iconv('utf-8', 'iso-8859-1', $name);
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= iconv('utf-8', 'iso-8859-1', $message);
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
非常感谢您的帮助!
解决方案
推荐阅读
- python - 基于组的 Bokeh 和 Holoviews 着色和弦图
- html - 如何从 woocommerce 快捷方式代码中删除 clearfix
- python - 新手的 Python 素数
- python - 使用 pandas 数据框将所有 sql 表导入 python
- node.js - 如何使用nodejs附加到json文件中
- spring-boot - 是否可以动态创建 JPA 存储库对象
- python-requests - 带有 python 请求的补丁页面抛出“请求的 JSON 无效”-MS OneNote API
- azure-devops - 如何在 Ubuntu 上的 Azure 管道中访问同一项目的私有存储库?
- javascript - 快递:express.urlencoded 不工作
- c# - 在 Xamarin android 问题中推送通知