Go Back   Armenian Knowledge Base > Technical sections > Webmaster Zone > Web Development

Reply
 
Thread Tools

JavaScript problem with variables-functions in AJAX ( jQuery )
Old 24.03.2008, 14:34   #1
Web developer
 
Aram Ghazanchyan's Avatar
 
Join Date: 09 2002
Location: Yerevan
Age: 44
Posts: 896
Rep Power: 0
Default JavaScript problem with variables-functions in AJAX ( jQuery )

I need to check same login verification submitted from form with help of AJAX/jQuery
Here is the sources ( jQuery file is not included )
main.html
Code:
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script language="JavaScript" type="text/javascript">
function verif() 
{
	var myErr = '';	
	var f = document.f1reg;
	
	myErr+=" * Fill in all required fields.\n";
	
	$.ajax({
	   type: "POST",
	   url: "checkusername.php",
	   data: "username="+ f.username.value,
	   success: function(msg){
			if (msg == "1"){
				myErr+=' * User with such login already exists.\n';
				//alert(myErr); Label 1
			}
	   }
	 });
	//alert(myErr); Label 2

	if (myErr!='') {
		alert('The following information is incorrect:\t\t\t\t\t\n\n'+myErr); // Label 3
	} 
	return (myErr=='');
}
</script>


<form action="action.php" method="post" name="f1reg" id="f1reg" onsubmit="return verif();">
  
  Login: <input type="text" id="username" name="username" value="" tabindex="1" />
    <input type="submit" name="submit" value="Submit" />
  </div>
</form>
checkusername.php just prints "1"

The problem is the following:
On Label 1, myErr adds an error message to the list
On Label 2, myErr looses the latest message and just shows " * Fill in all required fields.\n";
On Label 3, the same situation as in Label 2
But if I remove Label 2 comment, and alert any message in that part of code, the script starts to working well.
What is the problem?

Old 24.03.2008, 15:37   #2
полюбому йес эм...
 
Mesrop's Avatar
 
Join Date: 02 2005
Location: near the computer
Age: 34
Posts: 3,243
Rep Power: 5
Default

inchqan haskaca, problem@ sranic e.
browser@ ajax zapros e uxarkum, bayc chi spasum vor checkusername.php -ic return ani, and minchev ed request@ sksum e gnal, inqe arag ancnum e hajord gorcoxutyane... sax JSn arag ashxatum e, u PHPic chi hascnum stanal variable ev kpcnel myErr -in.
erp label2 -um alert es anum, script@ cuyc e talis et alerti window-@ and kangnum e. ed kangnac vaxt karox a ga PHP-ic qo variablen and kpni myErr -in, u label 3 um arden normal php'ic return exac string unenas.
script@ arden petq e sharunakel function(msg) -i mej, vor return exac lini php'n...
husov em chisht and haskanali er...

Last edited by Mesrop; 24.03.2008 at 15:47. Reason: avelacum

Old 25.03.2008, 05:36   #3
Web developer
 
Aram Ghazanchyan's Avatar
 
Join Date: 09 2002
Location: Yerevan
Age: 44
Posts: 896
Rep Power: 0
Default

Haskanalin - haskanalia, bayc de es @tenc liq@ ktorner unem, voric petq e sborka arvi u resultiruyushiy error ta. Aysinqn es chem karoghana menak 1 tvyal-i stugman jamanak verjnakan result@ cuic tam.
Erevi xndir@ aveli global senc tam, vonc karelia Ajax-ov stugel inch vor DB-ic tvyal u dra result@ poxancel inch vor mi JS-i popoxakanin?

Old 25.03.2008, 10:04   #4
полюбому йес эм...
 
Mesrop's Avatar
 
Join Date: 02 2005
Location: near the computer
Age: 34
Posts: 3,243
Rep Power: 5
Default

de script@ parberabar petq e pause lini, and server request ani...
uxaki amen requestic heto coden piti sharunakes success: function(msg){} -i mejic...orinak
success: function(msg){check2();} ...
isk vapshe ed depqum imho aveli chisht klini vor 1 zaprosov sax stuges. aysinqn 1 zaprosov uxarkes fieldneri valuenere PHPin, and 1 array'ov return anes from PHP bolor errornere irar het JS variablei mej.

Old 25.03.2008, 10:34   #5
Web developer
 
Aram Ghazanchyan's Avatar
 
Join Date: 09 2002
Location: Yerevan
Age: 44
Posts: 896
Rep Power: 0
Default

Mersi Mesrop.
Kporcenq takic durs ganq.

Old 25.03.2008, 10:37   #6
dardanian
 
aeneas's Avatar
 
Join Date: 11 2005
Location: new troy
Posts: 175
Rep Power: 0
Default

qo request-@ serverin XMLHttpRequest (IE -i hamar el ActiveXObject(....)) object-i mijocov petq a anes. Mesrop -@ &isht a asum, qo js-@ petq a spasi request-i patasxanin server-ic. onreadystatechange event handler-@ ogtagortsi u stugi readyState -i arjeq@ (yete &isht em hishum, request-d complete a yerb vor readyState=4).

readyState-i myus arjeqner@ (u @ndhnrapes ajax-db query -in veraberogh mi vagon urish info) internet-um karas gtnel
__________________
o gloriose stelle, o lume pregno
di gran virtù, dal quale io riconosco
tutto, qual che si sia, il mio ingegno
dante, paradiso - canto 22

Old 25.03.2008, 12:07   #7
Web developer
 
Aram Ghazanchyan's Avatar
 
Join Date: 09 2002
Location: Yerevan
Age: 44
Posts: 896
Rep Power: 0
Default

Quote:
Originally Posted by aeneas View Post
qo request-@ serverin XMLHttpRequest (IE -i hamar el ActiveXObject(....)) object-i mijocov petq a anes. Mesrop -@ &isht a asum, qo js-@ petq a spasi request-i patasxanin server-ic. onreadystatechange event handler-@ ogtagortsi u stugi readyState -i arjeq@ (yete &isht em hishum, request-d complete a yerb vor readyState=4).

readyState-i myus arjeqner@ (u @ndhnrapes ajax-db query -in veraberogh mi vagon urish info) internet-um karas gtnel
Es et tegh grel ei jQuery-em ogtagorcum. Ete 1 vagoni masin eq xosum, apa imaceq vor jQuery-n mi hat mec sostava

Old 13.06.2008, 11:04   #8
Младенец
 
Join Date: 06 2008
Location: -
Posts: 3
Rep Power: 0
Default

Error is here:
if (msg == "1"){
myErr+=' * User with such login already exists.\n';
//alert(myErr); Label 1
}

You write msg == "1", but javascript string use single quotes only: '1'
replace that to msg == '1'

Use single quotes only and be happy! )
Reply




Реклама:
реклама
Buy text link .

All times are GMT. The time now is 21:39.
Top

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.