My method for updating form input properties involves switching the disable attribute from "false" to "true" and vice versa

I have a form that uses Ajax to submit data. Once the user submits the form, the text is updated to indicate that the data was sent successfully, and then the form is displayed with the fields filled out. I want to display the form but prevent users from re-submitting it, so I need to disable the input fields and submit button. The form has three operations: add, update, and view. After disabling the form inputs in view mode, how can I keep them disabled in add mode as well?

<div id="userModal" class="modal fade">
  <div class="modal-dialog">
    <form method="post" id="user_form" enctype="multipart/form-data">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Add Report</h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-xs-2">
              <label for="ex1">Square</label>
              <input type="text" name="square" id="square" class="form-control">
            </div>
            <div class="col-xs-2">
              <label for="ex1"> Number </label>
              <input type="text" name="report_number" id="report_number" class="form-control">
            </div>
            <div class="col-xs-4">
              <label>Report Date </label>
              <input type="date" name="report_date" id="report_date" class="form-control" />
            </div>
          </div>

          <br>

          <div class="row">
            <div class="col-xs-12">
              <label>Notes </label>
              <textarea rows="15" cols="80" type="text" name="notes" id="notes" class="form-control" > </textarea>
            </div>
          </div>

          <br>
          <label>Select Image</label>
          <input type="file" name="user_image" id="user_image" />
          <span id="user_uploaded_image"></span>
        </div>
        <div class="modal-footer">
          <input type="hidden" name="report_id" id="report_id" />
          <input type="hidden" name="operation" id="operation" />
          <input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </form>
  </div>
</div>


<script>

 $(document).on('submit', '#user_form', function(event){
  event.preventDefault();

  var addedSquare = $('#square').val();
  var addedNumber = $('#report_number').val();
  var addedDate = $('#report_date').val();
  var extension = $('#user_image').val().split('.').pop().toLowerCase();
  if(extension != '')
  {
   if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
   {
    alert("Invalid Image File");
    $('#user_image').val('');
    return false;
   }
  } 
  if(addedSquare != '' && addedNumber != '')
  {
   $.ajax({
    url:"insert.php",
    method:'POST',
    data:new FormData(this),
    contentType:false,
    processData:false,

success:function(data)
    {

     alert(data);
     $('#user_form')[0].reset();
     $('#userModal').modal('hide');

     dataTable.ajax.reload();
    }
   });
  }
  else
  {
   alert("Both Fields are Required");
  }
 });

 $(document).on('click', '.update', function(){
  var report_id = $(this).attr("id");
  $.ajax({
   url:"fetch_single.php",
   method:"POST",
   data:{report_id:report_id},
   dataType:"json",
   success:function(data)
   {
    $('#userModal').modal('show');
    $('#square').val(data.square).prop( "disabled", false );
    $('#report_number').val(data.report_number).prop( "disabled", false );
    $('#report_date').val(data.report_date).prop( "disabled", false );
    $('.modal-title').text("Edit Report");
    $('#report_id').val(report_id);
    $('#user_uploaded_image').html(data.user_image);
    $('#action').val("Edit");
    $('#operation').val("Edit");
   }
  })
 });

 $(document).on('click', '.view', function(){
  var report_id = $(this).attr("id");
  $.ajax({
   url:"fetch_single.php",
   method:"POST",
   data:{report_id:report_id},
   dataType:"json",

success:function(data)
    {
    $('#userModal').modal('show');
    $('#square').val(data.square).prop( "disabled", true );
    $('#report_number').val(data.report_number).prop( "disabled", true );
    $('#report_date').val(data.report_date).prop( "disabled", true );
    $('.modal-title').text("View Report");
    $('#report_id').val(report_id);
    $('#user_uploaded_image').html(data.user_image);
    $('#action').val("View");
    $('#operation').val("View");
   }
  })
 });


</script>

Is there a way to enable the inputs again in the add operation?

Answer №1

simply use the .prop( "disabled" , false );

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

Guide to uploading multiple images to an Amazon AWS S3 bucket using Node.js, Angular, and JavaScript

Can anyone share insights on where and how E-commerce websites typically upload their product images? Has anyone had experience using Amazon AWS S3 bucket for this purpose? Additionally, does anyone know how to upload or retrieve multiple images at once ...

Update a user's role through an ajax button within a user list

In my webpage, I have a customer list being displayed using a loop <?php foreach ($customers as $user){ ... }; ?> Next to each user, there is a "Change role" link that appears in a loop I've implemented an A ...

In angular.js, repeating elements must be unique and duplicates are not permitted

My view controller includes this code snippet for fetching data from an API server: $scope.recent_news_posts = localStorageService.get('recent_news_posts') || []; $http({method: 'GET', url: 'http://myapi.com/posts'} ...

Model in Sequelize does not get updated

I have a basic user model with two simple relationships: export const Password = sequelize.define("password", { hash: { type: DataTypes.STRING, allowNull: false, }, salt: { type: DataTypes.STRING, allow ...

What is the best way to verify the application's authenticity when making AJAX requests?

I have the below Code written in AngularJs, but I need to ensure that the authenticated app is sending requests to the backend. How can I achieve this? resp.get Update = function () { //return $http.get(urlBase); return $http({ ...

"Enhance Your WordPress Website with the Power of jQuery

I have been developing a unique Wordpress plugin that incorporates a grid loading effect using jQuery. The script I have implemented is as follows: <script type="text/javascript"> new GridScrollFx( document.getElementById( 'grid' ), { ...

Prevent onClick event in jQuery and extract parameters from a function call

We've been tasked with implementing a temporary solution to some code, so it might seem a bit silly at first. But please bear with us. The goal is to block the onclick method of an anchor tag, extract the parameters from the function call, and then u ...

Troubleshooting: Why Won't My Basic JQuery POST Request Work?

Can someone help me figure out how to use JQuery to send a POST request and display the data in a PHP file? Here is the HTML file: <html> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> ...

Is there a way to filter and tally the JSON objects that have latitude and longitude within a 10km radius from the

I'm currently working on filtering and counting objects from an API endpoint that fall within a 10km radius of the specified origin. My main challenge lies in correctly filtering the API results and tallying the number of matching items. While I succ ...

Ending a connection to MS SQL server in node.js with mssql library

In my journey to write a node.js script for querying an MSSQL database, I find myself navigating the realm of JavaScript, node.js, and VSCode with limited SQL knowledge. While I have managed to piece together working code, the issue lies in the connection ...

Obtaining the scene's coordinates in Three.js

Struggling with a coding issue, I've scanned various discussions that don't quite address my specific problem. Any assistance would be greatly appreciated. In my HTML document, I am using the three.js library to create a scene titled scaledScene ...

Using an AJAX call in JSF to trigger a popup window

I have the following code in a .xhtml file. I am looking to incorporate an ajax call into one of the input textbox components. Specifically, when the inputtextbox with id="search_dias" loses focus, I want to trigger an ajax call that will display the res ...

Tips for creating AngularJS forms which display radio buttons and populate data from a JSON file

I'm having trouble displaying the json data correctly. How can I show the radio buttons from my json file (plunker demo)? Additionally, I want to validate the form when it is submitted. Here is the HTML code: <my-form ng-app="CreateApp" ng- ...

Utilize React MaterialUI's ExpansionPanelSummary by incorporating a counter to a specific div id

I am in need of a feature that will automatically increment a counter in the div id every time the render function is invoked. The main goal is to ensure that each div has a unique identifier. Below is the current render function implementation - render ...

Refresh the angular form after submitting data

I am currently working on an angular form to input data into a database. My framework of choice is angular-fullstack by yeoman. After successfully submitting the data, I encounter an issue where clearing the form values doesn't allow me to add new en ...

What is the best way to set the sidebar height to 100% in Material-UI?

Trying to learn MUI and encountered a problem. I want the sidebar to cover the full height, I tried 100vh but it stops increasing when the table extends. Using 100% only covers the end of the list. Is there another way to solve this, or is it fine becaus ...

A common inquiry: how can one pinpoint a location within an irregular figure?

I have been studying Html5 Canvas for a few weeks now, and the challenge mentioned above has puzzled me for quite some time. An irregular shape could be a circle, rectangle, ellipse, polygon, or a path constructed by various lines and bezier curves... I ...

Tips for incorporating side effects into an observable property?

I am relatively new to Mobx and I need to automatically call a function when this observable array is updated. The observable array: @observable Todos = [] I have many functions to manage this array (addTodo, removeTodo, ...) and I would like to avoid ...

Updating a document on Firestore based on a query is a straightforward process that involves first identifying

I'm currently working on a web form page that needs to update input fields into a specific firestore document based on certain conditions. Can anyone provide guidance on how this can be achieved? The initial part where I retrieve the query results se ...

Is it possible to identify when someone is copying a URL in a web browser?

During a recent conversation with a taxi driver, I mentioned that I work as a programmer. He then proceeded to tell me about an unusual experience he had a few days ago. While trying to copy the URL from his browser's address bar, a message box popped ...