I have a problem with a piece of code that I use to check if a function returns a string that is equal to another string. The problem sounds trivial, but I cannot seem to get it to work.
Here is the code, and make sure you yell at me if you'll need any more code.
<script>
function emptyElement(x){
_(x).innerHTML = "";
}
function login(){
var e = _("email").value;
var p = _("password").value;
if(e == "" || p == "") {
_("status").innerHTML = "Fill out all of the form data";
} else{
_("loginbtn").style.display = "none";
_("status").innerHTML ='please wait...';
var ajax = ajaxObj("POST", "index.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
document.write(ajax.responseText); // returns "login_failed" when I try it
if(ajax.responseText == "login_failed"){
_("status").innerHTML = "Login unsuccessful, please try again.";
_("loginbtn").style.display = "block";
} else {
//window.location = "afterlogin.php?u="+ajax.responseText;
}
}
}
ajax.send("e="+e + "&p="+p);
}
}
</script>
The window.location function is commented out for testing purposes. Don't mind that. Below, I will post code for the two scripts that are used, "main" and "ajax".
function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
And the code for main.js
function _(x){
return document.getElementById(x);
}
I do not want to take credit for the original code; it is gathered from developphp.com and just altered slightly.
The problem is that, as I've commented, the line
ajax.responseText == "login_failed"
never seems to be true, not matter how I do. This is what I need guidance on.
Regards
It's too late for the answer, but someone may have the same issue.
You need to write
ajax.responseText == '"login_failed"';
Related
I am very new in programming websites I created a website already but am now attempting to add in a database. I have that all set up but ajax isnt working and I'm not sure what I'm doing wrong.
Here is my javascript code :
<script>
var ajax = new XMLHttpRequest();
var method = "Get";
var url = "data.php";
var asynch = true;
ajax.open(method, url, asynch);
ajax.send();
//recive ajax
ajax.onreadystatechange = function()
{
if(this.readyState == 4 && this.status == 200)
{
alert(this.responseText);
}
}
/*
function chbg(color, name) {
document.getElementById(name).style.backgroundColor = color;
document.getElementById(name + 'Chore').style.backgroundColor = color;
document.getElementById(name+ 'Bathroom').style.backgroundColor = color;
}
function openForm() {
document.getElementById("myForm").style.display = "block";
}
function closeForm() {
document.getElementById("myForm").style.display = "none";
}
*/
</script>
Here's the php all I'm trying to do right now is get the 2 to connect.
<?php
echo "Hello World";
?>
I didn't understand what you're exactly trying to do but your php file just echo something and he does not return something so your responseText property is empty.
I have a var that's value is set, with an if statement to check which value, then deploy depending on the statement.
function Sort() {
var Response = document.getElementById("ResponseDiv").innerHTML;
if (Response == "Verified.") {
alert("Verified");
}
else if (document.getElementById("ResponseDiv").innerText == "Not verified.") {
alert("Not verified");
}
else {
alert("Else: " + Response);
}
}
My code isn't landing on "Verified" or "Not Verified" instead it lands on the else statement. I went through several times thinking I had a typo but isn't the same.
The ResponseDiv is filled by this (which works fine):
function Check() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://' + Http + '/ID_' + ID + '.html', false);
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
document.getElementById("ResponseDiv").innerHTML = xhr.responseText;
//setTimeout(Sort, 200);
Sort();
}
}
};
xhr.send(null);
}
My div gets the correct data, and the alert box in the else even gives me what I'm looking for, but lands on the else.
Debugger shows Response as coming back as "Verified.\n", but in no other way. However statement works fine when coded as if(Response == "Verified.\n").
I have problem with window.location. So I have ajax request as follows:
function login(){
var u = document.getElementById("username").value;
var p = document.getElementById("password").value;
if(u == "" || p == ""){
} else {
var ajax = ajaxObj("POST", "ajaxResp.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "login_failed"){
alert("fail");
}
else {
window.location = "user.php?u="+ajax.responseText;
}
}
}
ajax.send("u="+u+"&p="+p);
}
return false;
}
where ajaxObj is decleared in other file. My ajaxResp.php send me back username to which i should redirect. But when I reach window.location, my request is cancelled. What can it mean?
If the window.location line is being hit. you are not waiting for the Ajax call to be complete.
That means whatever the code is inside of the ajaxReturn is not checking for readyState to be complete.
I'm trying to get a notification box to pop up on errors/successes and then hide after 5 seconds. The problem is that it shows up, hides, then repeats 3 times. It's annoying and I'm sure it will bug the user.
I thought maybe my javascript was bad, so I tried jquery, still the same result.
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();
x.open(meth, url, true);
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
function login() {
var u = _("user").value;
var p = _("pass").value;
var status = _("status");
var ajax = ajaxObj("POST", "login_script.php");
ajax.onreadystatechange = function(){
if(ajax.responseText !== "success"){
status.innerHTML = ajax.responseText;
_('status').className = "nNote nFailure hideit";
var svalue = _('status').innerHTML;
_('status').innerHTML = svalue + '<span style="float:right; margin-right:20px; font-size:10px;" onclick="hideStatus()"> X </span>';
$( document ).ready(function() {
$('#status').slideDown(1000).delay( 5000 ).slideUp(1000);
});
}
};
ajax.send(
"u=" + u
+ "&p=" + p);
}
If you want to see what it's doing, go to this site and click "login"
Change:
if(ajax.responseText !== "success"){
to:
if(ajaxReturn(ajax) && ajax.responseText !== "success"){
You have this function for checking whether the AJAX request is complete, but you forgot to call it.
leave the function when ajax.readyState!==4
You are calling this function when the onreadystatechange-event fires, but this event will fire multiple times(when readyState===4 the request is complete)
But this question is tagged with "jquery", you better use jQuery.ajax()
I've been trying to create my own custom LiveValidation (LiveValidation.com) function that connects to a database and checks if a username already exists. This is the relevant part of the form:
Username: <input type="text" name="username" id="username" class="textinput">
<script type="text/javascript">
var username = new LiveValidation('username');
username.add( Validate.Presence );
username.add( Validate.Length, { minimum: 3, maximum: 12 } );
username.add( Validate.Username );
</script>
This is my Validate.Username function:
Validate.Username = function(value, paramsObj){
var paramsObj = paramsObj || {};
var message = paramsObj.failureMessage || "Sorry that username is taken!";
var http = new XMLHttpRequest();
var url = "usernamecheck.php";
var params = "username="+value;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
if(http.responseText == 'yes') {
return true;
} else {
Validate.fail(message);
return false;
}
}
}
http.send(params);
}
Here's the relevant part of usernamecheck.php:
if(mysql_num_rows($query) != 0 && $query) {
echo "no";
} else if ($query) {
echo "yes";
} else {
echo "err";
}
The function seems to work fine (if I put alert('fail') above Validate.fail(message); I get an alert when a used username is used), but LiveValidation never shows the error message. Anyone know what I've done wrong?
Thanks
Hmmm... just looking at it real quick, could it be that "message" is not defined in your function ? It is defined in the parent function.