0

I am trying to find a way to populate a form select with Go, the data I am trying to populate is taking from a MySQL table, I have already managed to create an array from the rows, I am now looking to populate a select form.

The goal is to be able to select a row from the drop down, to then send a query to delete the selected row from the table.

Below is the code:

HTML

                        {{if .Success}}
                           <h1>Your Vehicle has been successfully removed!</h1>
                           <div class="divFlex">
                              <button onclick="location.href = 'Add-Vehicle.html';" class="btn-block frgt_1 btn addV_btn" type="button" value="Add Another Vehicle">Add Vehicle<br>
                              </button>
                              <button onclick="location.href = 'Add-Vehicle.html';" class="btn-block frgt_1 btn addV_btn" type="button" value="Add Another Vehicle">Remove Vehicle</button>
                           </div>
                        {{else}}
                           <form action="/remove" method="POST" source="custom" name="form">
                              <input type="hidden" name="xss-token" value=""/>
                              <div class="form-group">
                                 <div>
                                    <label class="addV_label">Select Vehicle&nbsp;</label>
                                    <select id="places" name="places">
                                    {{range .}}
                                    <option value="{{.Value}}" id="{{.Id}}" {{if .Selected}}selected{{end}}>{{.Text}}</option>
                                    {{end}}
                                    </select>
                                    <select name="select" class="form-control loginInput2" required="required">
                                       <option value="BMW">BMW</option>
                                    </select>
                                 </div>
                              </div>
                              <div>
                                 <button class="btn-block frgt_1 btn addV_btn" type="submit" value="remove">REMOVE</button>
                              </div>
                           </form>
                        {{end}}

Main.go

func RemoveVehicle(w http.ResponseWriter, r *http.Request) {

    tmpl := template.Must(template.ParseFiles("Remove-Vehicle.html"))

    db, err := sql.Open("mysql", "root:WgFl3f8218!@tcp(127.0.0.1:3306)/my_db")

    if err != nil {
        fmt.Println("Connection Failed.")
        panic(err.Error())
    }

    defer db.Close()

    // Query the DB
    var car Vehicle
    sqlStatement := `SELECT * FROM Vehicle`
    rows, err := db.Query(sqlStatement)
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    var carSlice []Vehicle
    for rows.Next() {
        rows.Scan(&car.Id, &car.Date, &car.Brand, &car.Model, &car.Mileage, &car.Year, &car.rented, &car.Dayrate)
        carSlice = append(carSlice, car)
    }

    fmt.Println(carSlice)

    // insert, err := db.Query("INSERT INTO vehicle(id, date, brand, model, mileage, manufactured, rented, dayrate) VALUES ( ?, NOW(), ?, ?, ?, ?, 0, ?)", id.String(), brand_, model_, mileage_, year_, dayrate_)
    // if err != nil {
    //  panic(err.Error())
    // }
    // defer insert.Close()

    // tmpl.Execute(w, struct {
    //  Success bool
    //  Brand   string
    //  Model   string
    //  Year    int64
    //  Mileage int64
    //  Dayrate int64
    // }{true, brand_, model_, year_, mileage_, dayrate_})
}

Another issue I see is that the form method being POST, as I've seen a few examples in which the form method is GET.

I need to be able to populate select on page load, and then remove the matching option from the table.

  • Bro, your question is not clear. I can't got the problem or bug in your code, you need to edit your question for reader to find key point. – Frank Wang Jan 29 '22 at 04:59

1 Answers1

1

You can use both POST and GET method but they serve for different purposes.

GET and POST explained

Also you can check the link below to see how POST and GET requests are handled in Go server with built-in http package.

Get and Post request handled

Alteran
  • 91
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 29 '22 at 08:38