Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2012, 12:41 AM   PM User | #1
UrbanCowboy
New to the CF scene

 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
UrbanCowboy is an unknown quantity at this point
Forum Validation Help Please

I'm having some struggles with form validation. I dont have a problem sending the request and receiving the response, but I am having a problem making it either submit or dont submit based on the outcome.

What I am trying to do, is for the user to clicks submit, which fires off the javascript request to ajax_info.php. If that file decides everything is AOK then there will be an empty response, in which case the function should return true and the submit should continue. If there is a response string, then notify the user via alert, and return false so the submit doesnt process.

The only way the alert is happening is if I return false at the end of the javascript function. But if I do that, then the submit never happens. If I put it anywhere else, I dont get the alert (as though the response string is empty, even if it is not), and the submit proceeds regardless of the response.

I hope I am making sense. Thanks.

Code:
<html>
<head>
<script type="text/javascript">

function loadXMLDoc()
{

var user = document.getElementById('username2').value;

var xmlhttp;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   	alert(xmlhttp.responseText);
	return false;
    }
  else
    {
     return true;
    }
  }
xmlhttp.open("GET","ajax_info.php?name=" + user + "&eml=fakemail@gmail.com&ref=filler",true);
xmlhttp.send();

}

</script>
</head>
<body>


<form action='fbauthorize.php' method='post' name='fbform' onsubmit="return loadXMLDoc();">
				<center><big><big><b>Please Enter An Account Name:</big><br>
				<input type='text' name='username' id='username'><br><br>
				<input type='submit' value='Create Account'></form>

</body>
</html>
UrbanCowboy is offline   Reply With Quote
Old 01-22-2012, 02:32 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
The very first line in your js kills the function
var user = document.getElementById('username2').value;
You have no username2 ID. But you have a username ID. Maybe a misprint?

BUT Biggest question is "Why come back from ajax_info.php to submit again to the server? Why not have ajax_info.php call fbauthorize.php? Or just include the code of fbauthorize.php into ajax_info.php and make the alert(xmlhttp.responseText) say Mission accomplished data stored.'"?
sunfighter is offline   Reply With Quote
Old 01-22-2012, 10:01 PM   PM User | #3
UrbanCowboy
New to the CF scene

 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
UrbanCowboy is an unknown quantity at this point
You're right about the username2. I admit I threw together the example based on everything I'd been doing and ultimately the username2 isnt the root of the problem I'm asking for help on. So yea, thanks for pointing that out, but we can ignore it for the moment.

The reason I cannot create the account on ajax_info.php is because that page isnt being directly access by the user, so the Adwords conversion tracking code that needs to go at time of creation would not register the conversion properly.
UrbanCowboy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:46 AM.


Advertisement
Log in to turn off these ads.