0

i am trying to send datas from ajax to php, but php doesnt show it. both scripts are on the same site: "testpage.php".

jQuery/Ajax:

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
  $(document).ready(function() {
    $("#button").click(function() {

      var test = "bla";
      $.ajax({
        url: "testpage.php",
        type: "post",
        data: test,
        success: function (response) {
          alert("test ok");
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log(textStatus, errorThrown);
        }
      });
    });
  });
</script>

PHP:

      <?php
        if(isset($_POST["test"])) {
          $test2 = $_POST;
          echo $test2;
          echo "Test";
        }
      ?>

I do not see the result of PHP

Basssprosse
  • 334
  • 1
  • 3
  • 17
  • 2
    `data: {test:test},` – Saty Oct 13 '15 at 12:49
  • use data: {test:test}, & alert your response to see your result. success: function (response) { alert(response); }, Or Just append your response to html. – Husnain Aslam Oct 13 '15 at 12:52
  • Possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Jan Oct 13 '15 at 13:01
  • With using data: {test:test}, my response is my whole HTML/PHP-code. is this correct? – Basssprosse Oct 13 '15 at 13:04
  • 1
    When will finally all questions about sending data from ajax to PHP be answered? It's like 5 questions a day... – Tomáš Zato Oct 13 '15 at 13:10
  • @Chpo7234 you should not get HTML in your response. You should only see the value of `$test2` and the string `"Test"`. Are you still getting those bad results? – CodeGodie Oct 13 '15 at 13:17
  • No, thank you! Works fine! – Basssprosse Oct 14 '15 at 11:24

5 Answers5

0

You need to use data: {test:sometext} if you want to do a POST request.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

The PHP can't see the value in the post because you only send bla in the PHP body. You have to send test=bla. But jQuery can do it automatically by sending data : { test : test }.

$(document).ready(function() {
    $("#button").click(function() {

        var test = "bla";
        $.ajax({
            url: "testpage.php",
            type: "post",
            data: {
                test : test
            },
            success: function (response) {
                alert("test ok");
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(textStatus, errorThrown);
            }
        });
    });
});
Magus
  • 14,796
  • 3
  • 36
  • 51
0

use data: {test:test}, & alert your response to see your result.

success: function (response) {
   alert(response);
},

Or Just append your response to html.

Husnain Aslam
  • 865
  • 1
  • 11
  • 28
0

You can use serialize() method into your $.ajax();

Something like:

$.ajax({
    url: "testpage.php",
    type: "post",
    data: $("#myForm input").serialize(),
    success: function (response) {
      alert("test ok");
    },
    error: function(jqXHR, textStatus, errorThrown) {
      console.log(textStatus, errorThrown);
    }
  });

Then try to accessing your post data by form input name.

Sergio Ivanuzzo
  • 1,820
  • 4
  • 29
  • 59
-1
    var data = {
        'test':  "bla",
    };

    $.ajax({
        type: "POST",
        url: "testpage.php",
        data: data,
        dataType: "json"
    }).done(function(response){
        console.log(response);
    }).fail(function(response){
        console.log(response);
    });
Sergynyo
  • 11
  • 3