0

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.

5
  • new FormData(this) ? this refer to what? Commented Apr 7, 2018 at 5:54
  • 1
    $("#signup_form").submit(function(e){ Commented Apr 7, 2018 at 5:57
  • it refers to the sign up form from which to get data Commented Apr 7, 2018 at 5:58
  • use var_dump($_POST); in your php code, see the result Commented Apr 7, 2018 at 6:02
  • Try new FormData($('form')[0]) Commented Apr 7, 2018 at 6:11

1 Answer 1

1

Try using below ajax code check result of $_POST response array.

$("#signup_form").submit(function(e){
    $.ajax({
            url: "ajax.php",
            type: "POST",
            data: new FormData($("#signup_form")[0]),
            contentType: false,
            cache: false,
            processData:false,
            dataType: 'json',
            success: function(done){
              console.log(done)
            },
            error: function(error){
              console.log(error);
            }
        });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.