One of my site visitor asked this question via email so that I am going to what are the four parameters used for jQuery Ajax method call. We need to add four mandatory parameter while doing Ajax call using PHP and jQuery.
Following are the list of those four parameters:
The jQuery library provides a few different methods to perform AJAX calls, although here we’ll look at the standard ajax
method, which is the most often used.
Take a look at the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <script> $.ajax( 'request_ajax_data.php', { success: function(data) { alert('AJAX call was successful!'); alert('Data from the server' + data); }, error: function() { alert('There was some error performing the AJAX call!'); } } ); </script> |
Below is the complete example of Ajax call using PHP and jQuery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <!doctype html> <html> <head> <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script> </head> <body> <form id="loginform" method="post"> <div> Username: <input type="text" name="username" id="username" /> Password: <input type="password" name="password" id="password" /> <input type="submit" name="loginBtn" id="loginBtn" value="Login" /> </div> </form> <script type="text/javascript"> $(document).ready(function() { $('#loginform').submit(function(e) { e.preventDefault(); $.ajax({ type: "POST", url: 'login.php', data: $(this).serialize(), success: function(response) { var jsonData = JSON.parse(response); // user is logged in successfully in the back-end // let's redirect if (jsonData.success == "1") { location.href = 'my_profile.php'; } else { alert('Invalid Credentials!'); } } }); }); }); </script> </body> </html> |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.