Hello, I am trying to send a POST variable to the controller in PHP via AJAX:

$(".inp_pr").keypress(function(f) { if (f.which == 13) { dataString = 'qwe'; $.ajax({ type: "POST", url: "/prwrk/", data: 'dataString=' + dataString, success: function(data) { alert('<?php echo($data)?>'); } }); event.preventDefault(); } }); 

Controller Code:

  function action_index() { $data=$_POST['dataString']; $this->view->generate('prwrk_view.php', 'template_view.php',$data); } 

AJAX successfully sends the variable, but it does not come to the PHP controller. There are suspicions that I do not correctly specify the Url, but even prescribing a direct path to the desired controller, the variable is not transmitted.

Router code:

 <?php class Route { static function start() { $controller_name = 'Main'; $action_name = 'index'; $routes = explode('/', $_SERVER['REQUEST_URI']); if ( !empty($routes[1]) ) { $controller_name = $routes[1]; } if ( !empty($routes[2]) ) { $action_name = $routes[2]; } $model_name = 'Model_'.$controller_name; $controller_name = 'Controller_'.$controller_name; $action_name = 'action_'.$action_name; $model_file = strtolower($model_name).'.php'; $model_path = "application/models/".$model_file; if(file_exists($model_path)) { include "application/models/".$model_file; } $controller_file = strtolower($controller_name).'.php'; $controller_path = "application/controllers/".$controller_file; if(file_exists($controller_path)) { include "application/controllers/".$controller_file; } else { Route::ErrorPage404(); } $controller = new $controller_name; $action = $action_name; if(method_exists($controller, $action)) { $controller->$action(); } else { Route::ErrorPage404(); } } function ErrorPage404() { $host = 'http://'.$_SERVER['HTTP_HOST'].'/'; header('HTTP/1.1 404 Not Found'); header("Status: 404 Not Found"); header('Location:'.$host.'404'); } } ?> 

How to send a variable to a PHP controller from AJAX?

  • more information can you add? if you follow your url '/ prwrk /', we will get into action_index? in firebug when sending ajax error is there any? or just everything passes, but in the action_index $ _POST empty? What is your alert in success returns? - sam3434
  • Yes, if you go to url '/ prwrk /', we immediately get into action_index. There are no errors in the debugger, it says that the post request was successfully sent. Alert in success returns null. Inserted into action_index code if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $data='получено'; } else{ $data='нет'; } if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $data='получено'; } else{ $data='нет'; } if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $data='получено'; } else{ $data='нет'; } , now I always get "No" in the success alert - Ivan Ivanov
  • By the way, did not pay attention. you have a strange alert - alert ('<? php echo ($ data)?>'); it will also show you the value of the $ data variable at the moment you just went to the page, without ajax. to get the result of the ajax query (i.e. what action_index returns), you need to use alert (data) - sam3434
  • alert (data) returns me the html-code of the current page ... - Ivan Ivanov
  • do this replace your action_index with function action_index () {print_r ($ _ POST);}, replace your alert with alert (data) and see what output sam3434

0