最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open modal when a button is clicked - Stack Overflow

programmeradmin1浏览0评论

In the ments section on a blog I'm coding I want the admin to be able to delete a ment.

I have a button next to each ment and I want a modal box to be opened when the delete button is clicked.

In this modal box I want to ask again if the ment should be deleted or not, if yes then it should lead me back to the post and the ment should be deleted.

Do I need javascript to do this? Unfortunately I'm new to php and I wondered if it's possible to only do it with html & css.

I have a PostsController.php in which the site is rendered (action page) and I have the CommentsRepository.php in which the connection to the db table ments takes place.

This is the part of the ment section:

<div class="ments-list">
 <?php foreach ($ments AS $ment): ?>
  <div class="one-ment">
   <p><?php echo e($ment->content); ?></p>
   <div class="one-ment-edit">
    <input type="button" value="x" name="delete" class="delete-ment" />
   </div>
  </div>
 <?php endforeach; ?>
</div>

I know how to do the query, but I don't know how to open the modal box when the button is clicked.

I would really appreciate if someone can help me, especially if I need javascript to do this.

In the ments section on a blog I'm coding I want the admin to be able to delete a ment.

I have a button next to each ment and I want a modal box to be opened when the delete button is clicked.

In this modal box I want to ask again if the ment should be deleted or not, if yes then it should lead me back to the post and the ment should be deleted.

Do I need javascript to do this? Unfortunately I'm new to php and I wondered if it's possible to only do it with html & css.

I have a PostsController.php in which the site is rendered (action page) and I have the CommentsRepository.php in which the connection to the db table ments takes place.

This is the part of the ment section:

<div class="ments-list">
 <?php foreach ($ments AS $ment): ?>
  <div class="one-ment">
   <p><?php echo e($ment->content); ?></p>
   <div class="one-ment-edit">
    <input type="button" value="x" name="delete" class="delete-ment" />
   </div>
  </div>
 <?php endforeach; ?>
</div>

I know how to do the query, but I don't know how to open the modal box when the button is clicked.

I would really appreciate if someone can help me, especially if I need javascript to do this.

Share Improve this question asked Jan 10, 2019 at 10:03 ofmiceandmoonofmiceandmoon 6062 gold badges14 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You don't need javascript, but it sure makes the thing easier!

I've found some examples of pop-up that are opened without any javascript. I find this one intersting:

  • Pure CSS popup/modal box without javascript

To do so, they create a pop-up with a visibility: hidden; css attribute, and they use the target pseudo-class to set this attribute to visibility: visible;

To trigger the target pseudo-class, they create a link (that acts like a button) that references the ID of your pop-up.

The link will target your pop-up, triggering the target pseudo-class, and showing your pop-up!

There is also the Bootstrap option, as remended by Jeppe Spanggaard, which works fine!

Hope it helped in any way!

You can use javascript for this: https://www.w3schools./howto/howto_css_modals.asp

But if you use Bootstrap there is an easier way: https://www.w3schools./bootstrap/tryit.asp?filename=trybs_modal&stacked=h

You can use SweetAlert for your cause, it is more elegant and easy to use.

https://sweetalert2.github.io/

here's working example: Jsfiddle

Simply on Delete Button click Apply following Code

Swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  if (result.value) {
    Swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    )
  }
})

Here's how it will look,

EDIT

Based on your ment I think this might be helpfull JsFiddle

You can customize the popup like this Example

发布评论

评论列表(0)

  1. 暂无评论