0

Here I have a simple form:

<form class="form" id="loginForm">
                <div class="form-group">
                    <label for="affiliate">Affiliate</label>
                    <select class="form-control" id="affiliate" name="affiliate">
                        {{#list locations}}{{name}}{{/list}}
                    </select>
                </div>
                <div class="form-group">
                    <label for="uname">Username</label>
                    <input type="text" class="form-control" name="uname" id="uname">
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" class="form-control" id="password" name="password">
                </div>
                <button type="submit" class="btn btn-lg float-right" id="btnLogin">Login</button>
            </form>

Then I prevent the default action from happening by:

    $('form').submit(function(event) {
    event.preventDefault();

    //attempt to log in to the system
    attemptLogin(event)
});

Inside the attemptLogin method

function attemptLogin(form){

//convert our form data to an array, then convert that array to JSON
var formData = JSON.stringify($('#loginForm').serializeArray());

//url for login, in future should be grabbed from action of form
var url = "http://www.rsbmat.com/api/login.php";

//debug statement
console.log(formData);

//make an ajax call to the server
$.ajax({
    //sent header information with ajax call, so app and server know that information can be sent back
    header: {'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': "Content-Type, Origin, Accept, X-Requested-With"},

    //method to send data to server
    method: "POST",

    //url the data will be sent
    url: url,

    cache: false,

    //data we are sending
    data: formData,

    //allow cross domain
    crossDomain: true,

    //type of content going to be sent
    contentType: "application/json",

    //type of data we are receiving
    dataType: "json",

    //this is called BEFORE the ajax call is sent
    beforeSend: function(xhr){
        xhr.withCredentials = true;
    }
    //when it's finished what do we do with data sent back
}).done(function(data){
    console.log(data);
});

}

And on the PHP side, I've tried print_r, echo, var dump and json_encode of the POST array, however it's always empty. The form data is not being sent. Before I send the ajax request, I print the json data to make sure it is indeed there:

[{"name":"affiliate","value":"South King County"},{"name":"uname","value":"testuser"},{"name":"password","value":"testpassword"}]

And the data is there however, the information received from the server is still empty.

<?php
header("Content-Type: application/json", true);
require_once("../../db.php");

//echo $_POST;
//print_r($_POST);
echo json_encode($_POST);
//var_dump(json_decode(file_get_contents("php://input")));

I have everything commented out to just get the POST to print.. It does echo back to the app, I can see it in console.log, however it is empty.

7
  • 1
    What's inside login.php? Also, check the network log in the browser and see if the data is sent. Commented May 29, 2018 at 2:36
  • Remove JSON.stringify just use serializeArray() or serialize(). Commented May 29, 2018 at 2:44
  • I added the small login.php Commented May 29, 2018 at 2:45
  • 1
    Note you can't send Access-Control-Allow-* headers from the client, the server sends them to the client. Also if you are trying to post cross domain you can't unless they use the right cors headers Commented May 29, 2018 at 2:45
  • If you really want to use $_POST the same as a standard form use serialize() and leave contentType as default which is form encode Commented May 29, 2018 at 2:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.