PDA

View Full Version : i'm little confusing to execute "responseText" from ajax.



smansakra
12-01-2008, 10:36 AM
hello...
i'm little confusing to execute "responseText" from ajax.

please read my problem below.

my responseText from ajax return this value " 0 " and this value "1"

then i make alert like this.

///-- part of code is below --//

var myvalue = responseText;
alert (myvalue);
if ( myvalue == '1') alert('wellcome');
else alert('try again');

//--end of part of code--//

then i execute that code,
the myvalue return "1" , but my alert return "try again", it should return "wellcome",
i have tried many times...
please thanks.

codeexploiter
12-01-2008, 10:43 AM
resoponseText is a property that is accessible only through your XHR object. The way you
've accessed it in your code seems to be confusing. Here is a doc (https://developer.mozilla.org/En/AJAX/Getting_Started) that explains the ajax in simple terms

smansakra
12-01-2008, 10:56 AM
what i mean here is

i set my php_target_file.php to return value 1 if the input is correct, and 0 if the input it incorrect.

becouse the input is correct so my responseTEXT return = '1' ,
then i'm confusing to manage that '1' value. :confused:

i try this code to manage that '1' value.:mad:
if ( myvalue == '1') alert('wellcome');
else alert('try again');


but it always alert "try again" :eek::eek:

smansakra
12-01-2008, 11:05 AM
it is strange

codeexploiter
12-01-2008, 11:09 AM
1. You need to check what is the correct value from the server-side.

2. Check the value comes in responseText.

3. There may be some blank spaces along with the response from the server and those blank spaces makes the condition checking to go to the else part of it.

4. It would be better if you use any trim function to remove any leading and trailing empty spaces from the value from the server-side. JavaScript doesn't support a trim function in it. You can use the following one:


function trim(str) {
if (!str || typeof str != 'string') return null;
return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
}

5. If the responseText is null then also it'll go to the else part or if it contains 0 too it will go to the else part.

It would be better if you can post your link so that we can have a look at the issue directly.

smansakra
12-01-2008, 11:12 AM
wait in minute

smansakra
12-01-2008, 11:17 AM
huh, i hate this,
so i change my code like this, and it work

if ( myvalue == '1') alert('wellcome');
else alert('try again');

// change to this one

if ( myvalue.indexOf('1') != -1) alert('wellcome');
else alert('try again');


may be you're right, may be there is a blank space there, or may be another character from database that invisible on browser.

your trim code, i like that.

codeexploiter
12-01-2008, 11:26 AM
The issue with the new way you've used is if myvalue contains anything that has 1 in it will be consider that as a true case I mean '1', '91', '19', '9991', '01', etc are the examples.

Though the code works now but it would be wise to check the condition for the correct value rather than using indexOf method.

If you can post your JS code then we can help you.

smansakra
12-01-2008, 11:44 AM
the file contains
1. login.html
2. processing/login.php

details
login.html
<html>
<head>
<title>
login here
</title>
</head>
<body>
<style>
.loginku
{
width:300px;
}
.login_form_input{
padding-left:3px;
}
.disignBoxFirst_login
{
position:relative;
border:1px solid #CCC;
margin-bottom:10px;
background-image:url('http://smansakra.jwnmedia.com/smansakra.net.tc/templates/imacons/sss10.jpg');
background-repeat:repeat-x;
background-color:#fff;
}
.boxFirstHeader_login
{
position:relative;

height:17px;
padding:3px 0px 0px 8px;
font-size:12px;
font-weight:bold;
color:#44952E;
text-align:left;
text-transform:uppercase;
border-bottom: 1px solid #CCC;
}

.boxContent_login
{
position:relative;
overflow:hidden;
font-size:12px;
color:#183880;
/*
padding:10px;
background-image:url("http://smansakra.jwnmedia.com/smansakra.net.tc/templates/imacons/ss2man10.png");
*/
background-image:url("http://smansakra.jwnmedia.com/smansakra.net.tc/images/ss2man10.png");
background-repeat:repeat;
}
.login_form_cont_login{
}
</style>


<div class="loginku">
<script type="text/javascript" src="http://smansakra.jwnmedia.com/smansakra.net.tc/inc/js/functions.js"></script>
<script type="text/javascript">
function checking_login(){
var login_form = document.login_form;
var username = login_form.ID.value;
var password = login_form.Password.value;
//alert
if(username == "" || password == ""){
alert('please fill in all input');
return false;
}
login_form.login_button.disabled="disabled";
login_form.login_button.value = "checking...";
var url_login = "processing/login.php";
// var url_login = "a.php";
var url_login_extra = "u="+username+"&p="+password;
var login_exe = ajaxpack.getAjaxRequest(url_login,url_login_extra,process_login,"text");
}


function process_login(){
var login_ajax = ajaxpack.ajaxobj
var login_filetype = ajaxpack.filetype
if(login_ajax.readyState == 4){
if(login_ajax.status==200 || window.location.href.indexOf("http")==-1){
if(login_ajax.responseText.indexOf('1')!=-1){
//alert(myrespon_value);
alert('Checking success, please click "Login Now"');
document.login_form.login_button.style.display="none";
document.login_form.login_submit.style.display="block";

}
else{
alert(login_ajax.responseText);
document.login_form.login_button.disabled="";
document.login_form.login_button.value = "try again";
}
}
}
}


</script>
<div class="disignBoxFirst_login">
<div class="boxFirstHeader_login">Member Login</div>
<div class="boxContent_login">
<div class="clear_both"></div>

<form name="login_form" action="member.php" method="post">
<input name="relocate" value="/smansakra.net.tc/member.php" type="hidden">
<div class="login_form_cont_login">
<div class="login_form_warning"></div>
<div class="login_form_row">

<div class="clear_both"></div>
<div class="login_form_label">Nickname:</div>
<div class="login_form_val">

<input name="ID" class="login_form_input" type="text" value="username or email" onfocus="select()">
</div>
<div class="clear_both"></div>
</div>
<div class="login_form_row">

<div class="clear_both"></div>
<div class="login_form_label">Password:</div>
<div class="login_form_val">

<input name="Password" class="login_form_input" type="password">
</div>
<div class="clear_both"></div>
</div>
<div class="login_form_row_forgot">

<a href="http://smansakra.jwnmedia.com/smansakra.net.tc/forgot.php">Forgot Password?</a>
</div>
<div class="login_form_row">

<div class="clear_both"></div>
<div class="login_form_label"></div>
<div class="login_form_val">
<input name="login_submit" class="login_form_submit" value="Log in Now" type="submit" style="display:none;">
</div>
<div>
<input name="login_button" class="login_form_submit" value="Log in" type="button" onclick="checking_login();">
</div>


<div class="clear_both"></div>
</div>
<br>
<div class="login_form_join">

<a href="http://smansakra.jwnmedia.com/smansakra.net.tc/join_form.php">Join Now</a>
</div>
</div>

</form>
<div class="clear_both"></div>

</div>
</div>
</div>

</body>
</html>


login.php
<?
//created by okie wardoyo
if(!isset($_REQUEST['u']) || !isset($_REQUEST['p'])){
echo "INVALID URL";
exit();
}
/*
if(!strstr($_SERVER['HTTP_REFERER'],"http://smansakra.jwnmdia.com/")){
echo "NOT ALLOWED";
exit();
}
*/
//require files
require_once('../inc/header.inc.php');
require_once(BX_DIRECTORY_PATH_INC.'admin.inc.php');
require_once(BX_DIRECTORY_PATH_INC.'design.inc.php');
require_once(BX_DIRECTORY_PATH_INC.'utils.inc.php');

//variable
$u = process_db_input($_REQUEST['u']);
$p = process_db_input($_REQUEST['p']);

$get_nickname_byEmail = "SELECT * FROM `Profiles` WHERE `Email` ='".$u."'";
$get_nickname_byNick = "SELECT * FROM `Profiles` WHERE `NickName`='".$u."'";

//processing..
$query=(check_email_address($u)==true)? $get_nickname_byEmail:$get_nickname_byNick;
$result=db_arr($query);
$success ="1";
if ($result == ""){
echo "nickname / email not found, try again";
exit();
}
else{
if(md5($p) != $result['Password']){
echo "wrong password, try again";
exit();
}
else echo $success;
}

?>

codeexploiter
12-01-2008, 12:45 PM
function process_login(){
var login_ajax = ajaxpack.ajaxobj;

if(login_ajax.readyState === 4 && login_ajax.status === 200 ){
if(login_ajax.responseText && trim(login_ajax.responseText) === "1"){
alert('Checking berhasil, silahkan click "Login Now');
document.forms['login_form'].elements['login_button'].style.display="none";
document.forms['login_form'].elements['login_submit'].style.display="block";
}else{
alert(login_ajax.responseText);
document.forms['login_form'].elements['login_button'].disabled="";
document.forms['login_form'].elements['login_button'].value = "try again";
}
}
}

function trim(str) {
if (!str || typeof str != 'string') return null;
return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
}

Comment your version of process_login method and try mine and put the trim function also inside script element. Actually as a false server response it is not '0' coming from the server but a message.

Make sure that you are sending the text '1' in case of success in case of failure anything is ok.

smansakra
12-01-2008, 01:22 PM
ok, i will try...
thanks:)

smansakra
12-01-2008, 01:23 PM
OK, i will try, thanks

smansakra
12-03-2008, 05:51 AM
hi, i think there is a problem or error on your function trim(str)

try this

<script type="text/javascript">
var text="hello....";
alert(trim(text));
</script>

no alert happend...!:(

jscheuer1
12-03-2008, 06:56 AM
If you just want to get rid of white space of any kind (accepts a number or a string, otherwise returns null):


function trim(str){
if(!/^(string)|(number)$/.test(typeof str))
return null;
return str.toString(10).replace(/\s/g, '');
};

Also remember that 0 == '' and ' ', but that '0' doesn't.

smansakra
12-03-2008, 07:20 AM
hum, :confused:
O K