Home > Mobile >  jquery ajax data sending not working with php
jquery ajax data sending not working with php

Time:06-01

i'm trying to send some data from jquery ( $.ajax ) to php file but it doesn't work. in the success function it always shows the right answer but when i verify the POST in php file with var_dump it shows an empty string. i tried to replace $.ajax with $.post and $.get but none of them works. here is my jquery code :

$('#sbt').on('click', function() {
    var email = $('#lgEmail').val();
    var pass = $('#lgPassword').val();
    $.ajax('../loginRegister.php', {
        type: 'GET', // http method
        data: {
            mail: email,
        }, // data to submit
        error:console.error,
        success: function(data, textStatus, xhr) {
            alert("done")
        },
        error: function(jqXhr, textStatus, errorMessage) {
            alert(errorMessage)
        }
    });
})

and here is my php code :

if (isset($_POST['mail'])){
    echo $_POST['mail'];
}

CodePudding user response:

Since you're using type: 'GET' you need to retrieve the parameter using $_GET['mail'].

You could also use $_REQUEST['mail']. $_REQUEST combines the parameters from $_GET, $_POST, and $_COOKIE.

CodePudding user response:

You are using type: 'GET' when you actually would need to use POST. Either remove the type argument and use $.post() or use $.ajax() and set it to the correct method, in this case type: 'POST'.

CodePudding user response:

Try this code:

 $('#sbt').on('click', function () {
            var email = $('#lgEmail').val();
            var pass = $('#lgPassword').val();
            $.ajax({
                type: "GET",
                url: "../loginRegister.php",
                data: { mail: email },
                datatype: "json",
                traditional: true,
                success: function (data, textStatus, xhr) {
                    alert("done")
                },
                error: function (jqXhr, textStatus, errorMessage) {
                    alert(errorMessage)
                }
            });
        })
  • Related