【问题标题】:submit PHP form with get using slug使用 slug 提交 PHP 表单
【发布时间】:2013-11-04 20:59:05
【问题描述】:
我的表格很简单:
<form action="/" method="get">
<select name="category">
<option value="social">social</option>
<option value="tech">tech</option>
</select>
</form>
如果我选择 tech 并像这样提交表单,我将在 URL 中附加 /?category=tech。是否可以改为提交 SEO slug。例如:/tech
【问题讨论】:
标签:
php
forms
.htaccess
mod-rewrite
get
【解决方案2】:
你可以绑定下面的链接。
您好,您将通过重定向链接尝试 .htaccess
http://www.generateit.net/mod-rewrite/
基于 slug 函数,我们可以创建 seo 友好的 url。
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>Enter the Text :</label><input type="text" name="name"><br>
<select name="category">
<option value="social">social</option>
<option value="tech">tech</option>
</select><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php
function seo_friendly_url($string){
$string = str_replace(array('[\', \']'), '', $string);
$string = preg_replace('/\[.*\]/U', '', $string);
$string = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace('/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $string );
$string = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $string);
return strtolower(trim($string, '-'));
}
?>
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$category = $_POST['category'];
echo "<b>input value : </b>".$name;echo "<br>";
$seofriendname = seo_friendly_url($name);
echo "<b>SEO Function value</b> </br>".$seofriendname;
}
?>