B
B
Basil2832020-04-15 17:59:42
JavaScript
Basil283, 2020-04-15 17:59:42

WAMP ajax server not working?

Help! On the server, I created a test folder and placed the index.php files in it, in which the following code:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script src="jquery-3.3.1min.js"></script>
    <script src="js.js"></script>
</head>
<body>
<h1>Нажать</h1>
 
<?php
echo 'Данные приняты - '.$_POST['text'];
 
?>
 
</body>
</html>

and the js.js file with the following code:
$(function() {
    $('h1').click(function () {
        $.ajax({
            url: 'index.php',         /* Куда пойдет запрос */
            method: 'post',             /* Метод передачи (post или get) */
                     /* Тип данных в ответе (xml, json, script, html). */
            data: {text: 5},     /* Параметры передаваемые в запросе. */
            success: function(data){   /* функция которая будет выполнена после успешного запроса.  */
                console.log(data);            /* В переменной data содержится ответ от index.php. */
            }
        });
});
});

When you click "Press", the following message appears in the console:
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script src="jquery-3.3.1min.js"></script>
    <script src="js.js"></script>
</head>
<body>
<h1>Нажать</h1>
 
Данные приняты - 5
</body>
</html>

, i.e. ajax seems to work. But here's an error in the browser: Notice: Undefined index: text in D:\Wamp\www\tost\index.php on line 12Call Stack#TimeMemoryFunctionLocation10.0008356792{main}( )...\index.php:0. The number 5 is not displayed.

Maybe my WAMP is somehow not working properly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2020-04-23
@HeadOnFire

Errors need to be read, they contain a detailed description of the problem. Let's break this error into pieces:

Notice: Undefined index: text in D:\Wamp\www\tost\index.php on line 12

1. Notice:- an error of the notice level, in fact, not even an error, but a notification.
2. Undefined index: text- index "text" does not exist, this means that you are trying to access the index "text" of some array, but such an index (array element) does not exist.
3. D:\Wamp\www\tost\index.php on line 12- and here is the specific place in your code where this error occurred. This is line 12, it contains the following code: echo 'Данные приняты - '.$_POST['text'];
What is wrong with this code? You are trying to output the element of the $_POST array at index "text", but before you have sent the data with this very POST method. To avoid this, you need to check:
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script src="jquery-3.3.1min.js"></script>
    <script src="js.js"></script>
</head>
<body>
<h1>Нажать</h1>
 
<?php
if (isset($_POST['text'])) {
    echo 'Данные приняты - ' . htmlspecialchars($_POST['text']);
} ?>
 
</body>
</html>

In this case, we first check that the data has actually been sent, and only then we output it. Also, we use https://www.php.net/manual/en/function.htmlspecial... to be on the safe side.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question