我写了一个简短的PHP程序,显示了两张图片。使用2个单选按钮,您必须选择一个,然后您必须单击提交按钮。然后,页面保存对您选择的图片的投票,然后显示接下来的两张图片。 我使用会话变量来存储,必须显示哪些图像。 不幸的是,在第一次点击" next"之后,变量没有增加。按钮。之后,它运作得非常好。
有什么问题?
这是我的代码:
<?php
session_start();
if (isset($_SESSION['image_nr'])) { /* If there is already a value set */
$x = $_SESSION['image_nr'];
echo $x;
}
else {
$_SESSION['image_nr'] = 1; /* Set to 1 */
$x = $_SESSION['image_nr'];
}
include('sql/db_connect.php');
include('sql/queries.php');
if($x == 9){
increaseParticipants();
$_POST['submit']='';
$_SESSION['image_nr'] = 1;
session_unset($_SESSION['image_nr']);
header( "Location: thankyou.php" );
}
?>
<html>
<header>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" language="JavaScript" src="functions.js"></script>
</header>
<body>
<form action= "" method = "post">
<div class='content' id='image1'><?php if($x == 1){echo"<img src='images/image_1.jpg' width='380'><br>Image 1";}?></div>
<div class='content2' id='image2'><?php if($x == 1){echo"<img src='images/image_2.jpg' width='380'><br>Image 2";}?></div>
<div id='bottom'>
<input type='radio' name='radio' id='radio1' value='<?php echo $x; ?>' onchange="enableButton()"> Image <?php echo $x; ?> <br>
<input type='radio' name='radio' id='radio2' value='<?php echo ($x+1);?>' onchange="enableButton()"> Image <?php echo $x+1; ?><br>
<input type="submit" id="button" name= "submit" value="Next" onclick="saveClick()" disabled />
</div>
<?php
if (isset($_POST['submit']) && $_POST['submit']!='') {
if(isset($_POST['radio'])) {
$image = $_POST['radio'];
increaseClicks($image);
$x = $x+2;
}
$_SESSION['image_nr'] = $x;
$next = getNext($image);
echo '<script type="text/javascript">'
, 'changeImage('.$next.');'
, '</script>'
;
$_POST['submit']='';
}
?>
</form>
</body>
</html>
这是JS函数:
function enableButton(){
document.getElementById("button").disabled = false;
}
function changeImage(next){
var firstpart = "<img src='images/image_";
var secondpart = ".jpg' width='380'><br>Bild"+next;
var inHtml = firstpart.concat(next, secondpart);
document.getElementById("bild1").innerHTML = inHtml;
var secondpart = ".jpg' width='380'><br>Bild"+(next+1);
var inHtml = firstpart.concat((next+1), secondpart);
document.getElementById("bild2").innerHTML = inHtml;
}
查询(我知道这些已被弃用)
<?php
function getClicks ($image){
$q1 = "SELECT clicks from tbl_clicks WHERE ID_bild=".$image;
$rq1 = mysql_query($q1);
$return = mysql_fetch_array($rq1);
$number_of_clicks = $return['clicks'];
return $number_of_clicks;
}
function getClicksAll (){
$q1 = "SELECT ID_bild, name, clicks from tbl_clicks";
$rq1 = mysql_query($q1);
return $rq1;
}
function increaseClicks ($image){
$clicks = getClicks($image);
$clicks = (int)$clicks;
$clicks = $clicks + 1;
echo "this are the clicks: ".$clicks;
$q2 = "UPDATE tbl_clicks SET clicks = ".$clicks." WHERE ID_bild = ".$image;
mysql_query ($q2);
}
function getNext ($image){
$q3 = "SELECT next from tbl_clicks WHERE ID_bild=".$image;
$rq3 = mysql_query($q3);
$return = mysql_fetch_array($rq3);
$next = $return['next'];
return $next;
}
function getParticipants(){
$q4 = "SELECT participants from tbl_participants";
$rq4 = mysql_query($q4);
$return = mysql_fetch_array($rq4);
$participants = $return['participants'];
return $participants;
}
function increaseParticipants(){
$participants = getParticipants();
$q5 = "UPDATE tbl_participants SET participants = ".($participants+1);
mysql_query ($q5);
}
?>