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

189
Visualizações
Javascript onsubmit not calling function in first form, but works fine in second nearly identical form? SOLVED

Ok, so I am completely baffled by this one... working in PHP with some Javascript.

I have created two nearly identical forms with only a submit button. The first one on the page never triggers the onsubmit function call, but the second one always does. Here is the code:

echo('<form action="restricted.php?page=dashboard&sm=unitreport" name="form1" method="post" onsubmit="alert(`test1`);">');
echo('<input type="submit" name="testsubmit1" value="submit1">');
echo('</form>');
                
echo('<form action="restricted.php?page=dashboard&sm=unitreport" name="form2" method="post" onsubmit="alert(`test2`);">');
echo('<input type="submit" name="testsubmit2" value="submit2">');
echo('</form>');

The ONLY difference between these two are that I numbered them "1" and "2".

With the code above, I never receive the alert "test1" but always receive alert "test2".

If I reverse the order of these two forms, then I never receive the alert "test2" but always receive the alert "test1".

In other words, it's only ever triggering the onsubmit call for the second form and never the first, but I have absolutely no idea why.

I've been trying to figure this one out for an hour now through trial and error and no matter what I try, I haven't been able to get both forms to trigger their respective onsubmit functions. w3schools.com and google searches haven't provided any clues yet either.

Any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Ok... So I found the solution to my problem, and it wasn't at all what I expected. I decided to post it as an answer to help in case someone else runs into a similar problem.

I went ahead and copied the code into a new blank page and sure enough, it worked fine. Both forms were triggering their respective messages.

It turns out that about 500 lines of code earlier, there was another html form, and somehow, the closing form tag got deleted and went unnoticed.

Thus the "first" of my two forms was being interpreted as a continuation of the earlier form in my code, which doesn't have an onsubmit call!

Doh! Embarrassing lesson learned.

about 4 years ago · Juan Pablo Isaza Relatório
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