Forums

[OK] How to validate a field from a DB query, before submit?

Aluizojr 30 Mar, 2011
Hello GreyHead! First I wish to thank you a lot. Because I followed other topics and solved all of my problems.

Well, my question is: Could the form check if a value was already been submitted, before someones try to submit the same value?

The objective is to display an user-friendly error related to a key marked as "unique" in the DB.
So, if I fill all the fields and submit repeating the value, I'll get a SQL error message saying that value couldn't be repeated ("unique"). That's not user-friendly, and will not be record in DB.

Could you help me on this? Thank you!!
GreyHead 31 Mar, 2011
Hi Aluizojr,

You can do this check in two ways.

a) In the browser using Ajax to check the database. There's an extract from The ChronoForms Book here explaining how to check an email address like this.

b) In the ServerSide validation - where you'd add the code to do a database lookup.

Ideally you'd add both for security :-)

Bob
Aluizojr 31 Mar, 2011
Oh thank you for all mr. Bob!
Well, due my small time to do that, I did a server-side validation with the help of another topics. I added a link to the user go back and correct the field, but I couldn't recover the Session back, all the fields were blank again.
Because my knowledge in PHP are pretty little, could you help me to do this?

Best regards,
Aluizojr.
GreyHead 31 Mar, 2011
Hi Aluizojr,

Please set "Republish fields if error occured" to "Try to republish" on the Form Editor General tab.

Bob
Aluizojr 31 Mar, 2011
Hi Bob,

I'd set "Republish fields if error occured" to "Try to republish", but I still get back with the fields blank.
The link has a javascript code: <a href="javascript:history.go(-1);">Clique aqui para corrigir o CPF. </a>

Thanks for the light speed answer!
GreyHead 31 Mar, 2011
Hi Aluizojr ,

Then don't use that link. That's relying on the browser to store the form info.

Where have you put it anyhow? If there is an error found in the serverside validation the form will be re-loaded without needing a link.

Bob
Aluizojr 31 Mar, 2011
Hi Bob! 🙂

There's the code in serverside validation. Also there's comments and some text in Portuguese, which is my language, sorry.

<?php
$cpf = $_REQUEST['cpf'];

$sql = mysql_query("SELECT * FROM jos_chronoforms_Trabalhe_Conosco WHERE cpf = '$cpf'");

if(mysql_num_rows($sql) == 1){
//Aqui é se o CPF já constar no banco (below if CPF already exists)
echo '<h2 style="color: red">CPF já consta no cadastro.</h2><br/><a href="javascript:history.go(-1);">Clique aqui para corrigir o CPF. </a>';
}else{
//Aqui é se o CPF não estiver cadastrado (below if CPF don't exists, so everything's ok)
echo '<h3>Obrigado!</h3>
<h2>Seu cadastro foi efetuado com sucesso!</h2>';

}
?>
GreyHead 31 Mar, 2011
Hi Aluizojr,

Here's your code re-written in Joomla! code. This needs to go in the ServerSide validation box on the Validation tab or it will not work correctly.
<?php
$cpf = JRequest::getString('cpf', '', 'post');
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__chronoforms_Trabalhe_Conosco`
        WHERE `cpf` = $db->quote($cpf) ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {
  //Aqui é se o CPF já constar no banco (below if CPF already exists)
  return 'CPF já consta no cadastro.';
}
?>

Bob
Aluizojr 31 Mar, 2011
OMG! It worked very sweet now!😀

Sure I owe you a big one Beer, Bob! As soon as I got a international card!
I'll study more to help the community too!😀
I wish the best for you!

Many thanks for your tips,
Aluízo Jr. 8)
This topic is locked and no more replies can be posted.