Form Validation With xajax

Listing 2. The input.php script uses xajax to validates a catalog entry form.

<?php require('./xajax/xajax.inc.php');
$xajax = new xajax();
$xajax->cleanBufferOn();  

$xajax->registerFunction("validateCatalogId");
$xajax->registerFunction("updateCatalog");


function validateCatalogId($formValues){
$objResponse = new xajaxResponse();


$catalogId=trim($formValues['catalogId']);

$username='OE';
$password='password';

$db='(DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCL)
    )
  )';

$connection = oci_connect($username, $password, $db);
$stmt = oci_parse($connection, "SELECT * from OE.CATALOG WHERE catalogId='".$catalogId."'");

$r = oci_execute($stmt);

$nrows = oci_fetch_all($stmt, $result);

if($nrows==0){
$objResponse->addAssign("validationMessage","innerHTML","Catalog Id is Valid");
$objResponse->addAssign("submitForm","disabled",false);

$objResponse->addAssign("journal","value","");
$objResponse->addAssign("publisher","value","");

$objResponse->addAssign("edition","value","");
$objResponse->addAssign("title","value","");
$objResponse->addAssign("author","value","");

}

if($nrows>0){
$objResponse->addAssign("validationMessage","innerHTML","Catalog Id is not Valid");

oci_fetch($stmt);

$catalogId=oci_result($stmt,'CATALOGID');
$journal=oci_result($stmt,'JOURNAL');
$publisher=oci_result($stmt,'PUBLISHER');
$edition=oci_result($stmt,'EDITION');
$title=oci_result($stmt,'TITLE');
$author=oci_result($stmt,'AUTHOR');

$objResponse->addAssign("journal","value",$journal);
$objResponse->addAssign("publisher","value",$publisher);

$objResponse->addAssign("edition","value",$edition);
$objResponse->addAssign("title","value",$title);
$objResponse->addAssign("author","value",$author);
$objResponse->addAssign("submitForm","disabled",true);

}

return $objResponse->getXML(); 

}

function updateCatalog($formValues){

$catalogId=trim($formValues['catalogId']);
$journal=trim($formValues['journal']);
$publisher=trim($formValues['publisher']);
$edition=trim($formValues['edition']);
$title=trim($formValues['title']);
$author=trim($formValues['author']);

$username='OE';
$password='password';

$db='(DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCL)
    )
  )';

$connection = oci_connect($username, $password, $db);

$sql = "INSERT INTO OE.Catalog 

VALUES('".$catalogId."','".$journal."','".$publisher."','".$edition."','".$title."','".$author."')";

$stmt = oci_parse($connection, $sql);
$r=oci_execute($stmt);

if (!$r) {
  $e = oci_error($stmt); 
  echo htmlentities($e['message']);
}
}
$xajax->processRequests();

?>
<head>
<?php $xajax->printJavascript('./xajax');
?>
</head>
<body>
<h1>Form to Create a Catalog Entry</h1>
<form id="validationForm"  name="validationForm" onsubmit="xajax_updateCatalog(xajax.getFormValues('validationForm'));">
<table>
<tr><td>Catalog Id:</td><td><input    type="text"
            size="20"  
              id="catalogId"
            name="catalogId"
    autocomplete="off"
         onkeyup="xajax_validateCatalogId(xajax.getFormValues('validationForm'));"></td>
         <td><div id="validationMessage"></div></td>
</tr>


<tr><td>Journal:</td><td><input    type="text"
            size="20"  
              id="journal"
            name="journal"></td>
</tr>

<tr><td>Publisher:</td><td><input    type="text"
            size="20"  
              id="publisher"
            name="publisher"></td>
</tr>

<tr><td>Edition:</td><td><input    type="text"
            size="20"  
              id="edition"
            name="edition"></td>
</tr>
<tr><td>Title:</td><td><input    type="text"
            size="20"  
              id="title"
            name="title"></td>
</tr>

<tr><td>Author:</td><td><input    type="text"
            size="20"  
              id="author"
            name="author"></td>
</tr>

<tr><td><input    type="submit"
            value="Create Catalog"  
              id="submitForm"
            name="submitForm"></td>
</tr>
</table>

</form>
</body>
</html>