Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
JavaScript addEventListner form within a form

I have a Form made up of multiple fields.

Within this form, I am adding two new fields that I like to be able to keep adding values to which get submitted to the database.

The primary form is used to populate a city name, address, etc. the new fields are used to populate event names and dates.

For example, I add Manchester as the city and then I'd like to add as many events to Manchester as I like.

At the moment I have a JavaScript addEventListener setup for the submit button of the primary form which will POST all for the fields i specify, but this refreshes the page every time i SUBMIT

I need to keep the city name field populated so that each event added will contain the city name and the additional event name and event date fields.

Ultimately I want a table with City, EventName and EventDate and a table with the city details.

So I'm wondering how i do this with JavaScript to only submit the city, event name and event date?

I tried to create a new form within a form and reference that with a different js file, but this triggers when the primary form submit is triggered.

var event_form = document.getElementById("add-event");
event_form.addEventListener("submit", function (e) {
    e.preventDefault();

    if (validated(this)) {
        this.classList.add("was-validated");
        var e_eventname = document.getElementById("project-event-name").value;
        var e_eventdate = document.getElementById("project-event-date").value;
        //var e_cityname = document.getElementById("city-name").value;

        const csrftoken = document.getElementsByName("csrfmiddlewaretoken")[0].value;

        const formdata = new FormData();

        formdata.append("event_name", e_eventname);
        formdata.append("event_date", e_eventdate);
        //formdata.append("city_name", e_name);


        const xhr = new XMLHttpRequest();
        xhr.open("POST", "/apps/add_event");
        xhr.setRequestHeader("X-CSRFToken", csrftoken);
        xhr.send(formdata);
        xhr.onload = () => {
            window.location.reload();
        };
    }
})

So i somehow need to setup the JS to trigger on a seperate button that will only POST the specific fields.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda