How to get data submitted via formData() object in Ajax and jQuery to PHP file.
//jQuery sample code
if((unerr = "")){
$.ajax({
method: "post",
url : "ajax.php",
data: new FormData(this),
processData: false,
async: false,
cache: false,
contentType: false
})
.done(function(data){
if(data == "error"){
$("#msg").html("<p class='text-danger'>An error occured, please try again</p>").fadeIn("slow");
}
});
}
//php file
<?php
//Database connection
include_once('includes/db_connect.php');
if($_POST){
$username = $_POST['un'];
//insert values
$insert_values=$con->prepare("INSERT INTO users(username) VALUES(?)");
$insert_values->execute(array($username));
$affected_rows = insert_values->rowCount();
if($insert_values->execute()){
echo "great";
}else{
echo "error";
}
}
?>
I get no response while submitting the form, I don't know what could be wrong.
new FormData(this)?thisrefer to what?var_dump($_POST);in yourphpcode, see the resultnew FormData($('form')[0])