I am trying to specify a dynamic url in the actions properties of form.
The url will be dependant on what the user enters in the textbox. For example, if the user enters "Fred" as firstname and 1234 as courseId, then the url should be "/users/Fred/course/1234"
This is what I have so far but it is not reading the firstname and courseId.
<form action="/users/{{firstname}}/course/{{courseId}}" method="POST">
<input type="text" placeholder="firstname" name="firstname">
<input type="email" placeholder="email" name="email">
<input type="text" placeholder="courseId" name="courseId">
<input type="submit" value="Submit">
</form>
No php and ajax can be used.
asked Jul 5, 2019 at 22:40
DanCode
7033 gold badges8 silver badges28 bronze badges
-
are you using angular or something apart from html?Omar Yafer– Omar Yafer2019年07月05日 22:54:57 +00:00Commented Jul 5, 2019 at 22:54
-
I'm using the hbs module from node.js.DanCode– DanCode2019年07月05日 22:55:52 +00:00Commented Jul 5, 2019 at 22:55
-
What happens when two users share the same first name and are in the same class? Use a user id instead of firstname.Ibu– Ibu2019年07月05日 22:58:25 +00:00Commented Jul 5, 2019 at 22:58
1 Answer 1
You need javascript to do this
const template = (firstname, courseId) => `/users/${firstname}/course/${courseId}`;
document.getElementById('myForm').addEventListener('submit', function(s) {
s.preventDefault();
this.action = template(this.elements["firstname"].value, this.elements["courseId"].value);
this.submit();
});
<form action="placeholder" method="POST" id="myForm">
<input type="text" placeholder="firstname" name="firstname">
<input type="email" placeholder="email" name="email">
<input type="text" placeholder="courseId" name="courseId">
<input type="submit" value="Submit">
</form>
answered Jul 5, 2019 at 22:56
baao
73.5k18 gold badges152 silver badges208 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
DanCode
where is 'placeholder' being called?
baao
Never @DanCode .
AdityaSrivast
@DanCode what do you want to do with
placeholder?DanCode
the placeholder should be the url of the webpage.
baao
placeholder will be replaced on submit @DanCode
lang-html