user2981393 user2981393 - 4 months ago 6
HTML Question

Only run PHP code after an HTML post

Right now I have code where I display the results of a search but it's running automatically when the page starts, not just after the user enters search. How can I make it so the PHP code runs only after a user enters the submit button?

<h1>Search Results</h1> <br />
<ul style="list-style-type:disc" class="indentList">
<?php foreach ($memberName as $value) { ?>
<li><a href="http://buddybosssport.tk/members/<?php echo $value;?>/"><?php echo $value ?></a></li>
<?php } ?>
</ul>


This is the code that's displaying automatically, I only want it to run after the post.

Answer

You need to check if the form was submitted, which depends on which method you set for the form. This will either be POST or GET.

<h1>Search Results</h1> <br />
<ul style="list-style-type:disc" class="indentList">
<?php if(isset($_POST['submitbutton'])) { foreach ($memberName as $value) { ?>
<li><a href="http://buddybosssport.tk/members/<?php echo $value;?>/"><?php echo $value ?></a></li>
<?php } }?>
</ul>