I am using ajax to a submit a form which inserts data into a table.

php

$data = array('full_name' => $_POST['fullname'] , 'email' => $_POST['email'] 
);
$type= array('%s' , '%s'); 
$wpdb->insert( 'email_database', $data,  $type);

javascript

$(function() {
$('#testform').submit(function(e){
e.preventDefault(); 
$.ajax({
type: 'POST',
data: $(this).serialize()
 })
.done(function(data){
alert("works");
})
.fail(function(){
 alert("doesn't work"); 
});
});
});

html

<form method="post" action="" id="testform"> 
 <div class="input-box">
   <input name="fullname">
    <input name="email">

   <input type="submit">
 </div>

It’s all put on a template page. Seems to work without any issues but is it the right way?

Read more here: Is not using admin-ajax to ajax submissions okay?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: