i have an form which will send some data to an php file , then return some results as example:
<form action"<?php $_SERVER['PHP_SELF']?>" method="post" id="data">
<input type"text" name="first_name"/>
<input type="text" name="last_name"/>
</form>
then in javascript i want catch clcik event in submit form as example :
$("#data").submit(function(){
some code .....
});
which that will prevent the default action for submitting inputs form to the php file , the question is can i send data of a form for php file and same time catch event for the same form ?
NOTE the returned data from php file will be needed by another php
function
To prevent the default behavior of form submit(Which is Page reload) you need to use
event.preventDefault()like thisAnd to post the form data into
phpwithout refreshing page you need to use any of the jQuery’s availableajaxmethods like.post()(Which will send form values usingHTTP POSTMethod) likeIf your php script returns the data in
jsonformat the you can either set thecontent-Typeheader using php or force jQuery to treat the return data asJSONby specifying thedataTypeasJSONin the 4th parameter like