简体   繁体   English

php如何确保重新加载页面并将echo更新为新值

[英]php how can I make sure the page is reloaded and the echo is updated to the new value

The users of my website can subscribe some of their animals for a competition. 我网站的用户可以订阅他们的一些动物参加比赛。 My code works perfectly but there is a big issue. 我的代码工作得很好但是有一个大问题。 When a user presses subscribe the page reloads but because the isset is after the echo of the button itself the page needs to be refreshed before the buttontext changes into "subscribed". 当用户按下订阅页面重新加载但由于isset在按钮本身的回显之后,页面需要在buttontext变为“subscribed”之前刷新。 Change the order gives issues as you probably can see. 更改订单会给出您可能看到的问题。 Who can help me out? 谁能帮助我? I'm out of options myself. 我自己没有选择权。 (I translated the variables etc.) (我翻译了变量等)

<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
    }
    }

... and so on for the next animals. ...等等下一个动物。

You have to use output buffering I think. 我想你必须使用输出缓冲。 Put ob_start() at the beginning of your page, now output any placeholder instead of real input code: 将ob_start()放在页面的开头,现在输出任何占位符而不是实际的输入代码:

[SUMBIT]

instead of: 代替:

<input type='submit' ....

At the end of page you get your buffered content: 在页面末尾,您将获得缓冲内容:

$content = ob_get_clean()

And replace submit button with correct code: 并使用正确的代码替换提交按钮:

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

And now output content: 现在输出内容:

echo $content;

quick fix can be javascript 快速修复可以是javascript

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

and when changing 并在改变时

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

in future I STRONGLY suggest you to use PHP MVC Framework 在将来我强烈建议您使用PHP MVC框架

You can use your post code above the page then loaded your page.... Please use like below code: 您可以使用页面上方的帖子代码然后加载您的页面....请使用如下代码:

 <?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++;
        }


        }

most easy solution move the isset up to the top. 最简单的解决方案将isset移至顶部。 just so you know actions should always be the first things you handle after that you are gonna work on the view 只是让你知道在你将要处理视图之后,行动应始终是你处理的第一件事

<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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM