简体   繁体   中英

how to open bootstrap modal with different PHP value

i have a php app with 3 table, each one have multiple row with button for each row to get a form for the affected data

the idea is to give a value for the modal to have as much column as in the affected table.

until now, i tried to have one form with button fo each table, with different value, and testing it in php to use the correct table for for the column as below in the code (it's a test webpage so there is just one button, i'm just trying to open a modal manually after testing a php value ):

<body>
<form action="" method="post">
    <button type="submit" class="btn btn-primary" data-bs-toggle="modal" data-bs- 
        target="#CreateInfra" name="der">
        Launch demo modal
    </button>
</form>
<script>
    function modal(){
            $("#myModal").modal('show');
    }
</script>
<div class="bs-example">
    <!-- Modal HTML -->
    <div id="myModal" class="modal fade" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Modal Title</h5>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <p>This is a simple Bootstrap modal. Click the "Cancel button", "cross icon" or 
                    "dark gray area" to close or hide the modal.</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data 
                        dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-primary">Save</button>
                </div>
            </div>
        </div>
    </div>
</div>
    <?php
    if (isset($_POST['der'])){
        $value = 1;
        var_dump($value);
    }
    else{
        $value=2;
        var_dump($value);
    }

    if ($value == 1){
        echo "<script>modal()</script>";

    }
    ?>

</body>

the thing is, when i hit the button, i see my page loading something for maybe 0.4 second but nothing happen.

is there any way to do this or i should use something else?

You can do something like this as long as you want it to decide on the page load

<script>
    function modal(val){

        let options = [];
        var myModal = new bootstrap.Modal(document.getElementById('exampleModal'))

        if (val == 0){
            myModal.show()
        }
    }
</script>
<?php $val = 0; ?>
<?php echo "<script>modal(".$val.")</script>" ?>

Other than that you would have to do it through a fetch request or AJAX because php and JS can't directly talk to each other

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