In order to leave a comment, I need to click on the button two times

Whenever I click on a button within a form, the comment should appear alongside the email and a small avatar. However, I'm facing an issue where I have to click the button twice for it to work as expected. This problem is rooted in some complications with the options request. To mitigate this, I'm utilizing a CORS plugin to run the application since the JSON file resides on a different domain. At present, the request's type is POST, yet it necessitates two clicks to function properly.

Below is the snippet of code from the controller. The corresponding view houses an input type=submit element with ng-click embedded within.

$scope.envoyer_comment = function() {

    $scope.c = $scope.commentaire;
    $scope.e = $scope.email;

    var comment = {
      article_id: $scope.article.Article.id,
      titre: $scope.article.Article.titre,
      content: $scope.article.Article.content,
      commentaire: $scope.commentaire,
      email: $scope.email
    }

    if ($scope.flag == 0) {
      $.ajax({
        type: "POST",
        url: "https://www.yabiladi.com/newsapi/post.json",
        data: comment,
        datatype: "json",
        success: function(res) {
          $scope.d = 0;

        },
        error: function() {
          console.log("Erreur");
        }


      })
    }

The segment in the view that employs the aforementioned function:

<div ng-if="flag==0">
  
  <label for="field2"><span style="margin-left:18px;">Commentaire:</span><textarea ng-trim="false" ng-model="$parent.commentaire" class="textarea-field outer" ng-maxlength="3000" required>Hello</textarea></label>
  <span style="margin-right:130px;">{{3000 - text.length}} caractères restants</span>
  
  <br/>
  
  <label for="field3"><span style="margin-left:18px;">email:</span><input type="text" class="input-field outer" ng-model="$parent.email" style="width:50%" required></label>
  
  <br/>
  
  <label><input type="checkbox" class="input-checkbox" name="checkbox1" style="margin-left:18px;"> 

Je souhaite recevoir un email à chaque commentaire<br/>&nbsp &nbsp &nbsp sur cet article
</label>
  <br/>
  
  <label><span>&nbsp</span><input type="submit" data-ng-click="envoyer_comment()" value="Envoyer" style="margin-left:-20%" ng-disabled="commentaire_article.$invalid"/></label>
  
  <br/>
  <br/>
  <br/>
  <div ng-hide="d">
    <ion-item style="border:0px;font-size:10px;margin-left:-50px;margin-top:-30px" class="item-text-wrap">
      <div class="form-style-2-heading" style="background-color: #fbf8f7;color:black;height:47px;margin-bottom:6px;margin-top:6px">
        <div style="margin-left:-240px;padding-top:5px"><img ng-src="img/sans titre.png" height="41" width="35" style="margin-left:-85px;margin-bottom:12px" style="border:0px">
          <div style="margin-left:45px;margin-top:-50px">{{e}}</div><br/>
          <div style="margin-left:90px;margin-top:-10px"> Date:{{timeNow|date:"d/m/y à HH:mm":"UTC"}}</div>
        </div>
      </div>
      <div style="padding-right: 50px;margin-bottom:30px" align="left">{{c}}</div>
    </ion-item>
  </div>
  <ion-list ng-repeat="article2 in article1.comments">



    <ion-item style="border:0px;font-size:10px;margin-left:-50px;margin-top:-30px" class="item-text-wrap">
      <div class="form-style-2-heading" style="background-color: #fbf8f7;color:black;height:47px;margin-bottom:6px;margin-top:6px">
        <div style="margin-left:-240px;padding-top:5px"><img ng-src="{{article2.avatar}}" height="41" width="35" style="margin-left:-85px;margin-bottom:12px" style="border:0px">
          <div style="margin-left:45px;margin-top:-50px">{{article2.PhorumMessage.author}}</div><br/>
          <div style="margin-left:90px;margin-top:-10px"> Date:{{article2.PhorumMessage.datestamp * 1000|date:"d/m/y à HH:mm":"UTC+0100"}}</div>
        </div>
      </div>
      <div style="padding-right: 50px;margin-bottom:30px" align="left">{{article2.PhorumMessage.body}}</div>
    </ion-item>
  </ion-list>

</div>

The variable flag is responsible for language selection while d indicates the display status of new comments.

Screenshot captured from dev tools: ss dev tools

Answer №1

Can you provide further information regarding the html code?

In this scenario, I am presuming that there are no modifications being made to the value of the "$scope.flag" variable and that you have validated that there are no errors during the initial execution.

If possible, please share the code for the webpage.

Answer №2

To implement, do the following:

<button type="button" onclick="executeFunction()">

<script>
    function executeFunction (){
        document.forms[0].submit();
    }
</script>

Answer №3

After experimenting with various options, I finally found the solution. By including async:false in the request body, I was able to get it to work smoothly. Big thanks to all those who offered their assistance!

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

Error in jQuery: Null property causing TypeError when reading 'text'

Issue: I have a modal form that is loaded via an ajax call. Inside the modal, there is a span element containing an email address. I am trying to capture the value of this span element using JavaScript. Currently, I have a button click event that triggers ...

What's the best way to navigate to a different page using a function in React JS?

Hello there! I'm just starting out with React js and I've been trying to figure out how to navigate to the home page once a user successfully logs in using React. Below is the function I currently have set up, which allows me to redirect to the h ...

Creating a powerful JavaScript/jQuery function that effectively adds a thousands comma separator for large numbers (10^6 and above) whenever a key is pressed up

After researching various solutions for formatting user input strings to include comma separators when typing large numbers such as 9999999999 --> 9,999,999,999, I came across multiple discussions here on stack overflow: How to print a number with comm ...

Converting a one-dimensional array into a two-dimensional array in JavaScript explained

Below is the code snippet const arrayColumn = (arr, n) => arr.map(x => x[n]); const pcorr = (x, y) => { let sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0, sumY2 = 0; const minLength = x.length = y.length = Math.min(x.length, y.le ...

Issues arising from using THREE.js canvasTexture

Describing my current issue. I have generated a Terrain geometry using a DEM. I am now downloading OSM Tiles to use as textures for this geometry. Every time a new Tile is downloaded, the callback function processMapImage is triggered: function processMa ...

Moving from ngRoute to ui-router in AngularJS is causing a [$injector:unpr] error to be thrown

After switching the routing in my app from ngRoute to ui-router, I encountered two errors: SyntaxError: Unexpected string Uncaught Error: [$injector:unpr] Here is the updated config.route.js: (function () { var app = angular.module('app'); ...

One array comprises of all elements found in a separate array

After grappling with this problem for a while, I still haven't been able to find a solution. If I have 2 arrays structured like this: array1 = [ { name: 'John', age : 25}, { name: 'Jane', age : 58} ] array2 = [ { name: ...

What is the most effective method for serializing SVG data from the client's Document Object Model (DOM

As I delve into the world of creating interactive SVG/AJAX interfaces, I find myself faced with a challenge. Users are constantly manipulating elements on-the-fly and I want to give them the option to export their current view as a PNG image or an SVG docu ...

Pressing the close button will shut down all bootstrap models

Is there a way to fix an issue where closing Model2 also dismisses the main Model? In my code, there is a button "Launch modal" to open the Model, and within the Model, there is a button "Launch modal2" to open a new model called Model2. However, when ...

Error in Laravel: The endpoint addproduct only supports POST requests, not GET

As a newcomer to Laravel, I am facing an issue when trying to post to a route without using any form for submission. My suspicion is that the problem lies within my controller. Below is the button code: <div class="button0"> <button ...

Testing the speed of the client's side

In my quest to create an application that allows users to conduct a speedtest on their WiFi network, I have encountered some challenges. While standalone speedtest apps like Speedtest.net and Google exist, server-based speed test modules from NPM are not s ...

Assigning properties to the constructor using `this.prop`

Within a React class component, I have multiple methods and the component receives various props. I am contemplating whether I should assign each prop as this.propName in the constructor and then access them using this.propName. For your reference, below ...

show fixed div when in view

After seeking inspiration from this specific thread on SO, I still need some help. The challenge I'm facing involves displaying a sticky footer within a parent div that is positioned halfway down the page. I've tried multiple tutorials, but none ...

"Utilizing the ng-class directive in AngularJS to apply multiple

I have been attempting to utilize the ng-class directive in AngularJS based on a certain condition: if a JSON value is 1, it should display as green; if it's 2, red; and if it's 0, white. However, I am unable to achieve the desired result for som ...

Get a Blob as a PNG File

Hope you had a wonderful Christmas holiday! Just to clarify, I am new to JS and may make some unconventional attempts in trying to download my Blob in PNG format. I am facing an issue with exporting all the visual content of a DIV in either PDF or image ...

How can I display several custom markers that are constantly updating on a Google map with MySQL and PHP?

Currently, I am using the following code to generate markers on a Google map by retrieving data from a database. However, the issue I am facing is that it only generates one marker instead of all the markers stored in the database. & ...

Encountering no automatic refresh feature in Next.js

Encountering an issue with Next.js where the local host index page doesn't automatically refresh whenever a change is made. To provide some context, I initiated a Next.js application using npx create-next-app --use-npm. After starting the local serve ...

How can I iterate through the lines of a JSON file using javascript?

I am currently working with JSON data and need to extract specific information based on an ID. For example, if the ID is 1, I want to retrieve details like service1 and version 1.0. I am looking to create a loop that will iterate through each line of data ...

Exploring the Nested Components Feature in Ionic 4

I am currently in the process of transitioning my application from Ionic 3 to Ionic 4, and I have noticed that async and await calls are now being used for each component. While I understand the reasoning behind this change, in my Ionic 3 app, I had a nest ...

Issue with Custom Directive: Receiving Token Error for Expression in Isolated Scope in Angular

(The following messages, code snippets, etc, have been slightly altered for clarification) The error message received is as follows: Syntax Error: Token 'promiseObject' is unexpected, expecting [:] at column 3 of the expression [{{promiseObject ...