Forums

please help, check combination of 3 field is exist before su

moivan 03 Oct, 2012
Please help,
i'm creating a form that contain 3 data field as the parameter, it was year, month and name.
my goals is to forbid data resubmission if combination of that 3 field is exist. for example:
if data for moivan oct 2012 exist then the user can't submit the data for that month and year again.
i've buy the Chronoform v4 using ajax.pdf and try to follow the guide but i really don'have the knowledge on java.
i'm using dropdown for month and year field and default hidden value for name.

sorry for my bad english.
thankyou in advance
GreyHead 03 Oct, 2012
Hi moivan,

Probably the easiest way to do this is to use a Custom ServerSide Validation action to check if a record exists. You will need some PHP and MySQL experience to create the validation code though.

It could be done using Ajax but the JavaScript will be more complicated because you need to check for changes in any of the three inputs.

Bob
moivan 03 Oct, 2012
thanks for info bob,
how about this ?
<?php
$cekmonth = trim(strip_tags($_POST['month']));
$cekyear = trim(strip_tags($_POST['year']));
$cekname = trim(strip_tags($_POST['name']));
?>
<?php
   $conn =mysql_connect("localhost","root","");
   $db = mysql_select_db("kbpp",$conn);
   $sql = mysql_query("SELECT name, month, year from hsnjz_chronoforms_data_pesertaperkecamatan WHERE name = '$cekname' AND month = '$cekmonth' AND year = '$cekyear'");
   $cekmonthyear = mysql_query($sql,$conn);
   ?>
   
<?php
if (!$cekbulantahun) {
    die('Query failed to execute for some reason');
}

if (mysql_num_rows($cekmonthyear) > 0) {
return false;
?>
    


regards
GreyHead 04 Oct, 2012
Hi moivan,

Here's how I'd write that for Joomla!/ChronoForms
<?php
$cekmonth = '';
$cekyear = '';
$cekname = '';
if ( isset($form->data['month']) && $form->data['month'] ) {
	$cekmonth = trim(strip_tags($form->data['month']));
}
if ( isset($form->data['year']) && $form->data['year'] ) {
	$cekyear = trim(strip_tags($form->data['year']));
}
if ( isset($form->data['name']) && $form->data['name'] ) {
	$cekname = trim(strip_tags($form->data['name']));
}
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__chronoforms_data_pesertaperkecamatan`
        WHERE `name` = '{$cekname}' AND `month` = '{$cekmonth}' AND `year` = '{$cekyear}' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count > 0 ) {
	$form->validation_errors['name'] = "Add message here";
	return false;
}
?>

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger