When you press one of the 300 buttons, the parameter should be transmitted by the Ajax.

Code:

$(document).on('click', '.lot-not-finished .lottery-member.empty, .lot-not-finished .take-part-btn', function() { var place = $(this).data('place'); ajax({thisPage: true, place: place}, function (data) { $('.user-balance').text(data.balance); $.notify(data.msg, "success"); FULL_LOT.myPlacesCount++; $('.myPlacesCount').text(FULL_LOT.myPlacesCount + n2w(FULL_LOT.myPlacesCount, SLOTS_TEXT)); MyLotsManager.add(FULL_LOT.info, 0); }, false); return false; }); 

The AJAX function itself:

 function ajax(data, success, error) { var url = data.thisPage ? '' : '/ajax'; delete data.thisPage; $.ajax({ url: url, type: 'POST', dataType: 'json', data: data || {}, success: function (data) { if (data.status == 'success') { success && success(data) } else { if(data.msg) $.notify(data.msg, "error"); } }, error: function () { $.notify(LANG['ajaxError'], "error"); console.log(data); console.log(error); error && error() } }) } 

I have already created a folder on the server / ajax and put index.php there and entered everybody into it .... Then I redirected the script to the ajax folder .. I can’t understand if my PHP file is receiving a value or not.

I already thrust there:

 <?php echo $_POST['place'] $msg = 'chetko'; echo 'success'; $success = true; echo $msg; echo json_encode($msg); echo 'chetko'; $status = 'success'; echo json_encode($status); echo $status; echo json_decode($status); echo '111'; ?> 

Who can explain to me what should be in my PHP file so that I still get this place value? Ajax it is sent - I see it in the console, when I press the button. But then nothing happens and I get an error.

I have been suffering for almost 2 hours, and I can’t understand if my PHP is accepting anything at all, and how to give Ayaks back the answer that he accepted .. :(

  • Аяксом ΠΎΠ½ΠΎ отправляСтся - я Π²ΠΈΠΆΡƒ Π² консоли Π΅Π³ΠΎ, ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ. screen request structure show. - Visman
  • Moral - first check what network activity is from your request. In browsers there are tools that show requests, with headers and so on. - D-side

2 answers 2

I'm confused by this

 ajax({thisPage: true, place: place}, function (data) { ... 

and then

 var url = data.thisPage ? '' : '/ajax'; 

it turns out you have an empty url is transmitted, that is, the request goes to index.php in the root of the site, and not in the folder / ajax. I advise you to fully url. Get the file ajax/ajaxpool.php and match var url = "/ajax/ajaxpool.php"; This is for starters.

Further you write that the format of the transmitted data is json. Well, better encode (serialize) your object in json

 ... data: JSON.stringify(data) || "{}", ... 

Also, this JSON must be recognized on the server. This is done like this.

  $json = file_get_contents('php://input'); // всС Π²Ρ…ΠΎΠ΄Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ $data = json_decode($json, true); // true - Π² ассоциативный массив, Π° Π½Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ 

If you want to use $_POST['place'] , then you need to transfer data not in the JSON format, but in the application/x-www-form-urlencoded format - the html form. In this format, the string looks like this:

 thisPage=true&place='myplace' 

that is, the pair имя1=Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅1&имя2=Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅2

And finally, to understand whether the script works, you can simply write something to the file. For example:

 file_put_contents("log.txt", "Π‘ΠΊΡ€ΠΈΠΏΡ‚ ΠΏΠΎ ΠΊΡ€Π°ΠΉΠ½Π΅ΠΉ ΠΌΠ΅Ρ€Π΅ запустился"); 

If there are additional questions - write in the comments.

    If you specified dataType: 'json' in the request, then you should give a valid JSON:

     print json_encode(array( "status" => "ok", "POST" => $_POST ))