Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
need help-survey voting
Old 11-08-2009, 01:02 AM need help-survey voting
Junior Talker

Posts: 3
Trades: 0
i'm a beginner and new to php. i'm having problem with survey script and voting function. can anyone please help me out.. the script only accept the last question's answer and update the database = acount+1.

script to display survey:

Code:
<?php

echo "<form action='vote.php' method=post>
<table width='300' border='0' cellspacing='0' cellpadding='0' align='center'>";

$query1 = "SELECT * FROM questions";
$result1 = mysql_query($query1)or die("ERROR: $query.".mysql_error());

$num_row=mysql_num_rows($result1);

$i=1;

while($i<$num_row){

while ($row1 = mysql_fetch_array($result1)) {

$qid = $row1['qid'];
$qtitle = $row1['qtitle'];

$query2 = "SELECT * FROM answers WHERE qid='$qid'";
$result2 = mysql_query($query2) or die("ERROR: $query.".mysql_error());

echo "<tr>
    <td width='300' align='left'><p>".$i.". ".$qtitle."</p></td>
 	 </tr>";
	 
while ($row2 = mysql_fetch_array($result2)) {

$aid = $row2['aid'];
$atitle = $row2['atitle'];

echo "<tr>
    <td width='300' align='left'><input type = radio name =aid value = '".$aid."'><font>".$atitle."</font></input><br></p></td>
 	 </tr>";
}

echo "<input type = hidden name = qid value = '".$qid."'>";
echo "<br />";
$i++;

}
}

echo "<td><br><br></td><tr>
<td width='300' align='center'><input type = submit name = submit value = 'Submit'></td>
</tr>
</table>
</form>";

}
}
// close connection
mysql_close();

?>

voting script:
Code:
<?php
if (isset($_COOKIE) && !empty($_COOKIE)) {
    if ($_COOKIE['lastpoll'] && $_COOKIE['lastpoll'] == $_POST['qid']) {
	echo '<script>alert("You have already voted. Thank You !!!");</script>';
	echo '<script>location.replace("trysurvey.php");</script>';
	exit();
    }
}
// set cookie
setCookie('lastpoll', $_POST['qid'], time() + 2592000);


if (isset($_POST['submit'])) {

    if (!isset($_POST['aid'])) {
		echo '<script>alert("Please select one of the available choices.");</script>';
		echo '<script>history.back(1);</script>';
    }
	else{
	
	require_once('config.php');
	
	$aid=$_POST['aid'];
	$qid=$_POST['qid'];
   
    // update vote counter
    $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid' AND qid = '$qid'";
    $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
	
	}

    // close connection
    mysql_close();


    // print success message    
	echo '<script>alert("Your have successfully voted.");</script>';
	echo '<script>location.replace("trysurvey.php");</script>';
}
else {
    echo '<script>alert("Unable to submit your Vote. Please try again !!!");</script>';
	echo '<script>history.back(1);</script>';
}

?>
please help... thanks in advance
wayz1229 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-23-2009, 11:17 AM Re: need help-survey voting
Experienced Talker

Posts: 48
Name: Pieter
Location: Holland
Trades: 0
The name's of you're fields are the same so it only takes the last one
You could use you're int $i to make the names different.
elcosmo is offline
Reply With Quote
View Public Profile
 
Old 11-23-2009, 11:55 AM Re: need help-survey voting
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
In the seccond loop, where you output the radio button, you can use the question ID as name attribute, as in

PHP Code:
echo '<input type="radio" name="' $qid '" value="' $aid '">'
Then you would end up with an array where the question IDs are keys and the selected awnser are value, as in

PHP Code:
$_POST = array(
   
'qid_1' => 'aid_1',
   
'qid_2' => 'aid_2',
   
'qid_3' => 'aid_3',
   ...
); 
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to need help-survey voting
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33020 seconds with 12 queries