There was a need to make a "script" on the PCP page that will send back the json file to the sent request:

{"count":/\*любая цифра\*/} 

How to implement this so that php can still verify the data? js sends via $.ajax

  • What do you mean by the phrase "so that php can still check the data"? - Artem Y

2 answers 2

Do this:

Js

 $.post('/url/', data ,function(response){ response = JSON.parse(response); switch (response['status']){ case 1: $('#block').html(response['count']); break; case 0: console.log('Произошла ошибка'); break; } }); 

Php

 <? // Получаете данные switch (gettype($_POST['data'])){ case string: // Делаете действия с данными break; case object: // Тут тоже можно что-то делать break; } 

That is, at the beginning, you need to know the type of data before checking. Type validation is always a good thing.

  • Either String or Object - Mykola Kіkec
  • @ МиколаКікець, I corrected the BackEnd script for you a little bit. Replaced the unnecessary with something that really helps in your situation. And if you need more types - php.net/manual/ru/function.gettype.php - Roman Kozin
 <?php $number = 5; $res['count'] = $number; echo json_encode($res); ?> 
  • one
    Add some explanation to the answer. - Abyx
  • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky