我正在制作一个php表单,它将使用$_SERVER"PHP_SELF“来提交到同一页面。我使用的是接受文件名的.htaccess文件,它将去掉.php扩展名,所以我的url看起来像这样:http://example.com/contact而不是http://example.com/contact.php......
当我从表单操作中删除.php时,它会刷新页面,但不会记录服务器‘’post‘。
我做错了什么??
记录是否发送了post请求的php代码
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo 'POST WORKS';
}表格
<form action="<?php echo '/'.htmlspecialchars(basename($_SERVER["PHP_SELF"], '.php'), ENT_QUOTES, "utf-8"); ?>" method="post" name="contact-form" id="contact-form" class="contact-form">
<div class="columns-2 float-left">
<label for="name">Your name <span class="required-text orangeText">(Required)</span></label>
<input name="name" id="name" type="text" class="" value=""/>
</div>
<div class="columns-2 float-right margin-0">
<label for="email">Your email <span class="required-text orangeText">(Required)</span></label>
<input name="email" id="email" type="text" class="" value=""/>
</div>
<div class="columns-1 margin-0">
<label for="message">Your message <span class="required-text orangeText">(Required)</span></label>
<textarea name="message" id="message" type="text" class=""></textarea>
</div>
<div class="columns-1 float-right margin-0 submit-btn-container">
<input name="submit" type="submit" id="submitbtn" value="Send your message"/>
</div>
</form>htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]发布于 2014-11-27 06:50:22
这也是一个问题。
回显'/‘
那里面没必要这么做
发布于 2019-12-23 14:49:33
您可以在.htaccess文件中使用以下代码:
#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]发布于 2020-03-15 09:43:42
<?php $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); ?>
<form method="post" action="<?php echo basename($PHP_SELF, '.php');?>">从PHP_SELF定义一个变量,然后使用basename来截断文件扩展名。
https://stackoverflow.com/questions/27159117
复制相似问题