How to use ajax to publish some content and then get it?

As you all see, I'm a big NOOB for javascript and jquery, but I have a question about ajax request.

Here is what I want to do, but I do not know how:

  • I announced content.append('<div id="box"><textarea>some content</textarea></div');
  • Now I want to do that text = $('textarea').value();jumps to the ajax request.

something like that:

$.ajax({
    type: "POST",
    url: "/localstorage/boxes",
    data: text,
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

and then later I want this data to return to another function oldBox()in order to display it when the user clicks on this event

UPDATE

function saveNote(){

        var theNote = $('div#note');
        var textValue = $this.$('textarea').value();
        var textData = JSON.stringify(textValue);


        $.ajax ({
            type:"POST",
            url: "/localstorage/notes",
            data: textData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                loadNote(data);
            }
        });

    }

That's good, it fixed my mistakes, but when I try to find the localstorage / notes file empty, should it be empty?

+4
source share
3 answers

None of your examples worked, but I found a solution, and here it is :)

JQuery

$(document).on("click", "#save", function(event) {
        var note = $(this).parent().parent().children("textarea").val();
        $.post( "main.php", { note: note });
    });

PHP:

<?
    $json = $_POST['note'];
    $form = json_decode( $json, true);

    print_r($form);
?>
0

ajax :

success: function(data) {
    oldBox(data);
}

, :

$.ajax({
    type: "POST",
    url: "/localstorage/boxes",
    data: text,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        oldBox(data);
    }
});

, oldBox() :

function oldBox(data) {
    $('.whatever').html(data);
}

($('.whatever').html(data);) success:.

/localstorage/boxes , , ajax.

:

text = "Hello";
// text -> /localstorage/boxes
/localstorage/boxes echoes out 'Hi to you to!'
// /localstorage/boxes -> original page
oldBox('Hi to you to!');
+1

I hope to help, I think the problem is your success:

function saveNote(){

    var theNote = $('div#note');
    var textValue = $this.$('textarea').value();
    var textData = JSON.stringify(textValue);


    $.ajax ({
        method:'POST',
        url: '/localstorage/notes',
        type: 'POST',
        data: textData,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json'
        success: function(data) {
            loadNote(data);
        }
    }).success(function(data) {
        loadNote(data);
    });

}

If it does not work

function saveNote(){

    var theNote = $('div#note');
    var textValue = $this.$('textarea').value();
    var textData = JSON.stringify(textValue);


    $.ajax ({
        method:'POST',
        url: '/localstorage/notes',
        type: 'POST',
        dataType: 'json'
        success: function(data) {
            loadNote(data);
        }
    }).done(function(data) {
        loadNote(data);
    });

}
0
source

All Articles