0

Form :

<form method="post" id="loginForm">
    <div class="form-group">
        <label for="email-signin">Email address:</label>
        <input type="email" class="form-control" id="email-signin" name="email-signin">
    </div>
    <div class="form-group">
        <label for="pwd-signin">Password:</label>
        <input type="password" class="form-control" id="pwd-signin" name="pwd-signin">
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default" id="signIn" name="signIn">Sign In</button>
    <div id="error">
        <!-- error will be shown here ! -->
    </div>
</form>

jquery :

$("#signIn").on("click", function(e) {

   e.preventDefault();

    var values = $("#loginForm").serialize();

    console.log( values );

    $.ajax({
        type: "POST",
        url: "../php/BusinessLayer/User.php",
        data: values,
        beforeSend: function() { $("#error").fadeOut() },
        success :  function(response)
        {
            console.log("Success");
            if(response=="ok"){

            }
            else{
                $("#error").fadeIn(1000, function(){
                    $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+response+' !</div>');
                });
            }
        }
});

php:

<?php

 session_start();

include ("../DataLayer/VO/UserVO.php");
include ("../DataLayer/DAO/UserDAO.php");

// Database Execution for User Related Request
$userDAO = new UserDAO();

print_r($_POST);

if(isset($_POST['signIn']))
{
  echo 'test2';

  $user = new UserVO();

  $user->setEmail(trim($_POST['email-signin']));
  $user->setPassword(trim($_POST['pwd-signin']));

  // Request signin
  $userDAO->signIn($user);
}

Using this code, my if(isset($_REQUEST['signIn'])) in my php file never returns true. I have tried multiple things, and nothing seems to work.

PS : I am using Jquery 1.12.4

Also, my print_r($_POST); returns an empty Array.

  • 1
    Just a sidenote Please use $_POST instead of $_REQUEST. – Vural Sep 28 '16 at 19:59
  • post the code where is `if(isset($_REQUEST['signIn']))` as a start – xYuri Sep 28 '16 at 19:59
  • `if(isset($_POST['email-signin']) && isset($_POST['pwd-signin']))` – xYuri Sep 28 '16 at 20:02
  • also your remember me checkbox wont work as it doesn't have a name – xYuri Sep 28 '16 at 20:03
  • the output of my print_r($_POST); is empty tho. I know a lot of other things won't work like checkbox etc, but this isn't my concern right now. –  Sep 28 '16 at 20:03
  • 1
    can you try `var_dump($_POST);` instead? – xYuri Sep 28 '16 at 20:08
  • yes, agree with @xYuri do a var_dump of $_POST and see what it gives you. Also, see my answer, because checking for the value of the button on the PHP side won't work. jQuery.serialize() doesn't work that way... :) – Native Coder Sep 28 '16 at 20:09
  • I fixed the issue with not having the button in the data : "email-signin=test%40ga&pwd-signin=test&signIn= welcome.js:47 Success". –  Sep 28 '16 at 20:11
  • Now for the var_dump($_POST);, I get array(0) { } –  Sep 28 '16 at 20:12
  • if what @NativeCoder, is true the you need to change your method, to somthing like this> `if submit pressed, get the values from the field1 with id=xx and the value of field2 with id=xx` (i hate js) – xYuri Sep 28 '16 at 20:12
  • @xYuri $('#formIdGoesHere').serialize() will work just fine for all the input fields. it just doesn't work for buttons :) – Native Coder Sep 28 '16 at 20:16
  • On the Javascript side, the parsing of the data is fine. It's on the PHP side, it seems that the POST isn't made successfully since it is empty –  Sep 28 '16 at 20:16
  • aha, well he dont need the button anyway, if he use `if(isset($_POST[''])){}` – xYuri Sep 28 '16 at 20:17
  • If the POST was successful, you'll have a populated $_POST superglobal, since you don't, I'd keep looking at the jQuery..... – Native Coder Sep 28 '16 at 20:20
  • 1
    Can you try pulling up the dev console, and going to the network tab? When you submit the AJAX request, the script that it's POSTing to will show up there. Click on it and you can view the submitted data as well as the response from the server. See what exactly AJAX is sending... – Native Coder Sep 28 '16 at 20:21
  • try this in your php file, `if(isset($_GET['test'])){die('it works');}else{die('it doesn\'t work :(');}` and put this `?test=1` to the end of the url in the `ajax` code – xYuri Sep 28 '16 at 20:23
  • @xYuri don't you think that's a stretch since his AJAX has the "type" option set specifically to "POST"? Worth a shot either way I guess... – Native Coder Sep 28 '16 at 20:24
  • if it echoes it doesnt work then u sure have something wrong with your jq – xYuri Sep 28 '16 at 20:25
  • @NativeCoder its an attempt to debug, and GET is easier than post cuz u can put to the url, and you will be 100% that its poted to the requested file, else then the post method is the problem, so you will exclude the php part – xYuri Sep 28 '16 at 20:27
  • @xYuri fair enough. :) I like your logic – Native Coder Sep 28 '16 at 20:29
  • @Marc-André well, am really confused, your php code has nothing wrong – xYuri Sep 28 '16 at 20:31
  • 1
    See my answer now. Try removing e.preventDefault() and putting return false; at the VERY END of the on click function. also, remove the method from the form (that way the form won't even try to submit, plus you don't need it since you are using AJAX) – Native Coder Sep 28 '16 at 20:36
  • @Marc-André try to post using very clear ajax, without anything except post values and `console.log("Success");` dont include `beforeSend: function() {` if nothing showed up in console then it doesn't work, the point is to use the basic method to avoid any kind of errors – xYuri Sep 28 '16 at 20:37
  • i like `remove the method from the form`, agree what is the point of method attr when it wont post?! – xYuri Sep 28 '16 at 20:39
  • @xYuri It still writes Success in my console, tho there's no way to check the value of the POST in the php. –  Sep 28 '16 at 20:45
  • @Marc-André this is out of my league, sorry, the last thing i can recommend you, is to use $_GET instead of $_POST, your using ajax so it wont be a big deal – xYuri Sep 28 '16 at 21:22

1 Answers1

1

jQuery's serialize function does not encode the values of buttons. Taken from here

NOTE: This answer was originally posted by slashingweapon

jQuery's serialize() is pretty explicit about NOT encoding buttons or submit inputs, because they aren't considered to be "successful controls". This is because the serialize() method has no way of knowing what button (if any!) was clicked.

I managed to get around the problem by catching the button click, serializing the form, and then tacking on the encoded name and value of the clicked button to the result.

$("button.positive").click(function (evt) {
    evt.preventDefault();

    var button = $(evt.target);                 
    var result = button.parents('form').serialize() 
        + '&' 
        + encodeURIComponent(button.attr('name'))
        + '='
        + encodeURIComponent(button.attr('value'))
    ;

    console.log(result);
});

As far as the var dump being empty on the PHP side, try using jQuery's .click instead of the .on event.

$('#signIn').click(function(){});

Also, remove the method from your form. It looks like the form may be submitting as soon as you click the button. Also, remove

e.preventDefault();

and place

return false;

at the VERY END of the on click function. return false does 3 things

  1. e.preventDefault()
  2. e.stopPropigation();
  3. return immdediatly
Community
  • 1
  • 1
Native Coder
  • 1,792
  • 3
  • 16
  • 34
  • Still no deffierence of results –  Sep 28 '16 at 20:39
  • @Marc-André are you 100% sure that you don't have a FileNotFound error? have you looked at the dev console to make sure that the data is being posted to the file? ("../php/BusinessLayer/User.php") – Native Coder Sep 28 '16 at 20:44
  • @NativeCoder I assumed it worked, since I did an echo('test') and I got test written on my error box –  Sep 28 '16 at 20:55
  • Where did you echo test? in the script that is being called via ajax? – Native Coder Sep 28 '16 at 21:08