Send Ajax post request and retrieve the inserted id
<p>I am trying to submit a form which will insert data into a mysql database which is working fine. I then would like to return the id of the new inserted row (id auto increment in mysql table) as I want to open up a modal once the form is submitted so I can provide a link which includes id as a parameter in the url.</p>

<p>To send the data for the form I am using the following code:</p>

<pre><code>$(document).ready(function(){
$("#submitForm").click(function(){

var string = $('#commentForm').serialize();

// AJAX Code To Submit Form.
    $.ajax({
        type: "POST",
        url: "SubmitData.php",
        data: string,
        cache: false,
        success: function(result){
        //alert(result);
        }
    });
});
});
</code></pre>

<p>The SubmitData.php file then inserts the form data into the database.</p>

<p>In the SubmitData.php I can create a variable to pick up the id of the newly inserted row like<br>
<code>$last_id = mysqli_insert_id($conn);</code></p>

<p>Is there a way I can return the $last_id from the SubmitData.php file within the same function?</p>