dongyan1548 2017-11-24 17:07
浏览 37

如何检测用户对网页表单的输入

I am trying to detect if the user enters something on a webpage hosted by golang but every time the user clicks the button and submits the form it just redirects to a different page (I am hosting on localhost:8080 and it redirects to localhost:8080/text). I am pretty sure this is because the form action is set to "/text" but if I remove that the golang handlefunc is never run. Here is what I have so far:

package main

import (
  "log"
  "net/http"
  "fmt"
)

func main()() {
  //runs server
  fs := http.FileServer(http.Dir("./"))
  http.Handle("/", fs)
  log.Println("Listening...")
  http.ListenAndServe(":8080", nil)
  //detects user input and calls function
  http.HandleFunc("/text", textGetter)
}

func textGetter (w http.ResponseWriter, r *http.Request) () {
  r.ParseForm()
  text := r.PostFormValue("text")
  fmt.Fprintf(w, "this: %s", text)
  fmt.Print(" was written in the text box")
}

and this html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello</title>
    <link rel="stylesheet" href="./stylesheet.css">
  </head>
  <body>
    <h1>Write something bellow</h1>
    <form class="userText" action="/text" method="post">
      <input type="text" name="textbox" id="textBox" value="" placeholder="type something here">
      <input type="submit" value="text" name="button" id="button" onclick="thank(message.value)">
    </form>
    <script type="text/javascript" src="./script.js"></script>
  </body>
</html>

Can someone tell me what's wrong?

  • 写回答

1条回答 默认 最新

  • dongshenghe1833 2017-11-24 19:39
    关注

    It’s a front end task independent of Go or any other language.

    You may track user input adding onkeypress attribute to the interesting field. You need some Ajax library. For example you may take jQuery.

    Try code similar to:

    <input ... onkeypress="input()">
    <script>
           function input(){
                $.ajax(url, parameters)
           }
     </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c