Skip to content
Advertisement

php ajax insert shows blank response but no error

I am simply inserting a demo data by ajax form to database. The ajax shows no error but shows blank response when i use console.log(response);.

Php code-

<form role="form" id="signup_form">
                            
            <div class="row">

            <div class="col-lg-6 col-md-6">
            <div class="form-group">
              <label class="required">First Name</label>
                 <div class="input-with-icon">
                   <input type="text" class="form-control" placeholder="Your First Name"
                     name="f_name" id="f_name" required>
                  <i class="ti-user"></i>
                      </div>
              </div>
         </div>
         </div>

           <div class="form-group">
            <button type="submit" id="submit" name="submit"
                class="btn btn-md full-width btn-theme-light-2 rounded">Sign Up</button>
                    </div>

 </form>

ajax code-

 <script>
$(document).ready(function() {
    $("#submit").on("click", function(e) {
        e.preventDefault();

        var f_name = $('#f_name').val();
        console.log(f_name);  // <--- but this shows the typed data from the form



        $.ajax({
            url: "homes/php/user-signup.php",
            method: "POST",
            data: {

                "f_name": f_name

            },
            success: function(response) {
                $('#signup_form')[0].reset();
                $('#signup').modal('hide');
                console.log(response); // <--- this shows blank response



            },
            error: function(response) {
                console.log(response);


            }
        });
    });
});
</script>

mysql —

<?php
session_start();
include_once 'conf.php';

if(isset($_POST['submit'])){
            $f_name=$_POST['f_name'];

              $query = "INSERT INTO `user_info`(`first_name`) VALUES (':f_name')";

            $stmt = $dbcon->prepare($query);
            $stmt->bindParam(':f_name',$f_name);
            $stmt->execute();

  
 
    }
 ?>

Advertisement

Answer

You’re echoing a script block in the response, which makes no sense in an AJAX context.

Also you’re not echoing any content in anything except the pure success case, and you’re checking for the wrong variable in $_POST – your AJAX code only sends “f_name” to the server, nothing else.

You also have a typo in your SQL query – :f_name should not in in quote marks within the SQL string.

This should give you more feedback:

<?php
session_start();
include_once 'conf.php';

if(isset($_POST['f_name']))
{
    $query = "INSERT INTO `user_info`(`first_name`) VALUES (:f_name)";
    $stmt = $dbcon->prepare($query);
    $stmt->bindParam(':f_name', $_POST['f_name']);
            
    if($stmt->execute())
    {   
       echo "Signup Successful";
    }
    else 
    {
      echo "Error - query did not execute correctly. See error logs for details";
      //N.B. You should put some code here to log the real error to a file
    }
}
else
{
  echo "Missing parameters, please try again";
}
?>

If it still doesn’t output anything after that, do some more debugging – check your browser’s Console and Network tools to see if the request is even succeeding.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement