Pass form values to Ajax

1

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

    
asked by anonymous 18.07.2015 / 23:09

2 answers

2

You can use $('SeletorParaOSeuForm').serialize() of Jquery to easily receive information from your form.

Example

            $.ajax({
            url: url + "/login/ajaxLogin",
            type: 'POST',
            data: $('#idSeuForm').serialize(),
            success: function (obj) {
            });

The values of the fields of your form will be sent in the request, the name of the variable will be the value of the "name" attribute of your fields.

In your PHP values can be redeemed as follows:

$_POST["seuName"]

where seuName is the value of the name attribute of the form field you want to retrieve

    
19.07.2015 / 07:13
2

Remember to send form values to and configure the upload method

$(function() {
if ($('#javascript-ajax-button').length !== 0) {
    $('#javascript-ajax-button').on('click', function(){
    $.ajax({
        method: "post",
        url:  "/login/ajaxLogin",
        data: {login:valor, senha: valor}) 
    .done(function(data) {
                switch(data) {
                    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
            });
    });
}
});

Jquest ajax

    
18.07.2015 / 23:26