Forums

Validation of fields by / Parsing multiple values

p1ngu1n 22 Oct, 2012
I have a php script inside a form which reads data from a MySQL DB and creates a group of checkboxes according to them. It all worked fine with V3 but now I got two issues:

1.) I need to check if there is at least one box checked, how can I do that? Assigning any special class?

2.) If more then one box is checked it does not parse these values into an email or confirmation page.

-->
<input type="radio" name="seminarauswahl" ... 
leads to just one value

-->
<input type="radio" name="seminarauswahl[]" ... 
leads to an array output but not as separated values.



Many thanks and regards

Jakob
GreyHead 23 Oct, 2012
Hi p1ngu1n,

I'm not sure what you actually have here. You've written about a checkbox group but the code you posted is for radio buttons?

I think you can validate a Checkbox group in CFv4 using the 'required' setting. And to handle the Arrays you need to drag in a Handle Arrays action to the On Submit event.

Bob
p1ngu1n 23 Oct, 2012

I'm not sure what you actually have here. You've written about a checkbox group but the code you posted is for radio buttons?



Sorry my fault. The "old" version was with the checkboxes. Because of the problem parsing several values, I changed it to radio buttons which is fine for me. So the only issue - I still have - is the validation.

I've set up a custom element which includes the code. The PHP code looks like this:


<?php  
  $host = "XXX";
  $db	= "XXX";
  $user = "XXX";
  $pw	= "XXX";
  $i    = 0;
  $mysqlconnect = mysql_connect($host,$user,$pw) or die("Keine Verbindung möglich: ".mysql_error());
  mysql_select_db($db) or die("Auswahl der Datenbank fehlgeschlagen");

	$query = "SELECT * FROM termine_anmeldung ORDER BY id";
	$res_termine = mysql_query($query) or die("Anfrage fehlgeschlagen: " . mysql_error());
	while ($line_termine = mysql_fetch_object($res_termine)) {
	?>
    <input type="radio" name="seminarauswahl" id="seminarauswahl_<?php echo $i; ?>" title="Bitte wählen Sie mind. ein Seminar!" value="<?php echo $line_termine->text; ?>">
    <label for="eminarauswahl_<?php echo $i; ?>"><?php echo $line_termine->text; ?></label><br>
<?php } ?>


It all works perfectly, but I want to make sure that the user has to choose one radio button before submitting. If I set up a radio button using your form I have the validation option. I thought I could implement that in my script?


Many thanks for you help!!

Jakob
GreyHead 25 Oct, 2012
Hi p1gu1n,

The simplest way to do this is by making these into a radio button group and setting the validate class for them.
    <?php 
$db =& JFactory::getDBO();
$query = "
    SELECT *
        FROM `termine_anmeldung`
        ORDER BY `id` ;
";
$db->setQuery($query);
$data = $db->loadObjectList();
foreach ( $data as $k => $v ) {
?>
<input type="radio" name="seminarauswahl[]" id="seminarauswahl_<?php echo $k; ?>" title="Bitte wählen Sie mind. ein Seminar!" value="<?php echo $v->text; ?>" class=" validate['required']" />
<label for="eminarauswahl_<?php echo $k; ?>" ><?php echo $v->text; ?></label><br />
<?php 
}
?>

Bob
p1ngu1n 25 Oct, 2012
Hey Bob,

first many thanks for your quick and detailed answer.

The complete code now looks like this


<?php 

	$lt_host = X
	$lt_db    = X
	$lt_user = X
	$lt_pw   = X

	$mysqlconnect = mysql_connect($lt_host,$lt_user,$lt_pw) or die("Keine Verbindung möglich: ".mysql_error());
	mysql_select_db($lt_db) or die("Auswahl der Datenbank fehlgeschlagen");
	
	$db =& JFactory::getDBO();
	$query = "SELECT * FROM `termine_anmeldung` ORDER BY `sortierung`;";
	$db->setQuery($query);
	$data = $db->loadObjectList();
	foreach ( $data as $k => $v ) {
		?>
		<input type="radio" name="seminarauswahl[]" id="seminarauswahl_<?php echo $k; ?>" title="Bitte wählen Sie mind. ein Seminar!" value="<?php echo $v->text; ?>" class=" validate['required']" />
		<label for="seminarauswahl_<?php echo $k; ?>" ><?php echo $v->text; ?></label>
		<br />
		<?php 
	}
?>


Which leads to the following error

Warning: Invalid argument supplied for foreach() in /mnt/web2/********/administrator/components/com_chronoforms/form_actions/show_html/cfaction_show_html.php(142) : eval()'d code on line 46


I'm not sure about your line
$db =& JFactory::getDBO();

Do I still need the sql connection part?


Many thanks

Jakob
GreyHead 25 Oct, 2012
Hi p1ngu1n,

You probably aren't getting a DB connection. So I guess this table isn't in the Joomla! database? You can set up a remote connection like this:
<?php
$options = array();
$options['driver']    = 'mysqli'; // Database driver name
$options['host']      = 'xxx'; // Database host name
$options['user']      = 'xxx'; // User for database authentication
$options['password']  = 'xxx'; // Password for database authentication
$options['database']  = 'xxx'; // Database name
// $options['prefix'] = ''); // probably not needed
$db2 = & JDatabase::getInstance($options);
. . .
$db2->setQuery($query);
$data = $db2->loadObjectList();
. . .


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