Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!