Trying to pass variable values from JavaScript to PHP using AJAX

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 quite a few similar questions/answers in StackOverflow, but none of the them are really helping.


<a href="#" id="text-id">Send text</a>
<textarea id="source1" name="source1" rows="5" cols="20"></textarea>
<textarea id="source2" name="source2" rows="5" cols="20"></textarea>


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

PHP (text.php):


$src1= $_POST['source1'];  
$src2= $_POST['source2'];     

echo $src1; 
echo $src2;


The problem: Nothing is errors..nothing. I don't see the values of 'source1' and 'source2' showing up in the PHP echo statements.


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

$("#text-id").on( 'click', function () {
        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
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

Need Your Help

Nodejs+Mongoose : find and update

node.js mongoose

I'm trying to search for a document in my mongo database then using the found document data to search another document and then update both. First document is to be saved after the second document is