Displaying form after Ajax submission

I have implemented an AJAX code to submit my form, but I am facing an issue where the form disappears after submission. Here is my current code:

 <script>
 $('#reg-form').submit(function(e){

e.preventDefault(); // Prevent Default Submission

$.post('ajaxdmsgsub.php', $(this).serialize() )
.done(function(data){
  $('#form-content').fadeOut('slow', function(){
      $('#form-content').fadeIn('slow').html(data);

    });
})
.fail(function(){
 alert('Ajax Submit Failed ...');
});
});
</script>

This is my form:

  <div id="form-content">
 <form method="post" id="reg-form" autocomplete="off"   action="ajaxdmsgsform.php">

<label>Message</label>
<textarea class="txtarea" name="message"></textarea>

 <div class="cleared"></div>
<input type="submit" value="Submit Message" class="submitit" onclick="hide('myModal')">
</form>

</div>

After submitting the form, it disappears and I want it to reset so users can keep submitting without a page refresh. I'm aiming for an instant messaging functionality with this form. However, I am unsure how to prevent the form from disappearing.

Answer №1

$('#contact-section').hide('slow', function(){
    $('#contact-section textarea, #contact-section input').each(function(){
        $(this).val("");
    })
    $('#contact-section').show('slow');
});

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

Ways to expose an object in a Node module

One of my modules relies on a parsing functionality, with other modules needing to query the values provided by this parser. I have some key questions regarding its design: How should I approach building it in terms of design aspects? Which method should ...

Tips for updating a plugin from phonegap 2.5 to the most recent version 3.1, and the steps to follow when including a new plugin in phonegap

$('#sendSms').click(function(e){ alert("Sending SMS in progress");//After this alert, I encountered continuous errors and couldn't determine the root cause var smsInboxPlugin = cordova.require('cordova/plugin/smsinboxplugin'); ...

The ng-disabled directive is functioning properly, however it is not having any impact on the disabled attribute in

Having an issue with enabling or disabling a button based on the selection of a certain string from a dropdown menu. HTML <select ng-change="checkType()" ng-options="sth in sth for things"></select> <input ng-disabled="{{toggleDisable}}" ...

Is automatic spacing insertion a feature?

I am currently working on a side project to learn php, sql, and ajax. In this project, I have codes that insert specific data into a database: index.php - <textarea class="form-control txt" rows='3' name="data[Address]" id="Address" placehol ...

The code within $(document).ready() isn't fully prepared

Feeling frustrated after spending hours searching and attempting to refactor one of my old modules on a rendered Mustache template. It's like diving into code chaos. <section id="slideShow"> <script id="slideShow-template" type="text/tem ...

The Alchemy feature on hover is not functioning

I am currently using alchemy.js to display a graph, but I am encountering issues with showing the "onMouseOver" caption of the graph's node. The console is displaying some errors which you can see here. Here is the code snippet: <html> < ...

Is it advisable to generate ajax responses on the server side?

Just starting out with Django and Ajax, looking for some advice. I have a question about best practices in web design: Should a view function that is called using Ajax return data structures and let the client-side (JavaScript) handle rendering HTML pages ...

How to retrieve the index of a nested ng-repeat within another ng-repeat loop

On my page, there is an array containing nested arrays that are being displayed using ng-repeat twice. <div ng-repeat="chapter in chapters"> <div ng-repeat="page in chapter.pages"> <p>Title: {{page.title}}</p> </d ...

Generate fresh input fields with distinct identifiers using JavaScript

My challenge is to dynamically create new empty text boxes in JavaScript, each with a unique name, while retaining the text entered in the previous box. I struggled with this for a while and eventually resorted to using PHP, but this resulted in unnecessar ...

Copying a Pinia state Array without referencing it is not possible

My goal is to duplicate an array within a Pinia store so that I can use it to create a reactive object in various components for input modeling. The intention is to only update the original array if the user decides to save any changes made using the copy ...

What is the process for creating a React Component with partially applied props?

I am struggling with a function that takes a React component and partially applies its props. This approach is commonly used to provide components with themes from consumers. Essentially, it transforms <FancyComponent theme="black" text="blah"/> int ...

When combining AJAX with PHP and HTML, one limitation to note is that PHP alone cannot generate or create the file

I am facing a particular problem here. Currently, I am using HTML with AJAX: <html> <head> <script> function ajax_post(){ // Create our XMLHttpRequest object var hr = new XMLHttpRequest(); // Create some variables we need to ...

Triggering updates from multiple controls in a GridView using ASP.NET

I am faced with a challenge involving a gridview containing numerous checkboxes, sometimes over 40. My goal is to have these checkboxes trigger a .NET function in the code behind upon being clicked. The complication arises from the fact that the checkboxes ...

Is there a way to retrieve information from an external URL in JSON format and display it using JavaScript on a separate HTML webpage?

I'm trying to display the value TotalPostNotificationCount from a JSON file in an external HTML using either JavaScript or PHP. The JSON data is as follows: { "DayPostCount": 1, "TotalPostNotificationCount": 7381 } This data can be found at t ...

Angular JS effectively prevents redundant data from being displayed to users when scrolling infinitely while also efficiently removing DOM elements for previous data

I'm currently working on implementing AngularJS Infinite Scroll without using jQuery. My goal is to display the first 3 data items when the page loads and then load the next 3 data items from a JSON array object as the user scrolls. The issue I am fac ...

Developing a personalized Hook with useRef functionality

Here is a code snippet where I demonstrate creating two custom hooks in React. The first hook, `useChangeText`, utilizes the `useState` hook and works as expected. The second hook, `useGetHTML`, attempts to use `useRef` to log an HTML element to the cons ...

Incorporating a pre-existing component into a Vue.JS template through an onclick event: How can this

I'm trying to enhance my template by implementing a feature that allows me to add a component simply by clicking on a button. Let me give you a glimpse of what I have in mind: The component named Draggable.vue is the one I intend to incorporate upon c ...

Refreshing the page when the button is clicked to upload files using AJAX

I have created a form and I need to send a file along with some other information via Ajax to a PHP file. The issue I am facing is that when I attempt to upload the file, the page refreshes after clicking on submit. How can I prevent this page refresh? Ad ...

Is it possible to utilize $regex alongside $all in mongoDB?

In my current project, I am facing a challenge where I need to handle an array of strings received from the frontend. Each document in my mongoDB database also has its own array of keywords. The tricky part is that the strings sent from the frontend migh ...

Having trouble combining different styles with Material-ui and Radium?

Trying to integrate Radium with Material-ui has presented a challenge. Attempting to apply multiple styles to a single Material-ui component results in no styling being applied. For instance, the following code fails to render any styling: <MenuItem st ...