Pass form values to Ajax

Asked

Viewed 2,375 times

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;
    }
}

NOTE: I am using MVC, and the ajax url is not for a file

2 answers

2


You can use the $('SeletorParaOSeuForm').serialize() 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 variable name will be the value of the attribute "name" of its fields.

In your PHP values can be redeemed as follows:

$_POST["seuName"]

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

  • How do I get this data in php?

  • $_POST["your name"], where your name attribute is value of the form field you want to recover

2

Remember to send the form values by 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

  • you don’t need to do any editing in php? , could you adjust my code so I can send the values?

  • Just change the $_POST for this->input->post('nome') @misakie

  • But I’m not using code Igniter '--', by the way, no framework

  • @misakie hahah this code there has a CI face, it was bad

  • Could you leave me a complete example, so I can understand better?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.