-1

I want to redirect page to URL that return from PHP script.
That when user created a post i want to return value of mysql_insert_id() for post_id.

$('input#submitButton').click( function() {
    $.ajax({
        url: 'newpost.php',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
            window.location.replace("http://localhost/?post_id=...");
        }
    });
});

I looked here but not found what i want. Link

Community
  • 1
  • 1
Marcus
  • 295
  • 4
  • 16

1 Answers1

2

In your php script do :

echo json_encode(array(
    'id' => $THE_ID
));

then here do:

$('input#submitButton').click( function() {
    $.ajax({
        url: 'newpost.php',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
            window.location = "http://localhost/?post_id=" + data.id;            
        }
    });
});
});
jj-aa
  • 1,019
  • 5
  • 19
  • 39
  • why nothing happened in `success: function(data) { .. }` when it done? – Marcus Dec 07 '14 at 23:12
  • probably because you got a php or javascript error before. Just open your browser's console and check the response of your post. – jj-aa Dec 08 '14 at 09:26