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
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.
$("#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