0

我是 PHP 和 javascript 编程的新手,根据用户给出的输入,我有一个字符串可以是“abc”或“aac”或“aaa”等。在用户单击某个按钮(比如提交)后,我想根据字符串生成一个动态链接,如 www.domain.com/abc 或 www.domain.com/aac,并将用户导航到生成的链接。这可能吗?谢谢你

4

5 回答 5

0

首先在page1.html

<form method="post" action="page2.php" >
String: <input type="text" name="string" />
<input type="submit" />
</form>

然后在page2.php

header('location:www.domain.com/string/'.$_POST['string']);

但是你应该放一个.htaccess包含:

Redirect /string/(.+) /page3.php?string=$1 [B,QSA]

并且page4.php

echo $_GET['string'];
于 2013-05-03T08:21:48.907 回答
0

是的,有可能,您可以在用户单击提交时传递字符串变量 throw POST 或 GET,然后用字符串 congrereated 回显您想要的链接。假设你想使用 php 看看这个例子

$adress = "www.domain.com/";

$string = $_POST["get_string"];

$result = $adress + $string; 



echo $result;
于 2013-05-03T07:22:06.693 回答
0

您实际上可以定义一个函数并将其设置为您的表单操作。然后获取用户输入,它实际上将在函数中header('location:YOUR_SITE_URL/'.$_GET['user_input']); die;用于将用户重定向到所需的 url。

希望有帮助。

于 2013-05-03T07:23:11.117 回答
0

在 PHP 中:

在您的页面中使用方法并将变量存储到 $UrlName (例如)

并继续

echo("<script>location.href = \"www.domain.com/" . $UrlName . "\";</script>");
于 2013-05-03T07:35:03.657 回答
0

可能,使用路由。一个真实的例子是您的用户名如何成为 Facebook 等社交网站 URL 的一部分。

您需要的是某种数据库来存储字符串,并且它是匹配的数据(可以是 ID 或某些已识别),以告诉服务器在收到该字符串时要加载什么。您还需要路由代码,它解析整个 url 以搜索应该包含该字符串的特定段。这就是路由在 CodeIgniter、Connect 和 Express 等框架中的工作方式。

在 JS 中,Connect 中的路由器如下所示:

app.route('/users/:username',function(username){
  //okay! we got the username!
  //now we'll look for it in the database if it's there
});

对于 PHP,这里有一篇关于 URL 解析的文章。

于 2013-05-03T07:19:25.997 回答