The simplest ajax request does not work. When you click on the button should be sent a letter. When you click on the button, the first alert is triggered. Then the second alert (data) pops up and it is empty, but the test.php handler does not work. It does not work with both direct and relative paths. Help, I am desperate!

Code

function klikaj() { alert('ok'); var name=3; var phone=6; $.ajax({ url: 'http://exmaple.xyz/test.php', data: {phone:phone, name:name}, type: 'POST', success: function(data){ alert (data); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Handler code
echo $_POST['name'];

mail("milllanikev@gmail.com", "My Subject", "Line 1\nLine 2\nLine 3");

There is an assumption that this is due to the single entry point located in index.php

 if ($_SERVER['REQUEST_URI'] == '/') { $Page = 'index'; $Module = 'index'; } else { $URL_Path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $URL_Parts = explode('/', trim($URL_Path, ' /')); $Page = array_shift($URL_Parts); $Module = array_shift($URL_Parts); if (!empty($Module)) { $Param = array(); for ($i = 0; $i < count($URL_Parts); $i++) { $Param[$URL_Parts[$i]] = $URL_Parts[++$i]; } } } if ($Page == 'index') include('page/index.php'); else if ($Page == 'regreg') include('page/regreg.php'); else if ($Page == 'editproject') include('page/editproject.php'); else if ($Page == 'regcom') include('page/regcom.php'); else if ($Page == 'regprofile') include('page/regprofile.php'); else if ($Page == 'addproject') include('page/addproject.php'); else if ($Page == 'delmember') include('page/delmember.php'); else if ($Page == 'addmember') include('page/addmember.php'); else if ($Page == 'account') include('acc/account.php'); else if ($Page == 'search') include('page/search.php'); else if ($Page == 'comm') include('page/comm.php'); else if ($Page == 'mail') include('mail.php'); 
  • Do you want to get rid of else / if? A more readable switch / case just fits - I. Kotov

3 answers 3

Never debuff so

 alert (data); 

always debazhe

 console.log(data); 

And look in the console.

Next, look at the console where ajax goes and what it returns. Try to just go directly to http://exmaple.xyz/test.php , to make sure that the requests go where they need to, you can write the return "да это я"; line in test.php return "да это я"; to check.

  • 3
    Debugger usually via debugger with breakpoints, not via console)) - Alexey Shimansky
  • advice based on "alert for sure", "console.log ()" - just as quickly and simply + does not stop the script - Maksym Tymofeiev
  • Rather, you need to correctly write not "debug", but "check". + add something about debag once, IMHO - Alexey Shimansky
  • "debug" vs "check"? In the context of this task the author does not see the difference. Is it just that the word "debazhte" is not in Russian. But I think this does not affect the understanding of the answer and its usefulness for the author. - Maksym Tymofeiev

it was necessary to register the file path in index

 else if ($Page == 'test') include('test.php'); 

And in ajax url: '/test'

  • one
    It is not the answer to the question. To leave your comments or ask the author to clarify, leave a comment to the appropriate post. - From the queue of checks - Sasha Omelchenko
  • @SashaOmelchenko This is the author himself and writes, so your comment may puzzle him a little :) - Yaant
  • @Yaant well this is a standard comment. In general, I rather urge to give a slightly more detailed answer, which may be useful to other participants) - Sasha Omelchenko
  • one
    Describe the solution to the problem in more detail. Now your answer looks like a comment. - Yuri

This code will not work. Surely you have $ .ajax is not a function in your browser console. (Browser console opens by pressing F12)

You first need to connect Ajax

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

and then call functions

However, you did not give the error codes that you have in the console so no intelligible answer will give you the answer.

In the file http://exmaple.xyz/test.php, I recommend you comment out the entire code and write only 2 test lines for the test.

 <?php print_r($_POST);