Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    SMC
    SMC is offline
    New Coder
    Join Date
    Jun 2007
    Location
    Orlando, FL
    Posts
    60
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Javascript won't post (return true) [SOLVED]

    Hello everyone I'm having a problem with my Javascript. If something is wrong it reports all the errors just as it should be and returns false. However if everything is correct it does nothing, just refreshes the page. I added an alert towards the bottom and confirmed the function returns true. Is there something in here that would stop it from posting?

    Here is the form tag:

    Code:
    <form id="register_form" action = "<?php echo $_SERVER['PHP_SELF'] ?>" method = "post" enctype="multipart/form-data" onsubmit="return validateForm( this );">
    And the rather long function, I apologize but I cannot find an error to condense it:

    Code:
    function validateForm( form ){
    		
    	form.register.disabled = true;
    	
    	document.getElementById( 'loading' ).style.display = "block";
    	document.getElementById( 'register_button' ).style.display = "none";
    	
    	document.getElementById( 'error_display' ).style.display = "none";
    	document.getElementById("errorText").innerHTML = "";
    	document.getElementById("alertIcon").innerHTML = "<img src=\"/images/home/alert_icon.png\" alt=\"Error!\" />";
    	
    	var clearSpanArray = new Array( "usernameSpan", "passwordSpan", "emailSpan", "verify_passwordSpan", "dateofbirthSpan" );
    	for ( x in clearSpanArray ){
    		document.getElementById( clearSpanArray[x] ).style.color = "#000000";
    		document.getElementById( clearSpanArray[x] ).style.fontWeight = "normal";
    	}
    	
    	var FormValid = true;
    	var spanArray = new Array();
    		
    	var field = form.username.value;
    	
    	if( field.length > 14 || field.length < 3 ){
    		
    		document.getElementById("errorText").innerHTML += "<br />&bull;&nbsp;Your username must be between 3 and 14 characters";
    		document.getElementById("alertIcon").innerHTML += "<br />";
    		
    		spanArray.push( "usernameSpan" );
    		FormValid = false;
    
    	}
    	
    	var field = form.email.value;
    	
    	var AtPos = field.indexOf("@");	
    	var StopPos = field.lastIndexOf(".");
    	
    	if( ( field == "" ) || ( AtPos == -1 ) || ( StopPos == -1 ) || ( StopPos < AtPos ) || ( (StopPos - AtPos) == 1 ) ){
    		
    		document.getElementById("errorText").innerHTML += "<br />&bull;&nbsp;Your e-mail is not valid";
    		document.getElementById("alertIcon").innerHTML += "<br />";
    		
    		spanArray.push( "emailSpan" );
    		FormValid = false;
    	
    	}
    	
    	var illegalChars = new Array( "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "\"", "\'", "\\", ">", ",", ".", "/", "-", "=", "?", ";" );
    	
    	var field = form.password.value;
    	var password = form.password.value;
    	var verify_password = form.verify_password.value;
    	
    	var passwordError = false;
    	
    	if( field.length > 16 || field.length < 5 ){
    		
    		document.getElementById("errorText").innerHTML += "<br />&bull;&nbsp;Your password must be between 5 and 16 characters";
    		document.getElementById("alertIcon").innerHTML += "<br />";
    		spanArray.push( "passwordSpan" );
    				
    		FormValid = false;
    		passwordError = true;
    	
    	}
    	
    	for ( x in illegalChars ){
    			
    			if ( field.indexOf( illegalChars[x] ) != -1 ){
    				
    					document.getElementById("errorText").innerHTML += "<br />&bull;&nbsp;Your password cannot contain symbols";
    					document.getElementById("alertIcon").innerHTML += "<br />";
    					spanArray.push( "passwordSpan" );
    					
    					FormValid = false;
    					passwordError = true;
    					break;
    					
    			}
    			
    	}
    		
    	if( password != verify_password ){
    		
    			document.getElementById("errorText").innerHTML += "<br />&bull;&nbsp;Your passwords do not match";
    			document.getElementById("alertIcon").innerHTML += "<br />";
    			spanArray.push( "passwordSpan" );
    			spanArray.push( "verify_passwordSpan" );
    			
    			FormValid = false;
    			passwordError = true;
    			
    	}
    	
    	var field = form.year.value;
    	
    	if( field.length != 4 || !isNumeric( field ) ){
    		
    		document.getElementById("errorText").innerHTML += "<br />&bull;&nbsp;Please enter your birthday";
    		document.getElementById("alertIcon").innerHTML += "<br />";
    		spanArray.push( "dateofbirthSpan" );
    		
    		FormValid = false;
    	
    	}
    	
    	if( passwordError = true ){
    	
    		form.password.value = "";
    		form.verify_password.value = "";
    	
    	}
    	
    	if( FormValid == false ){
    		
    		form.register.disabled = false;
    		document.getElementById( 'error_display' ).style.display = "block";
    		document.getElementById( 'loading' ).style.display = "none";
    		document.getElementById( 'register_button' ).style.display = "block";
    		scroll(0,0);
    		
    		for ( x in spanArray ){
    			document.getElementById( spanArray[x] ).style.color = "#ff0000";
    			document.getElementById( spanArray[x] ).style.fontWeight = "bold";
    		}
    	
    	
    	}
    	
    	alert( FormValid );
    	return FormValid;
    	
    }

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    If it "refreshes" the page, that means javascript is OK. Check the action of the FORM and see if the address of the server-side application is correct, if that application exists, and if so, if it works...

    In a word... check what is that php variable <?php echo $_SERVER['PHP_SELF'] ?>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    SMC
    SMC is offline
    New Coder
    Join Date
    Jun 2007
    Location
    Orlando, FL
    Posts
    60
    Thanks
    5
    Thanked 2 Times in 2 Posts
    See heres the weird thing. If I take out the Javascript function, it posts fine (I get a PHP error since the receiving doc isn't up yet). But when the Javascript is there, it 'refreshes' the page. So that leads me to believe the Javascript is the problem.

    I tried swapping the validateForm(); function with one that just returns true, and that worked. So it must be with that validateForm(); function.

    I apologize for posting all this code, I just have no idea what could be wrong with it!

    Thanks

  • #4
    SMC
    SMC is offline
    New Coder
    Join Date
    Jun 2007
    Location
    Orlando, FL
    Posts
    60
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Any ideas? I've totally run out

  • #5
    SMC
    SMC is offline
    New Coder
    Join Date
    Jun 2007
    Location
    Orlando, FL
    Posts
    60
    Thanks
    5
    Thanked 2 Times in 2 Posts
    AHA! Okay I was forced to go line by line. My PHP script was authenticating the script submission based on the variable sent by the submit button, which I was disabling at the begging of the script.

    When I included a hidden input and made the PHP script check that it worked perfectly!

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    I do not see the function 'isNumeric' in the posted script that is called here:
    Code:
    if( field.length != 4 || !isNumeric( field ) ){

  • #7
    SMC
    SMC is offline
    New Coder
    Join Date
    Jun 2007
    Location
    Orlando, FL
    Posts
    60
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by rwedge View Post
    I do not see the function 'isNumeric' in the posted script that is called here:
    Code:
    if( field.length != 4 || !isNumeric( field ) ){
    That one is in my global.js file, like I had said there are no javascript errors and I'm pretty sure that would return one.

    Eitheir way, like the topic says, problem solved.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •