Solved

Increase number of fields in existing form

Posted on 2011-09-28
20
319 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:rick_danger
  • 10
  • 8
  • 2
20 Comments
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 36717463
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.
0
 

Author Comment

by:rick_danger
ID: 36717500
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.
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36718356
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...
0
 

Author Comment

by:rick_danger
ID: 36718373
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

0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36718413
Found it!

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

var max =12

to

var max = 20

See if that helps.
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36718418
Although that might only be for the number of people you can select...
0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 36719510
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.
0
 

Author Comment

by:rick_danger
ID: 36719571
Yeah, I think it is, but thanks anyway. Any other ideas are most welcome!
0
 

Author Comment

by:rick_danger
ID: 36719598
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))";
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36719710
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...
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 13

Expert Comment

by:NUKIT
ID: 36719720
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;
0
 

Author Comment

by:rick_danger
ID: 36719765
I'll have to try tomorrow, I'm away from the computer now. Will let you know how it goes...
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36719772
No problem.  Take your time.  
0
 

Author Comment

by:rick_danger
ID: 36719869
Just to check, what do you mean by comment? As in, insert at the start?
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36719900
Just wrap these around the rest of the code after line #27...it might throw some errors...might not.

/*   <code to comment out    */
0
 

Author Comment

by:rick_danger
ID: 36813541
Ok, so I've inserted echo $count; into line 27. Should I now insert /*   /* from line 28 to the end of the script?
0
 

Accepted Solution

by:
rick_danger earned 0 total points
ID: 36814665
It's ok, I've cracked it! It was just a case of adding more fields to the table. Thanks anyway.
0
 

Author Closing Comment

by:rick_danger
ID: 36908404
I added a field to the table, which solved the problem.
0
 

Author Comment

by:rick_danger
ID: 36814682
This is correct.
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36814690
Excellent.  Glad you were able to resolve it.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now