JavaScript popup confirmation in an MVC3 AJAX form

I have implemented an ajax form in a MVC3 project and I am looking for a way to incorporate a JavaScript confirm popup upon clicking the submit button. While I have managed to get the popup to display, I am struggling to prevent the form submission if the user selects the cancel option.

Can anyone suggest the most effective approach to achieve this?

I have experimented with the following JavaScript code...

       function showWarning(e) {
           var answer = confirm('message');
           if (answer) {
               return true;
           }
           return false;
       }

However, due to the asynchronous nature of ajax, this method is not functioning as desired.

Instead of coding the ajax call myself, I am utilizing the ajax.begin form helper as shown below...

using (Ajax.BeginForm("LinkName", "Account", null, new AjaxOptions { UpdateTargetId = "divLinkName", HttpMethod = "POST" }, new {  @id="formLinkName" }))
    {
    <div class="row">
        <div class="large-12 columns">
            <div class="editor-label">
                @Html.LabelFor(m => m.LinkName)
                @Html.TextBoxFor(m => m.LinkName)
                @Html.ValidationMessageFor(m => m.LinkName)
                <input type="submit" value="Set my Link Name" class="button radius small" />
            </div>
        </div>
    </div>
    }

If you have any insights or suggestions on how to address this issue, please share them. Thank you.

Answer №1

Here's a solution that has been successful for me, particularly when using an Ajax.BeginForm with new AjaxOptions:

<input type="submit" onclick="if (!confirm('Are you sure?')) return false;" />

Answer №2

Attach a function to the onSubmit event of a form.

using (Ajax.BeginForm("LinkName", "Account", null, new AjaxOptions { UpdateTargetId = "divLinkName", HttpMethod = "POST" }, new { @id="formLinkName", onsubmit="return takeconfirmation()" }))

Create a JavaScript function called takeconfirmation:

function takeconfirmation(){
return confirm('Are you absolutely certain?');
}

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

Can the optionsText be shown while saving the optionsValue in a dropdown menu?

Here is the code snippet I am currently working with: var model = function() { this.nameSomething = ko.observable(''); this.nameId = ko.observable(0); }; var vm = (function() { var myList = [{ id: 1, type: 'foo1'}, { id: 2, ...

Having trouble with Isotope masonry functionality

While experimenting with Isotope from , I tested the reLayout method using the provided example found at . I copied the css and js to my page () but encountered an issue where clicking on the first element caused all other elements to move to the first col ...

How can I effectively handle extensive data parsing from a file using JavaScript?

Looking to optimize data parsing in JavaScript for a large file? I'm currently using JSON parse for a 250MB file, but it's too slow. Is there a faster method to extract a high volume of data without iterating through every character? The file con ...

Tips for creating a dynamic curved SVG path

I'm looking to draw a black border only along the inside of this SVG. I've tried adding stroke and setting the stroke-width, but that applies the border to the entire SVG. Is there a way to limit the stroke to a certain point within the SVG? d ...

Creating dynamic rows for Firebase data in React BootstrapTable can be accomplished by dynamically rendering each row

Hey everyone, I'm currently working on a web app project where I am creating a table. Below is the code snippet for it: class Table1 extends Component { render() { return ( <div> <BootstrapTable data={this.props.data}> ...

Using Jquery along with the Foreach function

Currently, I am working on a form that allows clients to edit their keywords/tags. Once the keywords are submitted, they are stored in the database and retrieved using foreach and explode. Instead of displaying the values like "one,two,three," they are s ...

Disable other actions during jquery change event validation

Is there an answer for this question already? If so, I apologize! Here is my situation: I have an edit field that is validated when the .change() event occurs. What I am trying to achieve is this: if a user types something and then immediately clicks the s ...

Responsive Design: Concealing a Sidebar with CSS

Seeking assistance with optimizing the layout of my app, . It currently displays a menu on the left and a login form in the center, with a graph shown to users upon logging in. Is there a way to configure it so that when accessed on iOS: 1) the left menu ...

The feature for automatically hiding other dropdowns when one is open is not functioning properly

Trying to finalize a mega menu that almost works. I need help with closing all other open mega menus when a user clicks on one. Check out the JavaScript code snippet I've written below. The mega menu currently toggles correctly, but it doesn't h ...

Changing the colors of multiple buttons in a React Redux form: a step-by-step guide

When using Redux Form Wizard on the second page, I have two buttons that ask for the user's gender - Male or Female. The goal is to make it so that when a user clicks on either button, only that specific button will turn orange from black text. You ...

Utilizing React.hydrate in conjunction with Vue: A Beginner's Guide

Wondering about a unique scenario here - I have a website built with Vue and now I aim to showcase a library I developed in React. In order to steer clear of server-side rendering (SSR), I can simply wrap ReactDOM.hydrate(ReactApp, document.getElementById( ...

jquery add to table id, not to a table within

Having trouble adding a table row to a specific table ID without it appending to other tables with different IDs nested inside. I've searched for a solution but can't seem to find one. Can someone help me figure out what I'm doing wrong? Her ...

Issue with incorporating Material UI HtmlTooltip within Material UI Select MenuItem and handling synthetic events in the select onChange method

---OBJECTIVE--- The goal is to integrate a Material UI select component with MenuItems wrapped in HtmlTooltips, providing hover information for each option in the list. For now, I'm keeping it simple as a proof of concept and plan to implement dynam ...

What is the best way to arrange an array of objects based on a specific attribute?

Ordering object based on value: test": [{ "order_number": 3, }, { "order_number": 1, }] Is there a way to arrange this so that the object with order_number 1 appears first in the array? ...

Is it feasible to verify for vacant dates with a single click?

Is there a way to determine if a date value is empty, and if it is, display a popup indicating so? After some research, I stumbled upon a similar issue where the date value was always filled with a default "mm/dd/yyyy" value. The solution provided involv ...

How can I set up multiselect values in AngularJS ui-select?

Solution: Success! I managed to resolve the issue by implementing this solution and creating a customized build of the ui-select. Hopefully, this fix will be incorporated into the official master branch soon! Issue Is there a way to set up a select elem ...

How to manipulate iframe elements using jQuery or JavaScript

Hey there, I have a simple task at hand: I have a webpage running in an iFrame (located in the same folder on my local machine - no need to run it from a server) I am looking to use JavaScript to access elements within this iFrame from the main page. How ...

Getting the input from an HTML editor and inserting it into a textarea using JavaScript

Currently, I am in the process of developing an HTML editor for a project. I have downloaded a program online that I am attempting to customize according to my requirements. However, I am encountering difficulties when trying to retrieve the inner HTML of ...

Tips for displaying "No Results" without causing any lag in the browser

I'm encountering difficulty trying to implement a simple feature without resorting to a "messy" solution. The performance is suffering, and I am certain it's not done in a "professional" manner. What I desire is straightforward – displaying a ...

How can I align the Socket.io path on the client with the directory structure of my Node.js server?

Currently, I am utilizing socket.io with node.js along with Expressjs. As I serve my HTML page, I have the socket.io.js file linked directly in a script tag: <script src="/socket.io/socket.io.js"></script> I'm facing some difficulty match ...