点击网页上的一个按钮,可以生成一个动态链接吗?我们可以浏览网页到动态生成的链接

On clicking a button on a web page, can a dynamic link be generated? can we navigate webpage to the dynamically generated link

本文关键字:链接 动态 一个 网页 浏览 我们 按钮      更新时间:2023-09-26

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

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

希望对你有帮助。

是的,这是可能的,您可以在用户单击提交时传递字符串变量throw POST或GET然后回显你想要的链接和已聚合的字符串。假设您想使用PHP,请看下面的示例

$adress = "www.domain.com/";
$string = $_POST["get_string"];
$result = $adress + $string; 

echo $result;

可能,使用路由。一个真实的例子是你的用户名如何成为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解析的文章

PHP中:

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

,接着是

echo("<script>location.href = '"www.domain.com/" . $UrlName . "'";</script>");

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]

And page4.php:

echo $_GET['string'];