0

我网站的用户可以订阅他们的一些动物参加比赛。我的代码运行良好,但存在一个大问题。当用户按下订阅时,页面会重新加载,但由于 isset 在按钮本身的回声之后,因此需要在按钮文本更改为“已订阅”之前刷新页面。如您所见,更改顺序会产生问题。谁能帮帮我?我自己没有选择。(我翻译了变量等)

<form action="" method="post" name="frmSubscribe">
<?php
$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){

    echo $row['Animalname'];
    $Duiven[] = $row['AnimalID'];
    $Username[] = $row['username'];

    ?>
    <input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php 
    $sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
    $resultSubscribed = mysql_query($sqlSubscribed);
    if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
    else {echo "deregister";}
    ?>'><br/><?php
    $Teller++;
    }

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

$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[0]."'";
    $resultCheck = mysql_query($sqlCheck);
    if (mysql_num_rows($resultCheck) == 0){

    $sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".$Animals[0]."','".$Username[0]."')";
    $result1 = mysql_query($sql1);
    $row=mysql_fetch_array($result1);
    ?><?php
    }
    }

......等下一个动物。

4

4 回答 4

1

我认为你必须使用输出缓冲。将 ob_start() 放在页面的开头,现在输出任何占位符而不是真正的输入代码:

[SUMBIT]

代替:

<input type='submit' ....

在页面末尾,您将获得缓冲内容:

$content = ob_get_clean()

并用正确的代码替换提交按钮:

$content = str_replace('[SUBMIT]', 'Your actual submit button code here...', $content);

现在输出内容:

echo $content;
于 2012-10-10T10:44:30.593 回答
1

快速修复可以是javascript

echo '<span id="someid" >Subscribe</span>';

和改变的时候

echo "<script>document.getElementById('someid').innerHTML = 'Subscribed';</script>";

以后我强烈建议您使用PHP MVC 框架

于 2012-10-10T10:45:45.020 回答
0

您可以在页面上方使用您的邮政编码,然后加载您的页面....请使用如下代码:

 <?php
    if (isset($_POST['btnSubscribe1'])){

    $sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[0]."'";
        $resultCheck = mysql_query($sqlCheck);
        if (mysql_num_rows($resultCheck) == 0){

            $sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".$Animals[0]."','".$Username[0]."')";
           mysql_query($sql1);
           header('Location: samefilename.php');
           die;
         }

        }
    ?>
    <form action="" method="post" name="frmSubscribe">
    <?php
    $Counter = 0;
    $sql = "Select * from Animals where username='".$_SESSION['User']."' ";
    $result = mysql_query($sql);
    while($row=mysql_fetch_array($result)){

        echo $row['Animalname'];
        $Duiven[] = $row['AnimalID'];
        $Username[] = $row['username'];

        ?>
        <input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php 
        $sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
        $resultSubscribed = mysql_query($sqlSubscribed);
        if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
        else {echo "deregister";}
        ?>'><br/><?php
        $Teller++;
        }


        }
于 2012-10-10T10:50:15.187 回答
0

最简单的解决方案将isset向上移动到顶部。只是为了让您知道操作应该始终是您处理的第一件事,然后您将在视图上工作

<form action="" method="post" name="frmSubscribe">
<?php

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

$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".key($_GET)."'";
$resultCheck = mysql_query($sqlCheck);
if (mysql_num_rows($resultCheck) == 0){

$sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".key($_GET)."','".$_SESSION['User']."')";
$result1 = mysql_query($sql1);
$row=mysql_fetch_array($result1);

}
}

$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){

echo $row['Animalname'];
$Duiven[] = $row['AnimalID'];
$Username[] = $row['username'];

?>
<input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php 
$sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
$resultSubscribed = mysql_query($sqlSubscribed);
if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
else {echo "deregister";}
?>'><br/><?php
$Teller++;
}
于 2012-10-10T10:52:42.587 回答