0

I am posting data to a PHP page using the $.ajax in Jquery. So far all this is working fine.

Here is how all this looks in my index.html.

function send() {
  $( "#send" ).show( "slow" );

var page = Page;
var title = $("#title").text();
var title_2 = $("#title_2").val();

$.ajax({
    url: "save.php",
    method: "POST",
    data: { MyPage : page, My_Title1 : title, My_Title2 : title_2 },
    dataType: 'json',
    success: function(data) {
        alert(data);
        var result = jQuery.parseJSON(data);
        alert(result.last_id);
    },
    error: function(output) {
    alert("not working whole process");
    }
});

This to sum up what I am doing, is sending some data, Html and contents in div's, to a sql database.

What I would like to do now is that once this data is posted to the save.php file, I get a response from the php sending me the ID of the page I have saved all this in. So I am using mysqli_insert_id($con); to acheive this.

Set it it looks like this.

$last_id = mysqli_insert_id($con);

When I execute all this, the Post works fine and I end up with what I want. {"last id":265} at the end of my post.

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

How do I get this value back to my index.html so that I can place it inside a input. The success is not working out.

//Reply to Steves answer

@ Steve. Thank you for answering. Your answer is exactly what is happening. I am sending a whole bunch of html to my save.php file so it can save it to a sql table. Something looking like this.

Write to MySQL OK!<br>INSERT INTO Project(ID_User,Name_Project,Page_Project,Date_Project) VALUES ( 110, '\"Project name here\"', '<div class=\"file_save_container\"> <------------- HERE THERE IS A WHOLE BUNCH OF HTML ------------> </div>\n\n\n', '2015-03-19 13:10:23');<br>

This is all saving properly to my sql table. What I would like to achieve here is that when the ajax is sent to my save.php I get a response sending me the id of the newly created Project so that I can then place the response "the id" inside a . Right now mysqli_insert_id is placing this at the end of my post.

<br>{"this_id":"311"}

This is what I would like to get back as a response to my index.html file and not have it at the end of my post.

11
  • the jQuery.parseJSON is superfluous, you already set dataType as JSON
    – Kevin
    Mar 19, 2015 at 11:36
  • "The success is not working out." Please explain - what is happening? Do you get an alert?
    – Steve
    Mar 19, 2015 at 11:37
  • Is this typo part of the problem dataType: 'json, should be dataType: 'json',
    – RiggsFolly
    Mar 19, 2015 at 11:38
  • yes I am getting a alert with "not working whole process". Mar 19, 2015 at 11:38
  • 2
    dataType: 'json, you are missing another ' after json and send back "insert-id" not "insert id" because, json will make this an object and "insert id" is not a valid object key. When you do that.. you have your id saved in "data.insert-id" so in your success: function(data){ do: console.log(data.insert-id);
    – Jan M.
    Mar 19, 2015 at 11:39

2 Answers 2

0

Try to set header('Content-Type: application/json'); in save.php

0

Write $data['last_id'] instead of $data['last id'] to match your JS.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.