I have a simple php file:
<?php
/**
* email_validation_query: evq.php
* This file is placed in the root directory and returns the owner's email address
* in the jos_owners table along with the animal's name and the breeder who registers them.
**/
$user_name = "user_name";
$password = "password";
$server = "localhost";
$email=$_GET['email'];
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
$SQL="SELECT cf_user_id,petname FROM jos_owners WHERE email='".$email."'";
$result = mysql_query($SQL);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$users='';
$petnames='';
while($data = mysql_fetch_array($result)){
if(!empty($users)){
$users.=',';
$petnames.=',';
}
$users.=$data['cf_user_id'];
$petnames.=$data['petname'];
}
echo $users.'|'.$petnames;
mysql_close($db_handle);
?>
My form is part of a chronoforms joomla addon with custom code:
<!--
'owners.php'
26th August 2014
-->
<?php
// Get user-information from Joomla
$user = &JFactory::getUser();
$form->data['breederemail'] = $user->email;
?>
<img id="processing_image"
style="visibility: hidden; position: absolute; float: left; left: 560px; top: 500px;"
src="images/wait.gif" />
<script type="text/javascript">
var userid=<?php echo $user->id;?>;
window.onload = function(){
initform();
}
function initform() {
document.owners.onsubmit=breeders_form_validation;
}
function breeders_form_validation(){
var response=validate_email();// always false, why?
if(response){
var wait=document.getElementById('processing_image');
wait.style.top=findTop(document.getElementById('submit'))-100+'px';
wait.style.visibility="visible";
}
return response;
}
function validate_email() {
var owners_email=document.getElementById('email').value;
if(ajaxCheck(owners_email)){// always false, why?
// never gets here
return true;
}
return false;
}
function ajaxCheck(owners_email){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
/* Something went wrong, we can't interrogate the database because the
browser doesn't support AJAX. So we have to allow the user to submit possible
duplicates*/
return true;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var responseText=ajaxRequest.responseText;
var petsarray=responseText.split('|');
var userarray=petsarray[0].split(',');
var petnames=petsarray[1].split(',');
for(var i=0;i<userarray.length;i++){
if(userarray[i]==userid){
if(document.getElementById('petname').value==petnames[i]){
alert('You cannot submit the same owner and pet twice!');
return false;
}
}
}
return true;// this should be true, but it isn't. why?
}
}
owners_email=encodeURI(owners_email);
ajaxRequest.open("GET", "evq.php?email="+owners_email, true);
ajaxRequest.send(null);
return false;
}
function findTop(element) {
if (element) {
var parentPos = findTop(element.offsetParent);
return parentPos + element.offsetTop;
} else {return 0;}
}
</script>
My form never submits; it always evaluates to false.
The php script returns the user id and petname as a string containing the comma deliminated strings of the ids and petnames separated by a pipe. I split both of these into an ID array and a Petnames array, which I search to see that the ID of the user has not submitted the same pet already. If he has, the function returns false with an error message. Otherwise it should return 'true'.
But it always returns 'false'.