watagal Posted May 27, 2008 Share Posted May 27, 2008 Greetings-My Code: var validation = ajaxCall('Html_php_code/validateSignIn2.php',"POST",'u='+userName+'&p='+password, false); // ajaxCall() returns xmlHttp.responseTextif (validation.substr(0,18) == "Invalid User Name:") {} The error I get via firebug is:validation has no propertiesHow can force the ajaxCall() return as a string object?thanks,-Gal Link to comment Share on other sites More sharing options...
Jesdisciple Posted May 27, 2008 Share Posted May 27, 2008 I would first try console.log()'ing validation to make sure you have what you think you have. If that works, try adding an empty string to it in the return statement. Link to comment Share on other sites More sharing options...
watagal Posted May 27, 2008 Author Share Posted May 27, 2008 Thanks Jesdisciple. In Firefly, under the console tab, then under the Response tab - it returns the string "Successful", which is the only thing echoed in my PHP file. var validation = ''+ajaxCall('Html_php_code/validateSignIn.php',"POST",'u='+userName+'&p='+password, false);if (validation.substr(0,18) == "Invalid User Name:") { document.getElementById('divInvalidUserName').style.display = "block"; document.getElementById('txtUserName').value = ""; } else if (validation.substr(0,17) == "Invalid Password:") { document.getElementById('divInvalidPassword').style.display = "block"; document.getElementById('txtPassword').value = ""; } else if (validation == "Successful") { window.location="index.php?p="+FRM_SIGNIN; } else { document.write(validation);} it should satisfy the third condition above, but it goes the the 4th condition and writes "undefined" in the browser.I also added an empty string to validation - no change.Thanks, Gal Link to comment Share on other sites More sharing options...
Jesdisciple Posted May 27, 2008 Share Posted May 27, 2008 I think we need the definition of ajaxCall... Link to comment Share on other sites More sharing options...
watagal Posted May 27, 2008 Author Share Posted May 27, 2008 Yes, you're right. I think I found the strange problem/solution: I was returning the "xmlHttp.responseText" directly (commented out now), now I assign it to a variable first and return the variable -- IT WORKS! // AJAX callfunction ajaxCall(dataSource, method, encode, async) { xmlHttp = createAjaxObject(); if (xmlHttp != null) { xmlHttp.onreadystatechange = function() { if ((xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) && xmlHttp.status == 200) { // return xmlHttp.responseText; var response = xmlHttp.responseText; return response; } } if (method == "POST") { xmlHttp.open("POST", dataSource, async); xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); xmlHttp.send(encode+"&sid="+Math.random()); } else { xmlHttp.open("GET", dataSource+"&sid="+Math.random(), async); xmlHttp.send(null); } } else alert ("Browser does not support HTTP Request");} Thanks, if you or anybody have any insight on this, please don't hesitate to shed some light. I feel like i'm in a dark room, feeling around for the instruction manual.--Gal Link to comment Share on other sites More sharing options...
watagal Posted May 27, 2008 Author Share Posted May 27, 2008 Sorry, I was wrong - it still does not work.help? still in the dark Link to comment Share on other sites More sharing options...
justsomeguy Posted May 28, 2008 Share Posted May 28, 2008 Use console.log like he mentioned, it will write something to the console in Firebug that you can inspect. e.g.var validation = ajaxCall('Html_php_code/validateSignIn2.php',"POST",'u='+userName+'&p='+password, false);console.log(validation);It will write the object where you can click on it and look through it to see all of the properties and methods. If you want to convert something to a string you can do this:var validation = new String(ajaxCall('Html_php_code/validateSignIn2.php',"POST",'u='+userName+'&p='+password, false)); Link to comment Share on other sites More sharing options...
watagal Posted May 28, 2008 Author Share Posted May 28, 2008 I went back to a post by Aspnetguy in another thread and implemented a callback function (validateSignIn2) and I think it works now. I'm still in the dark reading the manual!Thanks all, // AJAX callfunction ajaxCall(dataSource, method, encode, async, callback) { xmlHttp = createAjaxObject(); if (xmlHttp != null) { xmlHttp.onreadystatechange = function() { if ((xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) && xmlHttp.status == 200) { if (callback) { callback(xmlHttp.responseText); } } } if (method == "POST") { xmlHttp.open("POST", dataSource, async); xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); xmlHttp.send(encode+"&sid="+Math.random()); } else { xmlHttp.open("GET", dataSource+"&sid="+Math.random(), async); xmlHttp.send(null); } } else alert ("Browser does not support HTTP Request");}//////////////////function validateSignIn() { var userName = document.frmSignIn.txtUserName.value.toLowerCase(); var password = document.frmSignIn.txtPassword.value; if (userName == '') {document.getElementById('divInvalidUserName').style.display = "block"; return;} if (password == '') {document.getElementById('divInvalidPassword').style.display = "block"; return;} ajaxCall('Html_php_code/validateSignIn.php',"POST",'u='+userName+'&p='+password, false, validateSignIn2);}function validateSignIn2(responseText) { // callback function document.getElementById('divInvalidUserName').style.display = "none"; document.getElementById('divInvalidPassword').style.display = "none"; var validation = responseText; if (validation.substr(0,18) == "Invalid User Name:") { document.getElementById('divInvalidUserName').style.display = "block"; document.getElementById('txtUserName').value = ""; } else if (validation.substr(0,17) == "Invalid Password:") { document.getElementById('divInvalidPassword').style.display = "block"; document.getElementById('txtPassword').value = ""; } else if (validation == "Successful") { window.location="index.php?p="+FRM_SIGNIN; } else { document.write(validation); }} Link to comment Share on other sites More sharing options...
watagal Posted May 28, 2008 Author Share Posted May 28, 2008 Use console.log like he mentioned, it will write something to the console in Firebug that you can inspect. e.g.var validation = ajaxCall('Html_php_code/validateSignIn2.php',"POST",'u='+userName+'&p='+password, false);console.log(validation);It will write the object where you can click on it and look through it to see all of the properties and methods. If you want to convert something to a string you can do this:var validation = new String(ajaxCall('Html_php_code/validateSignIn2.php',"POST",'u='+userName+'&p='+password, false));Good to know, I'll use this alot! Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.