imdvlt39
imdvlt39

Reputation: 29

Problems getting id posted with ajax

I'm trying to post an id to my ajax.php page with ajax, but unfortunately it doesn't work. More precisely, after sending the id, I load that page and it does not appear there. How can I fix this problem, thank you.

$(".connectedSortable li").click(function(){
            $(".connectedSortable li").css('filter', 'grayscale(100%)');
            $(this).css('filter', 'none');
            var id = $(this).attr('data-index');
            $.ajax({
                url: 'assets/standart/ajax.php',
                method: 'POST',
                dataType: 'text',
                data:{
                    id: id
                },success:function(){
                    $("#formDuzenle").load('assets/standart/ajax.php');
                }
            })
        })

This is my ajax.php document

<?php 
echo $_POST['id'];
?>

Upvotes: 0

Views: 92

Answers (1)

Quentin
Quentin

Reputation: 943579

You are making two HTTP requests.

First you make a POST request to ajax.php. You send an id and the PHP script echos it back.

Then, when the request is successful you run a function with makes a GET request to ajax.php. You don't send the ID again, and you display the result in formDuzenle.

There is nothing in your code which would make PHP remember the data from the first request and send it back to you again for the second request.


You should make one and only one HTTP request.

Either use only $.ajax and then do something with the data you get back in the success function.

$.ajax({
    url: 'assets/standart/ajax.php',
    method: 'POST',
    dataType: 'text',
    data: {id },
    success: function(data){
        $("#formDuzenle").text(data);
    }
})

or

Use only $(...).load() and make it a POST request:

$("#formDuzenle").load('assets/standart/ajax.php', { id } );

Upvotes: 1

Related Questions