Isset php在表单的同一页面只显示php代码

Isset php在表单的同一页面只显示php代码

问题描述:

I have this code

<html>
 <body>
 <table cellpadding="5" cellspacing="1" >
<tbody class="ui-widget"><form method="post" action="xx.php">
<tr>
<td><strong>Name</strong><br /> 
<input name="name">
</td>
</tr>
 <button name='button'>Success Button</button>
 </form></div>


<?php

if(isset($_POST['button'])) 
{ echo "BYE BYE";}
?>

Now the question is: if a click on button I'll obtain the form and after the word "BYE BYE"

How can I do if I want have only "BYE BYE" but in the same page?

There's a way for don't show another time the HTML code?

You need to place your PHP before your HTML and make a if/else :

<?php
if(isset($_POST['button'])) {echo "BYE BYE";}
else {
?>
<html>
 <body>
 <table cellpadding="5" cellspacing="1" >
<tbody class="ui-widget">
<form method="post" action="xx.php">
<tr>
<td><strong>Name</strong><br /> 
<input name="name">
</td>
</tr>
 <button name='button'>Success Button</button>
 </form>
</div>
<?php } ?>

You are outputting HTML before the PHP code is executed. It is not affected by the if-statement at the moment. To fix your specific issue, move the if-statement to the top and put the HTML-code in the else statement. Something like this should work:

<?php
if (isset($_POST['button'])) {
    echo 'BYE BYE';
} else {
?>
<html>
 <body>
 <table cellpadding="5" cellspacing="1" >
<tbody class="ui-widget"><form method="post" action="xx.php">
<tr>
<td><strong>Name</strong><br /> 
<input name="name">
</td>
</tr>
 <button name='button'>Success Button</button>
 </form></div>
<?php
}

Note that checking for a POST-request is more reliable by checking the $_SERVER its REQUEST_METHOD index.