10

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.

HTML:

<div>
<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>
</div>

JAVASCRIPT:

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

PHP (text.php):

<?php 

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

echo $src1; 
echo $src2;

?>

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

5
  • 1
    first include jquery file Commented Oct 19, 2013 at 5:18
  • that is already there...in the HTML head. I just didn't show it here. Commented Oct 19, 2013 at 5:20
  • 1
    Please - give us a clue! What is happening, or not happening? Do you see an error message? Commented Oct 19, 2013 at 5:20
  • see updated answer POST be captical Commented Oct 19, 2013 at 5:21
  • Ok - what's in the server logs? Commented Oct 19, 2013 at 5:22

2 Answers 2

10

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.

4
  • 1
    Hi, the JavaScript console does show "some textsome text 2". but for some reason I don't see any of the PHP echo statements. Can you please tell me why? Commented Oct 19, 2013 at 5:51
  • @Gandalf I'm not sure I understand your question. You want the PHP source code to be sent back? Commented Oct 19, 2013 at 6:15
  • yes...it is supposed to do something and then send the control back to the JavaScript. At least that is what I am trying to achieve. In this case the "echo $src1" statements is not doing anything. I was expecting it to populate the page with the $src1 value. Commented Oct 19, 2013 at 6:26
  • 1
    The $src1 value is some text and $src2 is some text 2. It IS populating those data Commented Oct 19, 2013 at 6:36
1
$("#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

1
  • Thanks, but this did not help. Commented Oct 19, 2013 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.