如何控制PHP代码的执行

时间:2016-08-22 16:41:28

标签: php

通过这个例子我回顾了我的问题: 我第一次执行这个php页面时,我想回复一下#34;我有两个按钮"。当用户点击按钮" next"或" next1"我想回声"我已经说过我有两个按钮"而不是"我必须按钮"我也用隐藏的输入文本证明了它但它不起作用。寻求帮助

<?php
if ($_SESSION['already_said'] == false ){
    echo "I have two buttons". date('h:i:s')."<br>";
}

$_SESSION['already_said']=true;

if( isset($_GET["next"]) || isset($_GET["next1"])) {
    echo "I already said I have two buttons". date('h:i:s')."<br>";
}
?>
<html>
<body>
<form name="form" method="GET" action="" >  

<button type="submit"  name="next">
<span class="label">Next</span>
</button>

<button type="submit" name="next1">
<span class="label">Next1</span>
</button>

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

当然重要的是要记住将session_start()放在使用会话的任何脚本的顶部。事实上,将它放在所有脚本中是一个好主意,即使少数人不使用会话只是为了让会话保持活跃。

<?php
session_start();

$msg =  "I have two buttons ". date('h:i:s')."<br>";

if (! isset($_SESSION['already_said']) ){
    $_SESSION['already_said'] = true;
}

if( (isset($_GET["next"]) || isset($_GET["next1"]) ) && isset($_SESSION['already_said'])) {
    $msg =  "I already said " . $msg;
}
?>
<html>
<body>
    <div> <?php echo $msg; ?></div>
<form name="form" method="GET" action="" >

<button type="submit"  name="next">
<span class="label">Next</span>
</button>

<button type="submit" name="next1">
<span class="label">Next1</span>
</button>

</form>
</body>
</html>