javascript - Send Ajax post request and retrieve the inserted id - Stack Overflow

I am trying to submit a form which will insert data into a mysql database which is working fine. I then

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.

To send the data for the form I am using the following code:

$(document).ready(function(){
$("#submitForm").click(function(){

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

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

The SubmitData.php file then inserts the form data into the database.

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

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

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.

To send the data for the form I am using the following code:

$(document).ready(function(){
$("#submitForm").click(function(){

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

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

The SubmitData.php file then inserts the form data into the database.

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

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

Share Improve this question edited Nov 25, 2016 at 6:18 hemalp108 1,2492 gold badges16 silver badges23 bronze badges asked Nov 25, 2016 at 5:47 AmarAmar 173 silver badges10 bronze badges 6
  • 2 yes, you add the id to the response from PHP, and there it is, in the success callback result argument – Jaromanda X Commented Nov 25, 2016 at 5:49
  • Just echo that id on the page and you will get that in result in success function – Redhya Commented Nov 25, 2016 at 5:50
  • 1 in your php, simply call echo $last_id; exit; then in your ajax, result will be the id :) – Wesley Smith Commented Nov 25, 2016 at 5:50
  • could you please write your php function here, at which you are sending the request – vishal Commented Nov 25, 2016 at 5:51
  • @vishal why? it wont change the process – Wesley Smith Commented Nov 25, 2016 at 5:51
 |  Show 1 more ment

2 Answers 2

Reset to default 5

Yes return from SubmitData.php the id using the following echo:

echo json_encode(['id'=>$last_id]);

js:

$(document).ready(function(){
$("#submitForm").click(function(){

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

// AJAX Code To Submit Form.
    $.ajax({
        type: "POST",
        url: "SubmitData.php",
        data: string,
        cache: false,
        success: function(result){
          alert(result.id);//this will alert you the last_id

        }
    });

});

});

print last id in that php file

echo $last_id;

get that in ajax success function

success: function(result){
        alert(result);

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744363801a4570600.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信