1

I feel like I've looked around for the answer for this question, but most of the responses are very hacky: involving javascript that pops in via AJAX, redirects and other ways of modifying the DOM on the fly.

What I want to do is make the submit button disappear when a user submits a document (javascript) and submit the message via mail (php). The code I have is the following:

<form action="" method="post">
...
<input onclick="removeElements()" id="subButton" class="submit" name="submit" type="submit" value="submit">

The php mail function is in the same document.

Here is the removeElements() function:

var el = document.getElementById("subButton");
el.remove();
document.getElementById("thankYouMessage").setAttribute("style", "display:block");

The submit function works without the javascript call, but when I add the onclick="removeElements()" part, then the javascript part starts working, but the php is no longer executed.

I know that there are other methods for doing this, but in this case, I'm actually curious about why this doesn't function as I had planned. By removing the submit button, am I in effect killing the child PHP process mid(or pre)-execution?

Thanks!

2
  • because you just remove the Dom so there is no submit event ? If so, I think change el.remove(); to el.style.display="none" for a test. Commented Jun 12, 2015 at 1:52
  • I guess I should have mentioned that I tried all of these css tricks to disable the visibility of the id. They don't work. I feel like they remove the button, but then it just comes back fractions of a second later. So the button doesn't disappear and the email isn't sent. I'll look at it more to see if I can update the above with more relevant information. Commented Jun 12, 2015 at 3:04

4 Answers 4

2

If you add onclick you will have to fire the submit manually.

The other option is add your javascript call code in onsubmit="removeElements()" on the form tag. This way, it will execute your code after executing submit

Related question here

Sign up to request clarification or add additional context in comments.

1 Comment

I like this - I didn't know about the onsubmit event listener. I felt like this was progress, but it doesn't work for me.
2

Don't remove the button, rather set visible: hidden or display: none for its style. This way it will still be in the document and will work, it just won't be shown.

Comments

0

When you send the form reloads your page so I suggest:

  • Using Ajax and only delete button on the response, or
  • Not generate the button when reloads your page.

Jquery Ajax examples: You can see them here api.jquery.com/jquery.ajax/

Regards.

Comments

0

You could simple use the .hide functionality which JQuery gives you. It's very simple to use and very clean.

Example1 using JQuery:

$("#FormID").submit(function(e) 
{
   e.preventDefault();
   // Hide button
   $("#subButton").hide();
   // Display thank-you message
   $("#thankYouMessage").css("display", "block");
});

Example2 using JQuery:

$(document).ready(function() {
    $(".submit").click(function() {
       $("#subButton").hide();
       $("#thankYouMessage").css("display", "block");
       return false;
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.