我有以下代码:
<?php
include_once 'init/init.funcs.php';
$pollid=(int) $_GET['pollid'];
$questions = array();
$result = mysql_query('SELECT kysimus FROM kysimused where kysimustik_id="' . $pollid . '"');
while($row = mysql_fetch_assoc($result)) {
$questions[] = $row['kysimus'];
}
$count=count($questions);
$x=0;
while ($x<$count){
echo $questions[$x];
$x+=1;
}
?>
<form method="get">
<input type="submit" value="Submit">
</form>
它从数据库中获取了具有kysimustik_id = poll_id的问题,然后回复它们。但是我怎么能让它工作,所以当时问题会出现一个问题,只有在我点击&#34;提交&#34;按钮?
答案 0 :(得分:0)
您可以执行以下操作
<div class="row text-center">
<button type="submit" class="btn btn-lg btn-info">Click Here!</button>
</div>
$(function(){
var values = [
"hi",
"hello",
"nice",
"wonderful"
];
var counter = 0;
$(".btn-lg").on("click", function(){
if(values.length) {
counter = (counter + 1) % values.length;
$('.row.text-center').append(values[counter] + ' ');
}
});
});
在上面的例子中,我使用了javascript数组。您可以通过php数组轻松替换它
答案 1 :(得分:0)
<?php
include_once 'init/init.funcs.php';
$pollid=(int) $_GET['pollid'];
if(isset($_POST['QuestionID'])
{
$questionID = $_POST["QuestionID"];
HandleAnswer($pollid,$questionID,$_POST['Answer']);
}
else
{
$questionID = 0;
}
$questions = array();
$result = mysql_query('SELECT kysimus FROM kysimused where kysimustik_id="' . $pollid . '"');
while($row = mysql_fetch_assoc($result)) {
$questions[] = $row['kysimus'];
}
function HandleAnswer(string PollId, string QuestionID, string Answer){
// Implement piece of code that sets the answer in the database,
// I leave this up to you since i have no Idea how your database is structured
}
$questionID++;
$count=count($questions);
if($questionID < $count){
?>
<form method="post" action="mypage?=<?php echo $pollid; ?>">
<?php echo $questions[$questionID]; ?>
<input type="hidden" name="questionID" value="<?php echo $questionID;?>"/>
<input type="text" name="Answer"/>
<input type="submit" value="Submit">
</form>
<?php }else{ ?>
THese were all our questions, thank you for answering!
<?php } ?>
我自己没有测试代码,但我确信它有效。请注意,这是一种非常不安全的方法,因为您可以使用扩展名(如firebug)编辑隐藏字段的值。