I am trying to add users to a database using jquery ajax calls. Users added just fine to the database, but ajax always returns with an error. I also do not know how to get a specific error. Below is the code, form, php and jquery.
Here is jQuery
$(document).ready(function() { //ajax call for all forms. $('.button').click(function() { var form = $(this).closest('form'); $.ajax({ type: "POST", url: form.attr('data'), dataType: 'json', data: form.serialize(), success: function (response) { alert('something'); }, error: function() { alert('fail'); } }); }); });
Here is php
<?php include 'class_lib.php'; if(isset($_POST['username'])) { $user = new Users; $user->cleanInput($_POST['username'], $_POST['password']); if($user->insertUser()) { echo json_encode('true'); } else { echo json_encode('false'); } }
Here is the HTML
<div id='newUser' class='tool'> <h3>New User</h3> <form method='post' name='newUser' data='../php/newUser.php'> <span>Username</span><input type='text' name='username'><br> <span>Password</span><input type='password' name='password'> <input type='submit' name='submit' class='button' style='visibility: hidden'> </form> <span class='result'> </span> </div>
javascript jquery html ajax php
T0w3ntuM
source share