1

I am trying to trigger a function when the form submit button is clicked. I tried both with click and submit methods but it is not triggered. Any advice would be greately appreciated.

<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button"  value="Submit" >
</form>   

$("#subm").click(function() {
alert('Doesnt work');
}

$("#my_form").submit(function() {
alert('Doesnt work');
}
2
  • This has nothing to do with php. Please fix your questions tags... Commented Jan 4, 2017 at 17:28
  • Do you get any hints in your browsers development console? Commented Jan 4, 2017 at 17:29

1 Answer 1

1

First you need to add Jquery library to work and then you have to use preventDefault() like below:-

<form id="my_form" action="process.php" method="POST">
<input type="submit" id="subm" class=" submit_button"  value="Submit" >
</form>
<script src = "https://code.jquery.com/jquery-3.1.1.min.js"></script>  
<script type = "text/javascript">
$(document).ready(function(){ // After document is completly loaded
    $("#subm").click(function(e) {
       e.preventDefault(); // prevent the form submit on click
       alert('Does work'); //alert the message
    });// you missed ); here
}); 

</script>

Note:- check your browser development console and you will exactly get what errors are there.

$(document).ready(function(){ // After document is completly loaded
        $("#subm").click(function(e) {
           e.preventDefault(); // prevent the form submit on click
           alert('Does work'); //alert the message
        });// you missed ); here
    }); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="my_form" action="process.php" method="POST">
    <input type="submit" id="subm" class=" submit_button"  value="Submit" >
</form>

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

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.