dongmaopiao0901 2016-10-25 09:58
浏览 213

提交表单时页面重定向

Working on getting a form to send data to mysql when submitted, using API calls. I currently have the form submitted successfully and displaying a message on a blank page simply displaying "worked". I am using GO as my backend, and using bootstrap and HTML for the front end

The issue I am having is trying to get the page to redirect to a url instead of displaying that message. I have tried to implement something in the HTML form however it seems to be overridden by the function in my GO application

This is the current GO function which handles the process:

func insertv4Reservation(ctx echo.Context) error {
stmt, err := ipamDB.Prepare(ipamSQL.Insertv4HostsStatement)
if err != nil {
    log.Fatal(err)
}
defer stmt.Close()

var identifier_value = ctx.FormValue("dhcp_identifier_value")
var identifier_type = 0 //value 0 corresponds to hw-address,
var dhcp_subnet_id  = ctx.FormValue("dhcp4_subnet_id")
var ipv4_reservation = ctx.FormValue("octet1")
var hostname = ctx.FormValue("hostname")
var next_server = ctx.FormValue("dhcp4_next_server1")
//var dhcp4_ser_host = ctx.FormValue("dhcp4_server_hostname")
//var dhcp4_bf_name = ctx.FormValue("dhcp4_boot_file_name")

res, err := stmt.Exec(identifier_value, identifier_type, dhcp_subnet_id, ipv4_reservation, hostname, next_server)
if err != nil {
    log.Fatal(err)
}

lastId, err := res.LastInsertId()
if err != nil {
    log.Fatal(err)
}

rowCnt, err := res.RowsAffected()
if err != nil {
    log.Fatal(err)
}

log.Printf("ID = %d, affected = %d
", lastId, rowCnt)
return ctx.String(http.StatusOK, "worked")

please bear in mind I am relatively new to GO, so please do comment any code given

  • 写回答

1条回答 默认 最新

  • duancanjiu3754 2016-10-25 11:42
    关注

    As mentioned in the comments, Echo have another response handler called "Redirect". Source.

    It takes a response code (must be between 300 & 307) as the first argument and a URL as the second. It handles the header and sends the appropriate header-values to the User-Agent (in your case probably the web browser).

    Replace the last line:

    return ctx.String(http.StatusOK, "worked")

    with

    return ctx.Redirect(http.StatusSeeOther, "http://your.new.url.here?foo=bar")

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据