Trying to pass variable values from JavaScript to PHP using AJAX

后端 未结 2 1777
天命终不由人
天命终不由人 2020-12-05 06:01

I want to pass some values from JavaScript to PHP using jQuery/AJAX. I have the following \"simplified\" code, not sure what is that I am doing wrong. There seems to be quit

相关标签:
2条回答
  • 2020-12-05 06:42

    You need to include a success handler in your AJAX call:

    $("#text-id").on( 'click', function () {
        $.ajax({
            type: 'post',
            url: 'text.php',
            data: {
                source1: "some text",
                source2: "some text 2"
            },
            success: function( data ) {
                console.log( data );
            }
        });
    });
    

    and in your console, you'll receive:

    some textsome text 2
    

    Do make sure that both the test.php and your html source files are in same directory.

    0 讨论(0)
  • 2020-12-05 06:46
    $("#text-id").click(function(e) {// because #text-id is an anchor tag so stop its default behaivour
    e.preventDefault();
    $.ajax({
    type: "POST",// see also here
    url: 'text.php',// and this path will be proper
    data: {
           source1: "some text",
           source2: "some text 2"}
    }).done(function( msg )
          {
           alert( "Data Saved: " + msg );// see alert is come or not
         });
    });
    

    reference ajax

    0 讨论(0)
提交回复
热议问题