-2

Is there a simple, other solution to trigger my boostrap toast which doesn't involve a button?

Maybe like :

button onclick ="trigger my bootstrap toast the easy way"

without the need of fetch or a button type="button"?

I think I have overlooked a simple way to implement it.

My site code looks like this currently:

<form>
  <button=type"submit" name="need the name for my json key" value="need you also for my json"

This submits to my PHP background app, to save my value to JSON strings in a file.

Is there any simpler way to implement the call to trigger bootstrap toast?

The PHP background side is not the problem, for that I just want to POST my values to my PHP.

10
  • getbootstrap.com/docs/5.3/components/toasts/#usage or getbootstrap.com/docs/5.3/components/toasts/#live-example didn't help? How exactly do you trigger a toast using fetch? Commented Jul 25, 2024 at 5:26
  • So, you want the button to submit the form AND also not result in a navigation (which a form submit does) - sounds like you want two conflicting results from the one action. Commented Jul 25, 2024 at 5:26
  • @brombeer Thats my problem my JS knowleage is not so good that i can implement fetch over javascript , and no the examples dont work in my case they all needs to work with fetch or button type="button" change to button type"submit" will never trigger the toast. Commented Jul 25, 2024 at 5:41
  • @JaromandaX Yes i want the button to submit the form, or any other simple way to give the form to my PHP background. To Navigate / stay on the same side is ok. So just want : do my Post request and popup my toast to the user. why conflicting where can i read about what you mean sorry but i donst know much about JS. thx Commented Jul 25, 2024 at 5:47
  • 1
    If so, <button type="button"/>, by itself does not submit the form, even it is inside the form, unless you trigger an event of the button (click, blur if you fancy, etc) then execute a method that will do fetch, xmlhttprequest, ajax, axios whatever term that you know, then return response based on the result of that xmlhttprequest Commented Jul 25, 2024 at 6:34

1 Answer 1

1

It sounds like you want to submit your form data asynchronously using JavaScript and then show a toast message when the async request is complete.

Toast messages can be triggered via JavaScript, as per the bootstrap documentation - there is no direct dependency on, or requirement to use, a button.

This code will listen for a form submit event, prevent the default postback behaviour, then send an async POST request via fetch() which submits the form data to the server, and finally show the toast message when the POST request completes:

HTML:

<form id="frm1">
  Enter your data: <input type="text" name="field1" />
  <button type="submit" class="btn btn-primary">Submit form</button>
</form>

<div class="toast-container position-fixed bottom-0 end-0 p-3">
  <div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-header">
      <img src="..." class="rounded me-2" alt="...">
      <strong class="me-auto">Bootstrap</strong>
      <small>Test</small>
      <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
    </div>
    <div class="toast-body">
      Form submitted
    </div>
  </div>
</div>

JavaScript:

document.addEventListener("DOMContentLoaded", function() {
  const frm = document.getElementById("frm1");
  const toastLiveExample = document.getElementById("liveToast");

  const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample);
  frm.addEventListener("submit", function(event) {
    event.preventDefault();

    fetch(
      "https://www.example.com/submit.php", 
      {
        method: "POST",
        body: new FormData(frm)
      }
    )
    .then(function(response) {
      return response.text();
    })
    .then(function(data) {
      toastBootstrap.show();
    })
    .catch(function(err) {
      console.log(`Error: ${err}`);
    });
  });
});

Live demo (with fake sink for the async request): https://jsfiddle.net/utazbhp2/3/

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @ADyson you made my Day and understood my question as it is. Perfect, sorry seen you post late.
@al-nabaproject no problem. Remember to mark the answer accepted if it was useful
but pls one more expl., take me the whole day. How would I do a <button name="show up" and value="show up" show up over fetch. By html post is no prob. thx
@al-nabaproject buttons aren't serialised as part of the form data, because those are the rules when the button didn't directly cause the request to the server. Why do you need to do that? Could you put the value into a hidden field in the form instead, or something like that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.