0

I have problem with submitting my contact form (written in php). When I submit form to check textareas, the info messages appears under form if some textarea is empty. Im using 'if' statement because without it the style.visibility appears before form submit. So, why my code don't work?

    function(){
        document.getElementById("buttonstatus").addEventListener('click', function(){
            document.getElementById("contact-form").submit();
            if(document.getElementById("contact-form").submit() == true){
                document.getElementById("messages").style.visibility = "visible";
            }
        });
    }

EDIT: The "messages" current property visibility style is hidden

EDIT2:

Tried to do smth like that but don't work either :/

<button id="buttonstatus" onclick="document.getElementById('contact-form').submit(function(){
    document.getElementById('messages').style.visibility = 'visible';
});">Send message</button>
  • 3
    Why is your if calling the submit method? That's going to cause the form to submit and perform a page transfer. – Taplar Apr 01 '20 at 16:54
  • 1
    document.getElementById("contact-form").submit() <-- not sure how submit returns a boolean. Submitting a form replaces the page. – epascarello Apr 01 '20 at 16:54
  • Are you trying to validate your forum before its sent? If so you should look at this answer [here](https://stackoverflow.com/questions/8664486/javascript-code-to-stop-form-submission#answer-8664680). I think its similar to what you are trying to do. – Jack Apr 01 '20 at 17:00
  • @Taplar I forgot to write before if - document.getElementById("contact-form").submit() – Marcin Böhm Apr 01 '20 at 17:01
  • 1
    You're ignoring the points about the page transfers. If the submit is successful, the page will be destroyed, there will be nothing to show. If some validation fails, and submit is prevented, related to epascarello's point, how is submit() expected to return a true/false? – Taplar Apr 01 '20 at 17:02
  • @Jack Yes I want to do something after submit like in your link. – Marcin Böhm Apr 01 '20 at 17:07
  • Ahh I'm out of ideas – Marcin Böhm Apr 01 '20 at 18:45
  • You need to pause the form from submitting, check validation and if everything good proceed with submit. Check @Jack link it describes exactly the process. Seems like duplicate. – A. Meshu Apr 01 '20 at 18:47

0 Answers0