Problem with Ajax the second time around during validation

Using jQuery I have a validation routine for a login page where I want to check the email address for 3 things, is it blank; is it a valid email address; is it already in the database. The first one obciously does not require any database interaction and the other two I do go over to the php on my server for results.

The problem is all three work fine but if I test it with a blank result, it works; and then test it with an invalid email, it works; and then test it with a duplicate email it does nothing. Similarly, if I test it blank and test it with a duplicate email, that works but then I test it with an invalid email and no response.

So it appears to be a problem on the second trip to the php code on the server but I can’t find any reason for that to happen. I know the validation process is good because it works fine if it is the first trip but always fails on the second trip regardless of which of the two validations it is that gets sent second. The code is below

jQuery code:

	$('form#useraddform input[name="email"]').focus();

	$('form#useraddform input[name="email"]').focusout(function() {
		valEmail();
	});
	

	function valEmail() {
		var emailval;
		emailval = $('form#useraddform input[name="email"]').val();
//alert(emailval);
		if ($('form#useraddform input[name="email"]').val() === "") {
			$('form#useraddform #valemail').replaceWith('<div id=\"valemail\" class=\"row b\"><div class=\"col-xs-22 offset-xs-1\"><div class=\"valmsg\">Email Address is a required field and cannot be left blank</div></div></div>');
			$('form#useraddform #valemail').show();
		} else {
			$.post("/js/UsersNew_sub.php", 
			{
				email: emailval
			},
			function(data) {
				var jsonobj = jQuery.parseJSON(data);
//alert(jsonobj[0]);
				if (jsonobj[0] == false) {
					$('form#useraddform #valemail').replaceWith('<div id=\"valemail\" class=\"row b\"><div class=\"col-xs-22 offset-xs-1\"><div class=\"valmsg\">' + jsonobj[1] + '</div></div></div>');
					$('form#useraddform #valemail').show();
				} else {
					$('form#useraddform #valemail').replaceWith('<div id=\"valemail\" class=\"row b\"><div class=\"col-xs-22 offset-xs-1\"><div class=\"valmsg\"></div></div></div>');
					$('form#useraddform #valemail').hide();
				}
			});
		}
	}

And the php code in /js/UsersNew_sub.php:

<?php
	$from = "phpsub_main";
	require $_SERVER['DOCUMENT_ROOT'] . '/includes02127/sysinit.inc.php';

	$valall = new Validate_LogicValidate($dbh, $content="");
	
	//execute the validation process for email
	$valall->content = array('users-email',$_POST['email'],'Y');
	$inp['email']=array();
	$inp['email'] = $valall->valMain();
	unset($valall);

	//to reset for error messages generated by the validation process
	$_SESSION['message'] = array();
	$_SESSION['message'][0] = 0;
	$_SESSION['message'][1] = '';
	
	$retval = json_encode($inp['email']);
	
	echo $retval;

Just a few comments, I have run this using .ajax as well and put alerts in both success and error and neither were triggered on the second validation sent to the server. However, with an alert inside the valEmail() function right after passdata I do get the proper value of passdata[‘email’], so it gets that far but appears not to execute the .ajax or .post methods because I get no indication in the success or error responses.

Never mind, it is something going on in the sysinit file. I pulled out what I needed from that and ran it locally and it now works fine.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.