How to get "data" from jQuery Ajax requests

this is the code i have on index.html:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script>
            $.ajax({
                type: "POST",
                url: 'test.php',
                data: "check",
                success: function(data){
                    alert(data);
                }
            });
        </script>
    </head>
    <body>
        <div></div>
    </body>
</html>

How do I program test.php to get the "data" that is sent to the AJAX API?

+4
source share
6 answers

You are asking a very simple question here. You must complete some Ajax tutorials first. Just to help you a bit (if you know the GET and POST methods for sending data), the "data" in the data: the "check" is different from the "data" in the function (data), are different. For clarity, you should call them different:

$.ajax({
     type: "POST",
     url: 'test.php',
     data: "check",
     success: function(response){
         alert(response);
     }
});

, , test.php POST, - , test.php . , , POST test.php, , ( , "type" :

$.ajax({
     type: "POST",
     url: 'test.php',
     data: {"type":"check"},
     success: function(response){
         alert(response);
     }
});

, , -.

, test.php :

if(isset($_POST['type'])){
  //Do something
  echo "The type you posted is ".$_POST['type'];
}

: ", , - ". , "type" AJAX.

+3

 $.ajax({
    type: "POST",
    url: 'test.php',
    data: {"data":"check"},
    success: function(data){
        alert(data);//This will alert Success which is sent as the response to the ajax from the server
    }
 });

test.php

if(isset($_POST['data']) && $_POST['data'] == 'check'){
  //$_POST['data'] contain the value that you sent via ajax
  //Do something
  echo 'Success';
}

+7

jquery ajax. json.

.

$.ajax({
    type: "POST",
    url: 'test.php',
    dataType: 'json',
    data: {"data":"check"},
    success: function(data){
        alert(data.value1);
        alert(data.value2);
    }
 });

PHP-

if(isset($_POST['data']) && $_POST['data'] == 'check'){
   //Data 1
     $data['value1'] = 'Data 1 Got Successfully';
    //Data 2
     $data['value2'] = 'Data 2 Got Successfully';
     $resonse = json_encode($data);
     echo $response;
}
+5

HTML

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script>
     $.ajax({
     type: "POST",
     url: 'test.php',
     data: {"myvar":"check",},
     success: function(data){
         alert(data);
     }
     });
 </script>
</head>
<body>
<div></div>
</body>
</html>

, - , . , .

, ajax, test.php:

<?php
if(isset($_POST['myvar']))
{
  $myVariable = $_POST['myvar'];
  echo $myVariable;   //This would output the string passed in ajax, check
}
?>

$_ POST , AJAX. GET, php $_GET. $_REQUEST, , AJAX GET POST.

+2
$.ajax({//create an ajax request to load_page.php
    type: "POST",
    url: "test.php",
    data:{"data":"check"},
    success: function(data) {
        if (data) {

           alert(data);
        }
        else {
            alert('Successfully not posted.');
        }
    }
});

test.php

<?php 

if(isset($_POST['data'])){
    echo 'successful';
}
?>
+2

PHP AJAX succes.

+1

Source: https://habr.com/ru/post/1569391/


All Articles