I have a page with a FORM that contains one or more INPUT TEXT boxes, a standard SELECT box, a standard SUBMIT button, a standard BUTTON button, and some hidden TEXT boxes, that will be used to Edit or to Add a record from a MySql database as usual, in Edit mode this form will get all the information from the database and in Add mode simply will fill it with default values, this page will be opened with the window.open() javascript function when someone clicks a button (one to ADD and another to EDIT) on the main page, so, it is like a Pop up window.
Also this page -the openend one- have attached an Ajax page that is used to validate that the value of one of the TEXT boxes is unique and do not exists on the database to prevent duplicates. The SELECT box acts as a one to many relation and gets its values from another table, the SUBMIT button will save the data on the database and the BUTTON simply will cancel and send back to the previous page discarding any change.
This is my code, i don't include an included file with some php functions, my session code and my database connection settings because they are irrelevant.
form page
CODE
<?php
// session code, include file and database connection settins go here
$bo="<body class='bodyedicion' onload='valida()'>";
// data from the database for the SELECT BOX
$query_rsContinente = "SELECT idcontinente, continente FROM continente WHERE activo = 'S' ORDER BY continente.idcontinente ASC";
$rsContinente = mysql_query($query_rsContinente) or die(mysql_error());
$row_rsContinente = mysql_fetch_assoc($rsContinente);
$totalRows_rsContinente = mysql_num_rows($rsContinente);
// php variables that hold the data from the database
$mode=(!isset($_POST[mode])) ? "" : $_POST[mode];
$idpais=(!isset($_POST[idpais])) ? "" : $_POST[idpais];
$idcontinente=(!isset($_POST[idcontinente])) ? "" : $_POST[idcontinente];
$pais=(!isset($_POST[pais])) ? "" : $_POST[pais];
$pais1=(!isset($_POST[pais1])) ? "" : $_POST[pais1];
if ($modo=="grabar") { // save data to the database
$pais=(string) safeString(trim($pais));
if ($accion=="a") { // when adding a new record
$cadena="insert into pais(pais,idcontinente, fecha, hora, creador) values ('$pais',$idcontinente, curdate(), curtime()," . $_SESSION["idusuario"] . ")";
}
if ($accion=="m") { // when editing a record
$cadena="update pais set pais='$pais', idcontinente=$idcontinente where idpais=$idpais";
}
$rs = mysql_query($cadena) or die("Error... query failed.\n" . mysql_errno(). ": " . mysql_error() . "<br /><br /><a href=\"java script:history.back();\">Go back</a>");
$bo="<body class='bodyedicion' onload='window.opener.recarga();window.close()'>";
}
else {
if ($accion=="m") { // edit record
$idpais=(int) ($codigo);
$cadena="select idpais, pais, idcontinente from pais where idpais=$idpais";
$rs = mysql_query($cadena);
$row = mysql_fetch_array($rs);
$idpais=(int) $row["idpais"];
$pais=$row["pais"];
$pais1=$row["pais"];
$idcontinente=(int) $row["idcontinente"];
}
else $pais1="";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Edit/Add a Country</title>
<link rel="stylesheet" href="hojaestilo/@data001.css" type="text/css">
<style type="text/css">
#ajaxDiv { font-size:10px;font-weight:bold}
</style>
<script type="text/javascript">
function valida()
{
document.a.aceptar.style.display="inline";
if (document.a.pais.value=="") document.a.aceptar.style.display="none";
document.a.pais.focus();
}
function ajaxFunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
var response = ajaxRequest.responseText.split("|");
ajaxDisplay.innerHTML = response[0];
var TheCode = response[1];
if (TheCode == 1) {
document.a.aceptar.style.display="none";
document.a.pais.style.borderColor="red";
document.a.pais.style.borderStyle="double";
document.a.pais.focus();
document.a.esvalido.value="0";
}
else {
document.a.aceptar.style.display="inline";
document.a.pais.style.borderColor="#80a0a0";
document.a.pais.style.borderStyle="solid";
document.a.esvalido.value="1";
}
}
}
var unique = document.getElementById('pais').value;
var actual = document.getElementById('pais1').value;
var table = 'pais';
var field = 'pais';
var queryString = "?table="+table+"&unique="+unique+"&actual="+actual+"&field="+field;
ajaxRequest.open("GET", "checkunique.php" + queryString, true);
ajaxRequest.send(null);
}
function verifica(){
if(document.a.esvalido.value=="0" || document.a.pais.value=="") {
document.a.pais.focus();
return false;
}
return true;
}
</script>
</head>
<?php echo $bo;?>
<table class="tituloedicion" width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td>ADD/EDIT PAÍSES</td></tr></table>
<form name="a" method="post" onsubmit="return verifica();">
<table class="tablaedicion" width="100%" border="0" cellpadding="2" cellspacing="4">
<tr>
<td class='textoedicion' width="32%">PAÍS</td>
<td><input type="text" name="pais" id="pais" class='textos' value="<?php echo $pais;?>" size="40" maxlength="40" onkeyup="valida()" onblur="ajaxFunction()"><span id='ajaxDiv'></span></td>
</tr>
<tr>
<td class='textoedicion' width="32%">CONTINENTE</td>
<td><select name="idcontinente" id="idcontinente" class="textos" size="1" style="width:167px">
<?php
do {
?>
<option value="<?php echo $row_rsContinente['idcontinente']?>"<?php if (!(strcmp($row_rsContinente['idcontinente'],$idcontinente))) {echo "SELECTED";} ?>><?php echo $row_rsContinente['continente']?></option>
<?php
} while ($row_rsContinente = mysql_fetch_assoc($rsContinente));
$rows = mysql_num_rows($rsContinente);
if($rows > 0) {
mysql_data_seek($rsContinente, 0);
$row_rsContinente = mysql_fetch_assoc($rsContinente);
}
?></select></td>
</tr>
</table>
<input type='text' name='modo' value='grabar' style="visibility:hidden;display:none">
<input type='text' name='idpais' value="<?php echo $idpais;?>" style="visibility:hidden;display:none">
<input type='text' name='pais1' id='pais1' value="<?php echo $pais1;?>" style="visibility:hidden;display:none">
<input type='text' name='esvalido' id='esvalido' value="0" style="visibility:hidden;display:none">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" align="center" valign="middle" class='textoBotones'><input type="submit" class='botonGr' name="aceptar" value="SAVE" > <input type="button" class='botonGr' name="cancelar" value="CANCEL" onClick="window.close()"></td>
</tr>
</table>
</form>
</body>
</html>
// session code, include file and database connection settins go here
$bo="<body class='bodyedicion' onload='valida()'>";
// data from the database for the SELECT BOX
$query_rsContinente = "SELECT idcontinente, continente FROM continente WHERE activo = 'S' ORDER BY continente.idcontinente ASC";
$rsContinente = mysql_query($query_rsContinente) or die(mysql_error());
$row_rsContinente = mysql_fetch_assoc($rsContinente);
$totalRows_rsContinente = mysql_num_rows($rsContinente);
// php variables that hold the data from the database
$mode=(!isset($_POST[mode])) ? "" : $_POST[mode];
$idpais=(!isset($_POST[idpais])) ? "" : $_POST[idpais];
$idcontinente=(!isset($_POST[idcontinente])) ? "" : $_POST[idcontinente];
$pais=(!isset($_POST[pais])) ? "" : $_POST[pais];
$pais1=(!isset($_POST[pais1])) ? "" : $_POST[pais1];
if ($modo=="grabar") { // save data to the database
$pais=(string) safeString(trim($pais));
if ($accion=="a") { // when adding a new record
$cadena="insert into pais(pais,idcontinente, fecha, hora, creador) values ('$pais',$idcontinente, curdate(), curtime()," . $_SESSION["idusuario"] . ")";
}
if ($accion=="m") { // when editing a record
$cadena="update pais set pais='$pais', idcontinente=$idcontinente where idpais=$idpais";
}
$rs = mysql_query($cadena) or die("Error... query failed.\n" . mysql_errno(). ": " . mysql_error() . "<br /><br /><a href=\"java script:history.back();\">Go back</a>");
$bo="<body class='bodyedicion' onload='window.opener.recarga();window.close()'>";
}
else {
if ($accion=="m") { // edit record
$idpais=(int) ($codigo);
$cadena="select idpais, pais, idcontinente from pais where idpais=$idpais";
$rs = mysql_query($cadena);
$row = mysql_fetch_array($rs);
$idpais=(int) $row["idpais"];
$pais=$row["pais"];
$pais1=$row["pais"];
$idcontinente=(int) $row["idcontinente"];
}
else $pais1="";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Edit/Add a Country</title>
<link rel="stylesheet" href="hojaestilo/@data001.css" type="text/css">
<style type="text/css">
#ajaxDiv { font-size:10px;font-weight:bold}
</style>
<script type="text/javascript">
function valida()
{
document.a.aceptar.style.display="inline";
if (document.a.pais.value=="") document.a.aceptar.style.display="none";
document.a.pais.focus();
}
function ajaxFunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
var response = ajaxRequest.responseText.split("|");
ajaxDisplay.innerHTML = response[0];
var TheCode = response[1];
if (TheCode == 1) {
document.a.aceptar.style.display="none";
document.a.pais.style.borderColor="red";
document.a.pais.style.borderStyle="double";
document.a.pais.focus();
document.a.esvalido.value="0";
}
else {
document.a.aceptar.style.display="inline";
document.a.pais.style.borderColor="#80a0a0";
document.a.pais.style.borderStyle="solid";
document.a.esvalido.value="1";
}
}
}
var unique = document.getElementById('pais').value;
var actual = document.getElementById('pais1').value;
var table = 'pais';
var field = 'pais';
var queryString = "?table="+table+"&unique="+unique+"&actual="+actual+"&field="+field;
ajaxRequest.open("GET", "checkunique.php" + queryString, true);
ajaxRequest.send(null);
}
function verifica(){
if(document.a.esvalido.value=="0" || document.a.pais.value=="") {
document.a.pais.focus();
return false;
}
return true;
}
</script>
</head>
<?php echo $bo;?>
<table class="tituloedicion" width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td>ADD/EDIT PAÍSES</td></tr></table>
<form name="a" method="post" onsubmit="return verifica();">
<table class="tablaedicion" width="100%" border="0" cellpadding="2" cellspacing="4">
<tr>
<td class='textoedicion' width="32%">PAÍS</td>
<td><input type="text" name="pais" id="pais" class='textos' value="<?php echo $pais;?>" size="40" maxlength="40" onkeyup="valida()" onblur="ajaxFunction()"><span id='ajaxDiv'></span></td>
</tr>
<tr>
<td class='textoedicion' width="32%">CONTINENTE</td>
<td><select name="idcontinente" id="idcontinente" class="textos" size="1" style="width:167px">
<?php
do {
?>
<option value="<?php echo $row_rsContinente['idcontinente']?>"<?php if (!(strcmp($row_rsContinente['idcontinente'],$idcontinente))) {echo "SELECTED";} ?>><?php echo $row_rsContinente['continente']?></option>
<?php
} while ($row_rsContinente = mysql_fetch_assoc($rsContinente));
$rows = mysql_num_rows($rsContinente);
if($rows > 0) {
mysql_data_seek($rsContinente, 0);
$row_rsContinente = mysql_fetch_assoc($rsContinente);
}
?></select></td>
</tr>
</table>
<input type='text' name='modo' value='grabar' style="visibility:hidden;display:none">
<input type='text' name='idpais' value="<?php echo $idpais;?>" style="visibility:hidden;display:none">
<input type='text' name='pais1' id='pais1' value="<?php echo $pais1;?>" style="visibility:hidden;display:none">
<input type='text' name='esvalido' id='esvalido' value="0" style="visibility:hidden;display:none">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" align="center" valign="middle" class='textoBotones'><input type="submit" class='botonGr' name="aceptar" value="SAVE" > <input type="button" class='botonGr' name="cancelar" value="CANCEL" onClick="window.close()"></td>
</tr>
</table>
</form>
</body>
</html>
checkunique.php (ajax page)
CODE
<?php
// session code, include file and database connection settins go here
$t=$_GET["table"]; // name of the table
$u= $_GET["unique"]; // unique value to check
$a=$_GET["actual"]; // actual value of the filed
$c=$_GET["field"]; // name of the unique field
$u = mysql_real_escape_string($u);
$sql = "SELECT $c from $t WHERE $c='$u'";
$total_registros=mysql_num_rows(mysql_query($sql)); // total records count
if ($total_registros>0) {
if( $a=="") {
$display_string = "Error|1";
}
else {
if ($a==$u)
$display_string = "Ok|0";
else
$display_string = "Error|1";
}
}
else {
$display_string = "Ok|0";
}
echo $display_string;
?>
// session code, include file and database connection settins go here
$t=$_GET["table"]; // name of the table
$u= $_GET["unique"]; // unique value to check
$a=$_GET["actual"]; // actual value of the filed
$c=$_GET["field"]; // name of the unique field
$u = mysql_real_escape_string($u);
$sql = "SELECT $c from $t WHERE $c='$u'";
$total_registros=mysql_num_rows(mysql_query($sql)); // total records count
if ($total_registros>0) {
if( $a=="") {
$display_string = "Error|1";
}
else {
if ($a==$u)
$display_string = "Ok|0";
else
$display_string = "Error|1";
}
}
else {
$display_string = "Ok|0";
}
echo $display_string;
?>
What i need is a way to disable the ENTER key when any of the TEXT boxes gets focused so if it is pressed do not submit the form simply goes to the next input of the form, only when the SUBMIT button gets focused and the ENTER key is pressed or when i click on it the form will be submitted.
Best regards,


