PDA

View Full Version : registration using xhr


emath
08-28-2017, 04:26 AM
Hey,

I'm trying to implement registration with ajax/xhr request:


$.post({
url: './register.php?do=addmember',
data: formData,
success: function(res) {
console.log(res);
},
dataType: 'html',
processData: false,
contentType: false,
});


formData has all the data needed.

The user is successfully registered yet when I am refreshing the page, I am not logged in.
The session cookie is set successfully as well.

When I try to register the usual way, everything works fine.

What am I missing? is there something needed in the server side after the registration to acknowledge the cookie ?

Any help will be appreciated.

emath
08-29-2017, 04:21 PM
I've done it successfully with XHR, If someone else will need to do this, here is an example code :


var xhr = new XMLHttpRequest();
var formObjData = {
timezoneoffset: 2,
dst: 2,
'options[adminemail]': 1,
'options[showemail]': 1,
do: 'addmember',
password: $("input[name='password']")[0].value,
passwordconfirm: $("input[name='passwordconfirm']")[0].value,
username: $("input[name='username']")[0].value,
agree: $("#agree")[0].checked,
email: $("input[name='email']")[0].value,
emailconfirm: $("input[name='email']")[0].value,
securitytoken: 'guest'
};
var strObj = '';

for (var k in formObjData) {
if (formObjData.hasOwnProperty(k)) {
var val = formObjData[k];
strObj = strObj + `${k}=${val}&`;
}
}
strObj = strObj.slice(0, strObj.length - 1);
xhr.open('POST', './register.php?do=addmember', true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
// sucess code
} else {
if (xhr.status !== 200) {
alert('error);
}
}
}
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(strObj);