I want to make a form with a text field where the user enters something, then that needs to be incorporated into the link to an external site the user will be redirected to.
So let’s say the user enters ‘foobar’. After he clicks submit he should be taken to http://example.com/foobar .
This should preferably be in JS, although PHP works as well.
So far I have this:
<html>
<head>
<script type="text/javascript">
function genURL() {
var user = document.getElementById('user');
window.location.href = "http://example.com/" + user;
}
</script>
</head>
<body>
<form>
username: <input type="text" name="user">
<input type="submit" value="Submit" name="user" onClick="genURL()">
</form>
</body>
</html>
The only thing this does is append ?user=foobar to the URL.
What do I need to do to make this work?
You should prevent the form from being submitted. You can do this by making your function the
onsubmithandler of the form and returningfalseto prevent the form from being submitted.Furthermore you should get the field’s content by using
.valueindocument.getElementById('').value.JavaScript:
HTML:
Demo: http://jsfiddle.net/jhogervorst/CFHtG/