简体   繁体   中英

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

I am new to PHP and javascript programming I have a string that can be "abc" or "aac" or "aaa" etc based on the inputs given by the user. After the user clicks some button(say submit) I want to generate a dynamic link like www.domain.com/abc or www.domain.com/aac based on string and navigate the user to the generated link. Is this possible? Thank You

Possible, using routing. A real world example is how your usernames get to be part of the URL in social sites like Facebook.

What you need is a database of some sort to store the string, and it's matched data (could be an ID, or some identified) to tell the server what to load when that string is received. You'd also need routing code, which parses the entire url in search of that certain segment which should contain the string. This is how routing works in frameworks like CodeIgniter, Connect and Express.

In JS, routers in Connect look like:

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

For PHP, here's an article regarding URL parsing.

Yes it is possible, you can pass the string variable throw POST or GET when the user click submit and then echo the link you want with the string congrenated. assuming you want to use php take a look at the example

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

$string = $_POST["get_string"];

$result = $adress + $string; 



echo $result;

You can actually define a function and set it your form action. Then taking the user input it will be really each in the function to use header('location:YOUR_SITE_URL/'.$_GET['user_input']); die; header('location:YOUR_SITE_URL/'.$_GET['user_input']); die; for redirecting the user to desired url.

Hope that helps.

In PHP :

Use methode in your page and store the variable to $UrlName (for example)

and continued with

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

First in page1.html :

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

Then in page2.php :

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

But you should put a .htaccess containing:

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

And page4.php :

echo $_GET['string'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM