shanto
shanto

Reputation: 125

onclick modal button call a php function

When click in modal button then a PHP function called and this PHP function retrieve data from DB and show in modal.

I am trying but confused. Any Example

modal button

<?php
    foreach($pa as $pitem)
    {
        print'<div class="col span_1_of_4">
                  <div class="pcimga">
                      <div class="pcimg"><img src="Portfolio_Image/'.$pitem[6].'" class="pcimg" /></div>
                  </div>
                  <div class="pcsdes">'.$pitem[1].'</div>
                  <div class="pcctg"><span class="pcctgb">'.$pitem[5].'</span></div>
                  <div class="pclink">
                      <span class=" butnn"><a data-toggle="modal"  id="'.$pitem[0].'" href="#myModal">View</a></span>
                  </div>
              </div>';
    }
?>

here I want to show db result in modal depending on this id="'.$pitem[0].'" which carry on modal button.

My modal code:

<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">'.$pitem[1].'====='.$pitem[0].'</h4>
            </div>
            <div class="modal-body">
                <div class="row clearfix">
                    <div class="col-md-6 column">
                        <div class="row clearfix">
                            <div class="col-md-12 column">
                                <img alt="140x140" src="'.$pitem[6].'"" class="img-responsive" />                           
                            </div>
                        </div>
                        <div class="row clearfix">
                            <div class="col-md-12 column">
                                <div id="pad">                          
                                    <a href="'.$pitem[4].'" target="_blank"><button type="button" class="btn btn-info btn-lg "> View</button></a>                           
                                </div>                          
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6 column">
                        <p>'.$pitem[3].'"</p>                   
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

here I assume that db data come by this ( $pitem[6]) array using foreach loop from function.

I want to call a function like

$pai = $c -> Select_by_id($id);

Upvotes: 0

Views: 4215

Answers (3)

H&#252;seyin BABAL
H&#252;seyin BABAL

Reputation: 15550

You can load modal content with following code;

$('[data-toggle="modal"]').click(function(e) {
  e.preventDefault();
  $(".modal").modal("hide"); // Close all modals first
  var url = 'your_base_url?id=' + $(this).attr('id');
  $.get(url, function(data) {
      $(data).modal();
  });
});

Simply, I assigned this ajax content loading to all modals in page. I get id of clicked modal link, made an ajax request to your url with specific param

Upvotes: 0

Joseph Collins
Joseph Collins

Reputation: 461

You can not call a PHP function that way, you must make the request via REQUEST or AsyncrAjax if you do not want to reload the page, with code like as @Ravi Mishra

Upvotes: 0

Raviranjan Mishra
Raviranjan Mishra

Reputation: 857

You can make ajax call on button click to reach that php function.

    $('#btn-id').click(function(){
       $.ajax({
            url: 'url',
            data: { var1:value},                        
            type: 'post',
            success: function (data) {
            }

       });
    });

Upvotes: 1

Related Questions