Link to home
Start Free TrialLog in
Avatar of Rick Danger
Rick DangerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Increase number of fields in existing form

Hi,

I am trying to amend a form for the voting section of our website. In the past, we have had 11 candidates, for which people could vote for up to seven. This year, we have 20 candidates, for which people can vote for up to 12.

I have managed to increase the number of candidates one can vote for from seven to 12, but for some reason I am yet to fathom, I can't increase the number of fields that appear when a person votes.

When a person votes, the Joomla form jos_voting is updated with their results, with a "Y" in each of the field they have voted for (e.g. choice 1, choice 3, choice 11 etc). However, despite there now being 20 candidates to choose from, there are still only 11 fields in the jos_voting form. Therefore, the results are not being correctly displayed.

I have attached the forms that I believe may need altering. However, there are other forms too that may relate to this, so please let me know if you don't think the relevant code is in here and I will attach others.

Many thanks,

Graeme
<?php defined( '_JEXEC' ) or die( 'Restricted access' );?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >

<?php

$user =& JFactory::getUser();
$username = $user->get('username');
$userid = $user->get('id');
$mineid = $username;

$db =& JFactory::getDBO();

$query = "SELECT vote_online FROM jos_dprsprofile WHERE userid='$userid'";

$db->setQuery($query);
$row = $db->loadObject();
$vote_online = $row->vote_online;

$sql = "SELECT * from jos_candidates ORDER BY id ASC";
$db->setQuery($sql);
$rows = $db->loadRowList();
$rows = (array) $rows;
$count = count($rows);

$box = '';
for ($i=0; $i<$count; $i++) {
	$j = $i+1;
	$box .= "box" . $j . " = document.forms.election.choice" . $j . ".checked;
";
}

$candidate = '';
for ($i=0; $i<$count; $i++) {
	$row3 = $rows[$i];
	$j = $i+1;
	$candidate .= "<div class='candidate'><input type='checkbox' name='choice" . $j . "' value='yes' onClick=\"countChoices(this)\"><div class='can'><div class='canlabel'>".$row3[1]."</div></div><div class='canprop'>";

	if (!$row3[3]) {  $candidate .= $row3[2] . "</div></div>"; }
	else { $candidate .= "Proposed by: " . $row3[2] . "<br />Seconded by: " . $row3[3] . "</div></div>"; }

}

$sql = "SELECT * from jos_boarddates ORDER BY year DESC";
$db->setQuery($sql);
$rowyear = $db->loadObject();
$year = $rowyear->year;
$open = $rowyear->open;
$close = $rowyear->close;
$agm = $rowyear->announceAgm;
$site = $rowyear->announceSite;
$regclose = $rowyear->regclose;



$votedisplay = "
<head>
<SCRIPT LANGUAGE='JavaScript'>


<!-- Begin
var Count = 0 
var max = 12

function countChoices(item)
{ 
  if(item.checked)
  {Count=Count+1}

else
 {Count=Count-1} 

if (Count>max)
  {item.checked=false 
  Count=Count-1 
  alert('You can only choose up to ' + max + ' choices. Uncheck an option if you want to pick another.') 
  } 

else if (Count == '0') {
alert('Please make a selection.');
return false;
	}
}
//  End -->


</script>
<link rel='stylesheet' href='forms/survey.css' type='text/css'>
<style type='text/css'>
.candidate {
	width: 45%;
	height: 65px;
	float: left;
}
</style>
</head>
<body>

<form name='election' action='index.php?option=com_content&view=article&id=363&Itemid=28' method='POST' onsubmit=\"return countChoices(this)\">
<FIELDSET>
<p>Below are the nominations for the ". $year ." Board Election. You may vote for a maximum of <strong>12</strong> candidates. Please indicate your choice by checking the boxes alongside the names and when you have finished press the button at the bottom of the page to submit your vote.
<p>Voting closes on <strong>".$close."</strong>.   The results of the election will be announced at the AGM on <strong>".$agm."</strong> and will be posted on the website from <strong>".$site."</strong>.</p>
<p>Please click <a href='index.php?option=com_content&view=article&id=362&Itemid=28' target=_blank' title='Candidates\' statements'>here</a> to read all of the candidate statements. 
<br />

" . $candidate . "



</FIELDSET>
<br>
<br>
<div class='clear'><div class='questionlong'><div class='label'>When you have made your selection of 12 candidates, please click the button below to submit your vote.</div></div></div>
<input type='submit' value='Submit'>

</form>
</body>";




if ($vote_online == 'yes') {
	$query = "SELECT * FROM jos_voting WHERE id='$userid'";
	$db->setQuery($query);
	$row2 = $db->loadObject();
	if ($row2) {
		$vote_already = $row2->vote_already;
		if ($vote_already == 'Y') 
		{
			echo "<h3>Thank you for your vote</h3><p>You have already submitted your vote for the ". $year ." Board Election. <p>Voting closes on <strong>".$close."</strong>.   The results of the Election will be announced at the AGM on <strong>".$agm."</strong> and will be posted on the website from <strong>".$site."</strong>.";
		} 
		else {
			echo $votedisplay;
		}
	}
	else {
		echo "<p>Unfortunately, you are unable to vote at this time. You need to have registered before <strong>".$regclose."</strong> in order to cast a vote online. If this is a mistake, please contact the office and they will be able to help you.</p><p>Voting commenced on <strong>Friday 2nd October</strong> and will close on <strong>".$close."</strong>.   The results of the election will be announced at the AGM on <strong>".$agm."</strong> and will be posted on the website from <strong>".$site."</strong>.";
	}
}
else {
	echo "<p>You have not registered for online voting and therefore will not be able to vote online via the website for the ". $year ." Board Election.  You should have received a paper ballot in the post.<p>Voting commenced on <strong>Wednesday 13th October</strong> and will close on <strong>".$close."</strong>.   The results of the election will be announced at the AGM on <strong>".$agm."</strong> and will be posted on the website from <strong>".$site."</strong>.
	<br />If you would like to register for online voting in the future please revisit this area after the AGM when the registration page will be back up.";

}



?>



</html>

Open in new window

<?php defined( '_JEXEC' ) or die( 'Restricted access' );?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
<?php
$user =& JFactory::getUser();
$username = $user->get('username');
$mineid=$username;
$userid = $user->get('id');

$db =& JFactory::getDBO();


$query = "SELECT * from jos_boarddates ORDER BY year DESC";
$db->setQuery($query);
$row = $db->loadObject();
$agm = $row->announceAgm;
$site = $row->announceSite;

$result2= mysql_query("SELECT vote_online FROM jos_dprsprofile WHERE userid='$userid'");
$vote_online = mysql_result($result2, 0, "vote_online");

if ($vote_online == 'yes') {
	$result3= mysql_query("SELECT vote_already FROM jos_voting WHERE id='$userid'");
	$vote_already = mysql_result($result3,0,"vote_already");
	if ($vote_already == 'Y') {
		echo "Thank you for voting. The results will be revealed at the AGM on ".$agm;
	}
	else 	{
		$query = "UPDATE jos_voting SET vote_already = 'Y' WHERE id = '$userid'";
		$r = mysql_query($query);

		$sql = "SELECT * from jos_candidates ORDER BY id ASC";
		$db->setQuery($sql);
		$rows = $db->loadRowList();
		$count = count($rows);
		for ($i=0; $i<$count; $i++) {
			$j=$i+1;
			$choice = "choice" . $j;
			$vote = $_POST[$choice];
			echo "VOTE" . $j . $vote;
			if ($vote) {
				$query = "UPDATE jos_voting SET $choice = '1' WHERE id='$userid'";
				echo $query;
				$result = mysql_query($query);
			}
			else {
				$query = "UPDATE jos_voting SET $choice = '0' WHERE id='$userid'";
				echo $query;
				$result = mysql_query($query);
			}
		}

	}
}
else 	{
	echo "You have not registered to vote and are unable to vote online. You should have received a paper ballot in the post. The results of the election will be revealed at the AGM on <strong>".$agm."</strong> and then posted here on <strong>".$site."</strong>.";
}

header("Location: index.php?option=com_content&view=article&id=128&Itemid=28");

?>

Open in new window

Avatar of Scott Madeira
Scott Madeira
Flag of United States of America image

Make sure you have 20 candidates in your database.  Looks like number of rows is determined by a count($rows) which is returned from your SQL query.  If you only have 11 candidates in the database it should only show 11 rows.

I would add some debug statements to verify that you are getting back more than 11 candidates from the database.
Avatar of Rick Danger

ASKER

Hi smadeira,

Thanks for replying. The candidates are saved in another form, jos_candidates, which I can confirm has 20 candidates stored within it.

I'm afraid I'm not familiar with debug statements - I'm fairly inexperienced with this - so if you could show me exactly what you mean, I'd be grateful.

Thanks.
I think smadeira means to write some custom php select statements and echo the results on the webpage.  This way you can make sure all of your varaiables actually have values in them.

For example you could put an echo string in on line 23 to echo the results of $vote_online and see what the values are...
Oh, ok. I've actually just found the following code, which might help.
<?php defined( '_JEXEC' ) or die( 'Restricted access' );?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
<?php

$db =& JFactory::getDBO();

mysql_query('drop TABLE jos_voting');

$query = "SELECT * from jos_candidates";
$db->setQuery($query);
$rows = $db->loadRowList();
$count = count($rows);

$sql = "create TABLE jos_voting (id INT(11), vote_already VARCHAR(1))";
$r = mysql_query($sql);

for ($i=0; $i<$count; $i++) {
	$j = $i+1;
	$choice = "choice" . $j;
	$query = "ALTER TABLE jos_voting ADD $choice TINYINT(4)";
	$r = mysql_query($query);
}



$sql = "SELECT * from jos_dprsprofile WHERE vote_online = 'yes'";
$db->setQuery($sql);
$rows = $db->loadRowList();
$count = count($rows);

for ($i=0; $i<$count; $i++) {
	$row = $rows[$i];
	$userid = $row[26];
	$result = mysql_query("INSERT into jos_voting (id) VALUES ('$userid')");
}

?>

Open in new window

Found it!

First script you posted...line 65 change from:

var max =12

to

var max = 20

See if that helps.
Although that might only be for the number of people you can select...
In your first script in your initial post put this on line 27:


echo 'Count = '.$count.PHP_EOL;


which should print out the number of candidates it found.

Below that you could do a

echo '<pre>',  print_r($rows) ,  '</pre>';

to have it dump the data from all of the rows to see how many candidates there are and who they are.
Yeah, I think it is, but thanks anyway. Any other ideas are most welcome!
Could it be that the code here in the last snippet I posted restricts it to 11?

$sql = "create TABLE jos_voting (id INT(11), vote_already VARCHAR(1))";
No, that's just create a table with a field that is set as an integer with up to 11 characters...so

int @ xxxxxxxxxxx chars...
What would happen if you stuck this is at line #27 on your first script: and commented everything else out...just comment, don't delete...

echo $count;
I'll have to try tomorrow, I'm away from the computer now. Will let you know how it goes...
No problem.  Take your time.  
Just to check, what do you mean by comment? As in, insert at the start?
Just wrap these around the rest of the code after line #27...it might throw some errors...might not.

/*   <code to comment out    */
Ok, so I've inserted echo $count; into line 27. Should I now insert /*   /* from line 28 to the end of the script?
ASKER CERTIFIED SOLUTION
Avatar of Rick Danger
Rick Danger
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I added a field to the table, which solved the problem.
This is correct.
Excellent.  Glad you were able to resolve it.