Is it possible to link to a modal and execute a function simultaneously within the Bootstrap framework?

I have created an HTML form that looks like this:

<form class="well span9 offset1" action="/summary/" method="post">
    {% csrf_token %}
        <fieldset>
        <label class="control-label" for ="inputIcon">Type or paste your text into box:</label>
        </fieldset>
        <div class="controls">
            <div class="input-prepend">
                <textarea rows="15" class="span9"></textarea> 

            </div>
            </div>


        <fieldset>
        <input type="checkbox" name="input_text" />
        Send me the copy of summary by e-mail.

        </fieldset><br />
        <a href="#summaryModal" data-toggle="modal"><button type="submit" class="btn btn-primary offset3">Summarize</button></a>
    <button type="reset" class="btn btn-danger">Reset</button>
</form>

When a user submits the form, I want it to process in the summary function and return the result to a function in a Bootstrap modal. However, instead of going to the function, the form just opens the modal popup. I am trying to return to the same template from the function with the result. Do I need to use Ajax for this?

Answer №1

If you have your JavaScript functions in place, you'll need to use AJAX for this to function correctly. Follow these steps:

  1. Ensure that your button's syntax resembles:

    <button type="button" class="btn btn-primary offset3"
    onClick="summaryFunction()">Summarize</button>

  2. When the button is clicked, trigger a JavaScript function (let's call it summaryFunction()).

  3. The function should send the form details to a PHP page that returns data in JSON format. (Refer to this for assistance)

  4. Use $('#divId').innerHTML to populate the modal window's content with the data returned by the PHP page.

  5. Show the modal window using $('#divId').modal();

I hope this explanation is helpful.

For example: Let the button execute a JavaScript function named summaryFunction() using the above syntax. The JavaScript function will appear like this:

<script>
function summaryFunction(){
$text = $('#textAreaId').val();
$.post("test.php", { "postedText": text },
function(data){
$('#modalInnerDiv').innerHTML = data.response;
$('#summaryModal').modal();
}, "json");
}
</script>

Remember to assign an ID to your textarea element. Replace test.php with your own PHP file. It should be expecting a posted value named 'postedText'. Organize your response into an array with key "response". For instance,

$reply = array("response" => $response)
, then convert it into JSON using json_encode(). Your PHP file should echo this JSON value.

Have an empty div within your modal with the ID #modalInnerDiv. The content of this div will be filled with the response from the PHP page and displayed within a modal window.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to adjust the height of the header image within a Bootstrap layout?

I'm attempting to create a full-width header image that occupies about 25% of the page's height for my website. I am relatively new to using bootstrap and I am facing issues changing the height of the image/container. I have tried adding the "h-2 ...

Node.js issue: Selected options not being inserted into database with multiple select

Is there a way to submit multiple values from a select form in HTML? Currently, when I click submit, none of the selected values are posted. Here is the basic HTML form I am using: <form id="form" action="" method="post"> <select id="har ...

When receiving a GET response after a server crash

Question: I am sending a Get request through Ajax every second and waiting for a response from the server. However, if my application crashes and I keep sending requests every second, I only receive a server timeout error (HTTP status code 502) with no oth ...

Using Selenium to locate an element by CSS and submitting a form

I am new to using selenium and I have a few questions about my first script. driver.findElement({ css: '.input[name=login]' }).sendKeys('login'); //driver.sleep(0); driver.findElement({ css: '.input.passwd' }).sendKeys(' ...

Showing a temporary marker while utilizing jQuery UI sortable container

Incorporating a sortable list of items using jQuery UI in a bootstrap layout has been successfully declared. $("#ReportContainer").sortable({ helper: 'clone', revert: 'invalid', }); Each draggable item represents a column size ra ...

Error occurs when utilizing jQuery ajax on Samsung devices

Encountering a puzzling issue with jQuery (3.3) ajax. Even after simplifying the code to its bare minimum, the reason behind this problem remains unclear. Using an HTML form to capture 'Submit' and trigger a PHP script via ajax. The ajax call is ...

problem involving the html <pre> element

I have a request that needs to be fulfilled. Requirement: key - this is the value type - this is the type value Currently, I am utilizing the <pre> tag. However, the output appears as follows: key1: this is the value type1 : this is the ...

Learn how to customize button styles in ExtJS with the pressedCls configuration option

Is there a way to change the color when a button is pressed? I tried using the pressedCls config but it didn't work. How can I fix this issue or is there another method to set the CSS when a button is pressed? Thank you so much! Javascript: Ext.crea ...

Exploring the process of fetching and showcasing API responses through AJAX

I need to pass the value of my car to an API, which is retrieved from the HTML below. After that, I would like to send this value to the API using AJAX. Finally, I want to display all the models in the same way as I displayed the cars. $(document).r ...

the recurring pattern of media conditions in every single column

Hello, I'm currently involved in a project where I have implemented media conditions for my columns using Sass. Here is the code snippet: // Mobile media - 320px width @mixin mobile{ @media only screen and (max-width:767px){ width: 300px; } } B ...

Modify all the content within the DIV using Regex, while keeping the HTML tags intact

I am attempting to replace all the text inside a DIV, including within its children, without modifying any HTML tags. Specifically, I want to switch all instances of 'Hello' to 'Hi'. Thank you for your help. var changes = $('div ...

X/Y Client accessing via Chrome on Android device

I am looking to accurately capture the X and Y coordinates where the user taps on the screen, regardless of zoom or scroll levels. Currently, I am utilizing event.clientX and event.clientY to achieve this, which is working as intended. The code snippet loo ...

Different dimensions of the <select> element with the help of Ryan Fait's jQuery design script

New to java and struggling on my own, I am seeking advice on how to customize Ryan Fait's jQuery input styles to accommodate 2 unique types of elements. For the code reference, please visit: While the code is efficient, I am unsure how to create two ...

Experimenting with animating an element through onClick event using JavaScript

I would like to create an animated div using JavaScript that activates on click. <div class="Designs"> <p>Designs</p> <div class="Thumbnails" data-animation="animated pulse"> <a href=" ...

Create a container with three rows (divs) each taking up 33.333333% of the container's height, designed to be responsive to changes in

Seeking a method to organize 3 divs that will collectively fill the entire height of the container. Sample code: <div class="container"> <div class="item-1"></div> <div class="item-2"></div> <div class="item-3">& ...

Getting rid of all CSS properties currently set for the Mobile view

Utilizing third-party library components in my project. The library includes components that come with predefined styles for mobile devices. I am seeking to completely UPDATE these properties within my own code. When I say "update," I mean removing all th ...

Storing various duplicates of items in local storage

Looking for help with storage settings in HTML/JavaScript as I work on a mobile not taking app using Phonegap. My goal is to allow users to input a note name and content, save them into a jquery mobile list, and see them on the home screen. However, I&apos ...

I am feeling a bit unsure, how can I shield this code from SQL Injection?

After going through various resources, I'm a bit lost on how to actually implement them in my code. Can anyone provide some guidance? Once I get a hang of it in my code, I believe I'll be able to grasp it better! I came across this AJAX autocompl ...

Having issues with handling button click events in jQuery

I'm currently working with jQuery to show a div when a button is clicked, but for some reason, it's not functioning as expected... HTML: <input type="button" id="addmoresg" value="Add More" name="button"> <div id="addsg" style="display ...

Activate month input programmatically

Having an issue trying to open a month popup using jQuery trigger. I have a button and an input of type month, but when the button is clicked, the popup does not open as expected. You can find the fiddle for this question here. $(document).ready(functio ...