简体   繁体   中英

foreach and input type submit in php?

My problem is the buttons don't work, the only thing that happens is that it reloads the page. And is there anyone that know how to make new buttons instant after I create them in my database?

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" class="index_head" id = "login_form" >
<?php 
        header('Cache-Control: no-cache');
        header('Pragma: no-cache');
        require "gettops.php";
        $tops = getTops();
        if(!empty($tops))
        foreach ($tops as $top) {

        if(isset($_POST[$top[0]]) && !empty($_POST[$top[0]]))
        {
            header("location:forgot_checkin.php");
        }

        ?>  
            <input type="submit" Name="print" value="<?php echo $top[0]; ?>" class="btn btn-large btn-primary" />

     <?php } //end foreach ?>   

<input type="submit" Name="print" value="Download Excel" class="btn btn-large btn-primary">
<input type="submit" Name="print" value="Download Excel" class="btn btn-large btn-primary">
</form>

Your form is not submitting anywhere: $_SERVER['PHP_SELF'] is equivalent to "#" or "" . You should include the place the form should submit to and avoid $_SERVER['PHP_SELF'] . Most of these SERVER_VARS are depreciated anyway.

I'm not entirely sure on your question vs. the code that is present for creating new buttons, but you can do:

<? foreach ($query as $row) { ?>
    <a href="<? echo $row['link']; ?>"><input type="button" Name="print" value="<?php echo $row['button']; ?>" class="btn btn-large btn-primary" /></a>
<? } ?>

You can of course make the button line an echo and never leave php mode. But are there multiple buttons just redirecting? Or do they submit the form to multiple places? If they just link you don't even need a form an can append <a href="#"></a> around your button or append onclick="#" to the button tag (or nicer: using jQuery('').click({});.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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