This topic is locked
[SOLVED]

 Execute PHP from javascript validation code

3/13/2016 7:36:36 AM
PHPRunner General questions
C
carlos.oschi@gmail.com author

hello, this alternative method was my solution to a simple trouble, the main idea is validate if the key exists in table "personas", the main trouble is "personas" has not a unique key, the main key rut is shared with other users, so the code of sql is
CREATE TABLE persona (

ID INT(11) NOT NULL AUTO_INCREMENT,

usuario_ID INT(11) NOT NULL,

empresa_ID INT(11) NOT NULL,

rut VARCHAR(10) NOT NULL,

nombres VARCHAR(50) NOT NULL,

PRIMARY KEY (ID));
if you can see, the rut field isn't unique for the all table, so the option "prevent duplicate" in the field definition doesn't work.
my solution was use the custom validation javascript (that can be put in c:\Program Files\PHPRunner8.1\source\include\validate\) and activate in the validate as option) and a second php file
the code is something like this.
javascript code
function validarut(sVal){





VALIDATION CODE IS HERE!!when all is ok with the code perform the aditional php code





$.get("./addon/inforut.php", {prut: sVal, tipo: "persona"}, function(respuesta){

if (respuesta.rut_existe == "SI")

{

alert("El rut ingresado ya existe");

}

}, "json");
inforut.php
<?php require_once("../include/dbcommon.php");





if (VALIDATION ROUTINE OK!!)

{echo json_encode(array("rut_existe"=>"SI"));}

else

{echo json_encode(array("rut_existe"=>"NO"));}}

?>
This work perfectly, but the asyncronous ejecution of java script doesn't allow to use the same text alert to validation. for that i use an alert popup message, is not the most elegant solution, but works.
If any can improve this, i really apreciate.
best regards

Carlos Oschilewski

Admin 3/14/2016

Instead of alert you can display validation message in some DIV next to field you validating. This is a better practice, do not obstruct user's way of action with popups.

C
carlos.oschi@gmail.com author 3/18/2016



Instead of alert you can display validation message in some DIV next to field you validating. This is a better practice, do not obstruct user's way of action with popups.


Nice idea, i only go to html mode, next to the field an add the next line next to the field...
<div id="rut_duplicado" style="display: block;"></div>
$.get("./addon/inforut.php", {prut: sVal, tipo: "persona"}, function(respuesta){

if (respuesta.rut_existe == "SI")

{

document.getElementById("rut_duplicado").innerHTML = '<font color=red>El rut ingresado ya existe</font>';

}

}, "json");

document.getElementById("rut_duplicado").innerHTML = ''; // this is for cleaning the message.

return true;