1

I have to say I saw a dozens of topics similar to my problem, but most of them were typos or spelling.

My PHP file does not seem to receive any data from AJAX POST function (I'm getting Undefined Index). Here is my code:

var login = $("#log_l").val();
var pwd = $("#pwd_l").val();
alert(login);
alert(pwd);
$.ajax({
    type: "POST",
    url: 'app/menu/login.php',
    data: {
        login1: login,
        pwd1: pwd,
    },
    cache: false,
    success: function (data) {
        if (data == 'fail') {
            alert('test2');
            $('#failure_log').show();
        }
        else {
            document.location = " {$conf->action_root}postlogin";
        }
    }
});
return false;

And the login.php:

$login = $_POST['login1'];
$haslo = $_POST['pwd1'];
...

I tried with both:

login1:login,
pwd1:pwd,

And:

'login1':login,
'pwd1':pwd,

But in both ways I receive "Undefined index" error in .php file on 'login1' and 'pwd1' variables. I checked that ajax 'reaches' file and it even receives the echos from .php file, but the variables are not sent/received.

Do you have any ideas what might be wrong?

Mirza Sisic
  • 2,195
  • 4
  • 23
  • 31
Macjej
  • 87
  • 8
  • Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Jun 14 '16 at 14:22
  • **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jun 14 '16 at 14:22
  • I'm not getting any errors in the browser.. Only info "navigated to: ..." – Macjej Jun 14 '16 at 14:26
  • Not the browser - the browser's developer tools. – Jay Blanchard Jun 14 '16 at 14:28
  • 1
    `var_dump($_POST, $_SERVER['REQUEST_METHOD'])` and see what's arriving at the server. – Marc B Jun 14 '16 at 14:29
  • Yes, sorry i ment the browser console. – Macjej Jun 14 '16 at 14:29
  • @MarcB I'm getting: array(2){[login1]=>string(3)"log" ["pwd1"]=> string(3) "pwd". – Macjej Jun 14 '16 at 14:37
  • So it does work somehow.. – Macjej Jun 14 '16 at 14:38
  • Set **login.php** to be `'.print_r($_POST, true).''; ?>` and take a look at http://stackoverflow.com/a/21617685/2191572 – MonkeyZeus Jun 14 '16 at 14:51
  • Not related to the undefined index probably but unless your JS is being echoed out by PHP this doesn't look right: `document.location = " {$conf->action_root}postlogin";` You probably want something like `document.location = "= $conf->action_root; ?>postlogin";` ... if the JS **is** being echoed out by PHP there's a chance that `$.ajax` is causing a hiccup. – CD001 Jun 14 '16 at 14:56
  • @MonkeyZeus If i did everything right it just shows that the content can't be displayed – Macjej Jun 14 '16 at 17:15

1 Answers1

0

You need to use file_get_contents to capture ajax calls or you need to define. Check this

How to get JSON data in php ?

Or

you need to set the ajax call as form_encoded_url contentType: "application/x-www-form-urlencoded",

refer this link Submitting HTML form using Jquery AJAX

Alaksandar Jesus Gene
  • 4,587
  • 9
  • 39
  • 62