Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

javascript - 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 ?

question from:https://stackoverflow.com/questions/66061253/how-to-open-bootstrap-modal-with-different-php-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...