How do I pass form values to PHP with Ajax?
JS (without passing values)
$(function() {
if ($('#javascript-ajax-button').length !== 0) {
$('#javascript-ajax-button').on('click', function(){
$.ajax(url + "/login/ajaxLogin") // ISSO É UMA URL, E NÃO UM ARQUIVO
.done(function(result) {
switch(result) {
case 'This user does not exist':
alert("This user does not exist");
break;
default:
alert("Sucesso!");
}
})
.fail(function() {
// this will be executed if the ajax-call had failed
})
.always(function() {
// this will ALWAYS be executed, regardless if the ajax-call was success or not
});
});
}
});
PHP
class Login extends Controller {
public function index() {
//if($this->model->isUserLoggedIn())
//header('Location: ' . URL . 'me');
$news = $this->model->latestNews();
require APP . 'view/_templates/header.php';
require APP . 'view/login/index.php';
require APP . 'view/_templates/footer.php';
}
/**
* AJAX-ACTION: AjaxLogin
* TODO documentation
*/
public function ajaxLogin()
{
// QUERO QUE OS VALORES SEJAM REGASTADOS AQUI
$errors = $this->model->doLoginWithPostData($_POST['user_name'],
$_POST['user_password']);
// simply echo out something. A supersimple API would be possible by echoing JSON here
echo $errors;
}
}
I am using MVC, and the ajax url is not for a file