/    Sign up×
Community /Pin to ProfileBookmark

I have a radio button poll form that I am developing. How can I make the submit button disappear once the user clicks submit? (I am displaying the results just below the questions, once the user hits submit. That works, but submit button still stays there…I tried all kinds of if else statemetns, with issets, is_nulls etc…I can not make the submit button go away.)

Here is my code, after connecting to database: (by the way it submits to itself, this file is called displaypoll. – may be that is the mistake, but if i dont do it, and submit the poll to another file, a whole new page comes, only with the poll results, and my home page where the poll sists in, disappear, which i dont want.)

[code=php]

$query=mysql_query(“select * from questions WHERE `category`='{$kat}’ ORDER BY id DESC LIMIT 1”);

while ($row = mysql_fetch_array($query)):
$id=$row[‘id’];
$category=$row[‘category’];
$title=$row[‘title’];
$date=$row[‘date’];
$q1=$row[‘q1’];
$q2=$row[‘q2’];
$q3=$row[‘q3’];
$q4=$row[‘q4’];
$q5=$row[‘q5’];
$q6=$row[‘q6’];
$q7=$row[‘q7’];
$q8=$row[‘q8’];
$qtotal=$row[‘qtotal’];

endwhile;

$sot = array(0,$q1,$q2,$q3,$q4,$q5,$q6,$q7,$q8);

echo “<form method=”post” action=”displaypoll.php”>”;

echo $title;
echo “</br>”;

for($i=1; $i<=$qtotal; $i++)

{

$answer=”answer”.$i;
echo “<input type=”radio” name=”answer” value=$answer>”;
echo $sot[$i];
echo “</br>”;

}

echo “<input type=”hidden” name=”aypi” value=$userip>”;
echo “<input type=”hidden” name=”aydi” value=$id>”;
echo “<input type=”hidden” name=”qtotal” value=$qtotal>”;

echo “<input type=”submit” name=”submitname” value=”submit my answer”/>”;

echo “</form>”;

//////////Below is the response display part

$response=$_POST[‘answer’];
$userip=$_POST[‘aypi’];
$idpoll=$_POST[‘aydi’];
$qtotal=$_POST[‘qtotal’];
$submitname=$_POST[‘submitname’];

$query1=”INSERT INTO answers1 (idpoll, userip, response) VALUES (‘$idpoll’,’$userip’,’$response’)”;
mysql_query($query1) or die (‘ I could not update the database’);

for($i=1; $i<=$qtotal; $i++)

{

$answer=”answer”.$i;
$query=”query”.$i;

$query=”select count(1) FROM answers1 WHERE `idpoll`='{$idpoll}’ AND `response`='{$answer}’ “;

$rs=mysql_query($query);

$total = mysql_fetch_array($rs);

echo $answer;
echo $total[0];

}

?>

[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterOct 12.2008 — Not quite a complete solution but some code to point you in the right direction:

First define a class:

[color=blue]classes/poll.php:[/color]
[code=php]
<?
if(!class_exists('poll')){
class poll{
public $result;
function poll($kat){
$kat=mysql_real_escape_string($kat);
$sql="SELECT * FROM questions WHERE category='$kat' ORDER BY id DESC LIMIT 1";
$result=mysql_query($sql);
if(!$result||mysql_num_rows($result)!=1){return 0;}
$this->result=$result;
}//function
function hasVoted($ip){
$ip=mysql_real_escape_string($ip);
$idpoll=$this->result['id'];
$idpoll=mysql_real_escape_string($idpoll);
$sql="SELECT COUNT(1) FROM answers1 WHERE idpoll='$idpoll' AND userip='$ip' LIMIT 1";
$result=mysql_query($sql);
if(!$result||mysql_num_rows($result)!=1){return 0;}
return 1;
}//function
}//class
}//if
?>
[/code]


Then use the class with something like:
[code=php]
...
$kat="myCategory";
include("classes/poll.php");
$ip=$_SERVER['REMOTE_ADDR'];//in case of proxy usage you might wanna switch to:
//$ip=isset($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR'];
$p=new poll($kat);
if($p->hasVoted($ip)==0){
echo '<input type="submit" name="submitname" value="submit my answer" />';
}
...
[/code]
Copy linkTweet thisAlerts:
@ariellOct 12.2008 — This is an extract from code that I use:

[code=php]
<?php
if (! isset($_POST['submitPoll'])) { // submit button insertion
$c = '<label name="clearlist" id="clearlist">';
$c .= '<input type="button" name="submitPoll" ';
$c .= 'onclick="clearlist.innerHTML=''; formName.submit(); " ';
$c .= 'value="Submit Poll"/>';
$c .= '</label>';
echo($c);
} // submit button insertion
?>
[/code]


You certainly have to replace "formName".

The code displays a "submit" button only, if form wasn't sent yet. Button disappears as soon as user submits form. If you first want to validate, you'll have to extend " if(! (isset..." to something like " && (! <error-condition>".

As for repeated form calls, those are easy to control with a simple db-request (namely whether or not user has already voted).

Best from the south.
×

Success!

Help @ketanco spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...