CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   Forum Validation Help Please (http://www.codingforums.com/showthread.php?t=249471)

UrbanCowboy 01-22-2012 12:41 AM

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>


sunfighter 01-22-2012 02:32 PM

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.'"?

UrbanCowboy 01-22-2012 10:01 PM

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.


All times are GMT +1. The time now is 09:58 PM.

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