英文:
HTMX POST request for a form is working, but there's no field data included
问题
我有一个表单,应该提交一个包含表单字段数据的POST请求,但是当后端应用程序接收到请求时,它是空的(没有数据)。为什么它不起作用?
<form hx-post="/meme">
<label for="name">有趣的迷因名称</label>
<input type="text" id="name">
<label for="rating">幽默等级</label>
<input type="number" id="rating">
<button type="submit">提交!</button>
</form>
英文:
I have a form that should submit a POST request containing the form field data, but when the backend app receives the request, its blank (no data). Why isn't it working?
<form hx-post="/meme">
<label for="name">Funny meme name</label>
<input type="text" id="name">
<label for="rating">Funniness rating</label>
<input type="number" id="rating">
<button type="submit">Submit!</button>
</form>
答案1
得分: 1
表单字段缺少name
属性。仅当表单字段设置了name
属性时,它们才会包含在POST请求中。例如:name="funny_meme_name"
。
以下是已设置name
属性的代码。
<form hx-post="/meme">
<label for="name">Funny meme name</label>
<input type="text" id="name" name="name">
<label for="rating">Funniness rating</label>
<input type="number" id="rating" name="rating">
<button type="submit">Submit!</button>
</form>
另外,name
应该是唯一的。如果有两个具有相同名称的字段,只有一个将在请求中发送。
英文:
The form fields are missing their name
attributes. Form fields are only included in a POST request if they have a name
attribute set. E.g. name="funny_meme_name"
.
Here's the code with name's set.
<form hx-post="/meme">
<label for="name">Funny meme name</label>
<input type="text" id="name" name="name">
<label for="rating">Funniness rating</label>
<input type="number" id="rating" name="rating">
<button type="submit">Submit!</button>
</form>
Also, the name's should be unique. If there's two fields with the same name, only one will be sent in the request.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论