Leveraging IF conditions in AJAX Response

When submitting a form, I have implemented a check to see if the value already exists in the database. The AJAX post is returning the correct result, but there is an issue when trying to display an alert for the wrong result. Even though the JavaScript alert and focus work as expected, the form still submits afterwards.

Here is the submit button:

<input type="submit" name="kaydet" class="btn btn-success form-control" onClick="return kaynak_kontrol()" value="Kaydet">

Below is the AJAX code:

<script type="text/javascript>
function kaynak_kontrol(){
  Form=document.forms['depo_kayit'];
  var depo_sube_no = document.getElementById('depo_sube_no').value; 
  var depo_firma_no = document.getElementById('depo_firma_no').value;
  var depo_kodu = document.getElementById('depo_kodu').value;
  var dataString ="depo_sube_no="+depo_sube_no+"&depo_firma_no="+depo_firma_no+"&depo_kodu="+depo_kodu; 

  $.ajax({ 
    type: "POST",
    url: "depo_kodu_kontrol.php", 
    data: dataString, 
    success: function(result){ 
      if(result != 0){
        alert("Aynı şubede aynı isimde iki depo olamaz!");
        document.getElementById('depo_kodu').focus();
        return false;
      } else {
        return true;
        Form.submit();
      }
    }
  });
}
</script>

Could someone assist me with understanding why the 'return false' statement is not preventing the form from being submitted?

Answer №1

To avoid the form from being submitted, jQuery must be used to manually prevent it by calling event.preventDefault()

Check out this quick solution

function check_source(event){ // important: add the new parameter !
    event.preventDefault();
    //include your remaining code here

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

Scroll to the bottom of a textarea in React by automatically shifting focus

I am having issues with appending text to a textarea and then scrolling to the bottom in order to keep the latest content within view. However, it seems that this process is causing the browser to crash or run out of memory. Can someone provide assistance ...

After a period of time since the server has been initialized, the function req.isAuthenticated() no

In my Node.js routes.js file, I have a function implemented to check if a request is isAuthenticated before serving it: function isLoggedIn(req, res, next) { if (req.isAuthenticated()) { console.log('Session Expiry '+req.session.cook ...

retrieve the index number of a parent node by utilizing the indexOf.call method in JavaScript

I discovered a unique DOM layout <ul class="main-products"> <li class="product">product</li> <li class="product">product</li> <li class="product">product</li> <secti ...

Delay the v-alert display after an item is added to the array basket using setTimeout

here is my custom rightTableMenu template <template> <div> <h1 align="center">{{ title }}</h1> <v-alert type="info" icon="mdi-emoticon-sad" v-if="basketStatus"> Empty Basket, please add some to basket < ...

Determine which JavaScript script to include based on whether the code is being executed within a Chrome extension

I am in the process of developing a Chrome extension as well as a web JavaScript application. I currently have an HTML container. I need the container.html file to include <script src="extension.js"> when it is running in the Chrome extension, and ...

Determine the variance between two dates selected using the jQuery datepicker

I have a query regarding date calculations in JavaScript. I am trying to find the difference in days between two dates selected using jQuery datepickers. Although I am accessing the dates by their clientIDs and attempting to calculate the difference, my m ...

What is the best method to transfer information from a What You See Is What You Get editor to a database using Vue.js?

I have recently started using the Vue2Editor in order to streamline my process of sending text and image data to my Firebase database. However, I am encountering an issue where the entered data is not being added successfully. Previously, with traditional ...

Utilize Express Handlebars to render an input-generated list for display

My goal is to showcase a collection of wishlist items on a basic webpage through user interaction. Here's how I envision it: 1. do something 2. do another thing 3. blahblah This snippet shows my index.js code: var wishlist = []; router.post('/& ...

Instructions for converting a blob URL into an audio file and securely storing it on the server

After successfully recording and adding the audio to my HTML page within the audio tag, I managed to obtain the blob source URL from the tag using the following line: var source = document.getElementById("Audio").src; The blob source URL looks ...

Changing JSONArray's to JSON Object in JavaScript

I have a task at hand where I need to combine two JSON arrays into a single JSON object that I have retrieved from an ajax call. Here is the code snippet that shows how I am fetching and storing the data: var a = []; var b = []; ...

Avoid changing the regex pattern if it is surrounded by a specific character

I want to change all occurrences of strings enclosed by - with strings enclosed by ~, unless the string is again surrounded by *. For instance, consider this string... The -quick- *brown -f-ox* jumps. ...should be altered to... The ~quick~ *brown -f-ox ...

AngularJS controller function fails to trigger

I have three directives, "parenta", "parentb", and "childb". The first two are siblings while the third one is a direct child of "parentb". When attempting to call a controller method from "parenta", it does not work. Strangely, calling a method on the "c ...

utilizing count to pre-determine record allocation

From my understanding, pre-allocating a record has the potential to enhance performance, particularly when dealing with a large volume of records in a time series dataset. updateRefLog = function(_ref,year,month,day){ var id = _ref,"|"+year+"|"+month; ...

extracting values from an array using the map function in React

In React JSX, I have an array called levels that may contain arrays with various level names such as one, two, and three. Within my render function, I utilize {renderLevels} to display all levels separated by commas. This approach works well: const rende ...

Steps for altering the public folder in a Next.js project

In my monorepo, the structure is as follows: apps/ web/ - next.config.js - pages/ public/ The Next.js application is located in the apps/web/ directory. How can I modify the next.config.js file to instruct it to serve public assets from the root ...

What is the best approach to determine the value of a textbox in an array for each row of

I am trying to display the total sum of values for each row in a data array. There are 5 rows of data, and I need to calculate the results for each one. Can someone assist me in solving this? function calculateTotalValue(){ var total = (document.get ...

Updating the output without the need for a page refresh following the insertion of data into the database

I'm interested in enhancing the interactivity of this section in my code. Currently, I utilize a form to send announcements via ajax to a php file which successfully updates my database. The table displays the data effectively. However, I want these c ...

Display a list of records retrieved from a Firebase query using ngFor to iterate through each instance

I'm currently using firebase and angular to work on a billing list project. The list contains data showing information for a specific month, you can refer to the imagehttps://i.stack.imgur.com/ZR4BE.png While querying the list was smooth, I encounte ...

Include the URL as a parameter in the query when utilizing Tesseract OCR

I have successfully implemented the tesseract ocr, but I am wondering if it is possible to run tesseract with a URL as a parameter. I want to achieve the following: localhost/test.html/?othersite.com/image/image2.jpg Here are some image URLs for demonst ...

Else statement malfunctioning with Alert() function

I have noticed an issue with my user input field. Even when I enter a valid genre name, the alert prompt still appears saying it is not a valid genre. This occurs after entering both valid and invalid inputs. For example, if the user enters "horror," whic ...