简体   繁体   中英

How to pass id to a modal within same page using GET method?

I have a following link:

<a type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#product_view" href="?id='.$row['upcoming_event_id'].'">
                                    <i class="fa fa-search"></i> Read more</a>

The read more looks like this:

在此处输入图片说明

When I press the read more button, a modal will pop up like this:

在此处输入图片说明

I tried the following code but it's not working:

<div class="modal fade product_view" id="product_view">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <a href="#" data-dismiss="modal" class="class pull-right"><span
                            class="glyphicon glyphicon-remove"></span></a>
                <h3 class="modal-title">I want to show the event id here <?php echo $_GET['id']; ?></h3>
            </div>

            </div>
        </div>
    </div>
</div>
</div>

How to solve this? Or is there any better way to do this? I have to show the id into the modal, not on another page. Any help will be appreciated. Thanks in advance.

On your button add a data attribute containing your id, like this :

<a type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#product_view" data-id="'.$row['upcoming_event_id'].'" href="?id='.$row['upcoming_event_id'].'">
                                    <i class="fa fa-search"></i> Read more</a>

Then in JS you need no write an event listener function automatically called when your modal is open. for your need this function seems like :

$('#product_view').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) 
  var id = button.data('id') 

  var modal = $(this)
  modal.find('.modal-title').text('I want to show the event id here ' + id)
})

See official documentation here

What you can do is to use html data attribute on your tag. let's say :

<a href="#openModal_edit" data-idValue="<?= $row['upcoming_event_id'] ?>" ...</a>

Then you need to create onclick event handler, maybe on your modal button to extract your data attribute :

onclick="myId=this.dataset.idValue;document.querySelector('#THE_HTMLID_THAT_YOUWANT_DISPLAY_IDVALUE').value = myId;return true;"

and make sure in querySelector you add the element id that you want show the value on it.

PHP code execute first, so you can't set PHP value for $_GET dynamically. Use a javascript function.

function setEventId(event_id){
    document.querySelector("#event_id").innerHTML = event_id;
}

Call setEventId() function from anchor tag

<a type="button" onclick="setEventId(<?= $row['upcoming_event_id'] ?>)" class="btn btn-primary" data-toggle="modal"
                                    data-target="#product_view" href="javascript:void(0)">
                                    <i class="fa fa-search"></i> Read more</a>

And use <span id="event_id"></span> to replace html value with event_id

<div class="modal fade product_view" id="product_view">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <a href="#" data-dismiss="modal" class="class pull-right"><span
                            class="glyphicon glyphicon-remove"></span></a>
                <h3 class="modal-title">I want to show the event id here <span id="event_id"></span></h3>
            </div>

            </div>
        </div>
    </div>
</div>
</div>

Here is demo example after getting value of $row['upcoming_event_id'] or page load.

 function setEventId(event_id){ document.querySelector("#event_id").innerHTML = event_id; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <a type="button" onclick="setEventId(1)" class="btn btn-primary" data-toggle="modal" data-target="#product_view" href="javascript:void(0)"> <i class="fa fa-search"></i> Read more</a> <a type="button" onclick="setEventId(2)" class="btn btn-primary" data-toggle="modal" data-target="#product_view" href="javascript:void(0)"> <i class="fa fa-search"></i> Read more</a> <a type="button" onclick="setEventId(3)" class="btn btn-primary" data-toggle="modal" data-target="#product_view" href="javascript:void(0)"> <i class="fa fa-search"></i> Read more</a> <div class="modal fade product_view" id="product_view"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <a href="#" data-dismiss="modal" class="class pull-right"><span class="glyphicon glyphicon-remove"></span></a> <h3 class="modal-title">I want to show the event id here <span id="event_id"></span></h3> </div> </div> </div> </div> 

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