2

我尝试了许多不同的方法,但我似乎无法弄清楚为什么提交按钮在第一次单击时不起作用。我已经从代码中删除了 if 语句,但仍然需要单击两次。在第一次单击时,它会刷新页面,当您单击必须向下滚动到的提交按钮时,这是最明显的。下面的代码在页面上重复了大约 27 次,但歌曲不同。在第二次点击后它们都可以工作,所以我很确定它们连接正确。此外,当您单击按钮一次,然后如果您单击另一个按钮,它会提交您单击的第一个按钮。

如果有人想看看它的实际外观,我已经在页面上设置了一个测试帐户。http://www.saint57records.com/rewards/index.php用户名:test 密码:test

<?php
$resultstwo = mysqli_query($con,"Select * FROM users WHERE user_name ='$username'");
while ($row = mysqli_fetch_array($resultstwo))
{
extract($row);
}
?>
<form method="post" action="index.php">
  <?php
    //If submit form was clicked
    if(isset($_POST['endo'])) {
        //Server side validation for security purposes
       if($points >= 100 AND $endo == 0 AND $savetheworldalbum == 0) {
            mysqli_query($con,"UPDATE users SET points = points - 100 WHERE users.user_name = '$username' LIMIT 1");
            mysqli_query($con,"UPDATE users SET endo = 1 WHERE users.user_name = '$username' LIMIT 1");
        }
    }
    if ($points >= 100 AND $endo == 0 AND $savetheworldalbum == 0) {
        echo '<input type="submit" name="endo" value="100pts">';
    } elseif ($endo == 1 OR $savetheworldalbum == 1) {
        echo '<input type="submit" name="submit" value="100pts" disabled title="You already earned this track!">';
    } else {
        echo '<input type="submit" name="submit" value="100pts" disabled title="You need at least 100 points for this download">';
    } 
  ?>
</form>

这就是输出

<form method="post" action="index.php">
<input type="submit" name="endo" value="100pts"></form>                                                    
4

2 回答 2

1

看起来您在$endo决定是否禁用提交按钮之后设置 php 变量。首先阅读$_POST,收集数据,然后生成 HTML。但是,除非您包含设置 $endo 的代码,否则我无法确定

于 2013-04-12T18:31:16.203 回答
1

尝试将提交处理程序放在表单之外(html/php 文件的顶部)。

<?php
if(isset($_POST['endo'])) {
    //Server side validation for security purposes
   if($points >= 100 AND $endo == 0 AND $savetheworldalbum == 0) {
        mysqli_query($con,"UPDATE users SET points = points - 100 WHERE users.user_name = '$username' LIMIT 1");
        mysqli_query($con,"UPDATE users SET endo = 1 WHERE users.user_name = '$username' LIMIT 1");
    }
} else { ?>
<html>
.`
.

于 2013-04-12T18:57:25.493 回答