The JavaScript confirm function will provide a false return value

Is it necessary to display a confirmation message asking "Are you sure ..."? I have implemented the message, but the function return false; is not working when the user clicks NO.

<script>
function confirmDelete(){
  var answer = confirm("Are you sure you want to delete?");
  if (answer){
    // Do something if YES is clicked
  }
  else{
    return false;
  }
}
</script>

I am invoking the function using onclick attribute here

echo '<td><a onclick="confirmDelete()" href="delete-car.php?id='.$data["Car_ID"].'" class="ico del">Delete</a><a href="edit-car.php?id='.$data["Car_ID"].'" class="ico edit">Edit</a></td>';

Answer №1

Make sure to utilize the return value:

<a onclick="return validateForm();" ... >

Simply placing it within the onclick will trigger the function, but in order to truly prevent the click, you must use the return value.

Furthermore, you can optimize the code slightly:

function validateForm(){
    return confirm ("Are you certain you want to submit this form?");
}

No need for an if..else statement when you can directly return the confirmation result. (unless, of course, additional actions are required)

Answer №2

Make sure to include the return statement as well:

echo '<td>
    <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Delete</a>
    <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Edit</a>
    </td>';

Answer №3

You must ensure that your onclick function includes the return value:

echo '<td>
         <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">
            Sil
         </a>
         <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">
             Düzenle
         </a>
      </td>';

Answer №4

Make sure to include the return statement in both your function definition and where you call it to ensure you receive the correct result. Here is an example of how to correctly implement this:

echo '<td><a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';

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

employing async/await in the function of a backend API

I'm facing an issue with the following code snippet: service.js module.exports = { getUser }; async function getUser({ data }) { return new Promise((resolve, reject) => { const id = data["id"]; const doc = await db.colle ...

What situations warrant the choice of an Isomorphic JavaScript application?

Recently, there has been an increase in tutorials and examples discussing Isomorphic web applications with react.js. As a beginner myself, I find it challenging to determine whether this is necessary or not. For instance, my application will primarily be ...

organize the values based on their priority using reactjs

I'm facing a challenge involving two arrays of objects: array1 and array2. I need to display only three values from both arrays, with priority given to array1. The requirements are as follows: if array1 contains 3 elements, all three should be shown. ...

User controls in ASP.NET may not trigger the jQuery (document).ready() function

I wrote a jQuery function within my ASP.net user control that is not functioning as expected: <head> <title></title> <script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script language="javascript" ty ...

Transmit information to the server

Currently, I am in the process of creating an iPhone app. My goal is to utilize JavaScript to send longitude and latitude values retrieved from the phone to a server for the purpose of initiating a search function. Should I familiarize myself with cross-do ...

How to effectively utilize wrapAsync in MeteorJS when working with callbacks in the vzaar API?

Issue to Resolve: My current challenge involves retrieving an uploaded video ID asynchronously from an API in order to incorporate it into my code once the video upload process is completed. Unfortunately, I am encountering issues where the returned value ...

Utilizing Angular and the Kendo UI framework to customize the dimensions of a Kendo window

Is there a way to dynamically set the height and width of the kendo window based on the content of the kendo grid? Below is the code snippet for my kendo-window: <div kendo-window="Operation.OperCustWind" k-width="800" k-height="700" ...

Innovative form creation using Vue.js

My interactive form allows users to input an item, quantity, and cost per item like this: <form @submit.prevent="submit"> <div class="form-group" v-for="(input,k) in inputs" :key="k"> <input ty ...

Activate browser scrollbar functionality

Below is the HTML code snippet: <html> <head> <style> #parent { position : absolute; width : 500px; height : 500px; } #top { position : absolute; width : 100%; height: 100%; z-index : 5; } #bottom { position : absolute; width : 100%; ...

Is it possible for PHP to delay its response to an ajax request for an extended period of time?

Creating a chat website where JavaScript communicates with PHP via AJAX, and the PHP waits for the database to update based on user input before responding back sounds like an intriguing project. By using a recall function in AJAX, users can communicate ...

django Ajax GET request could not locate the specified URL

I'm facing an issue while trying to pass parameters through Ajax with Django 1.11. The error message states: Not Found: /enquiry/followup_alter/. Below is the relevant code snippet. Error: Not Found: /enquiry/followup_alter/ Ajax: $(docume ...

Dynamic options can now be accessed and modified using newly computed getters and setters

When using Vuex with Vue components, handling static fields that are editable is easily done through computed properties: computed: { text: { get() { return ... }, set(value) { this.$store.commit... }, }, }, <input type ...

Is it possible to incorporate a PHP line within a Handlebars context array?

I'm currently utilizing handlebars along with a handlebars register helper to iterate through an array that I've included in my context. The result is then displayed as an unordered list (ul). However, I'm facing an issue when attempting to ...

Is it possible for a JWT generated using RS256 to be decoded on the jwt.io platform?

After setting up my first Express server and implementing user authentication with jwt, I'm now searching for a method to encrypt the jwt in order to prevent users from viewing the payload on the website. I am curious if anyone is aware of an encryp ...

The debounced function in a React component not triggering as expected

I am facing an issue with the following React component. Even though the raiseCriteriaChange method is being called, it seems that the line this.props.onCriteriaChange(this.state.criteria) is never reached. Do you have any insights into why this.props.onC ...

A guide to customizing node names using vue-slider-component

I am facing an issue with the vue-slider-component. Below is the link to my current test module: template:` <div> <vue-slider v-model="value" :order="false" :tooltip="'always'" :process="false" ...

Angular displays X items in each row and column

I've been struggling with this task for the past 2 hours. My goal is to display a set of buttons on the screen, but I'm facing some challenges. The current layout of the buttons doesn't look quite right as they appear cluttered and unevenly ...

How can I loop through JSON in AngularJS to populate fields without knowing the key value?

Here is the data structure that I'm working with: { "0": { "keyword": "flower", "short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "pt_value": "5" }, "1": { "keyword": "tree", "short_desc": "Lorem ipsum dolor sit amet, consecte ...

The value of the comment box with the ID $(CommentBoxId) is not being captured

When a user enters data in the comment box and clicks the corresponding submit button, I am successfully passing id, CompanyId, WorkId, and CommentBoxId to the code behind to update the record. However, I am encountering an issue as I also want to pass the ...

The search feature in my React Pagination is not performing as effectively as expected

I recently set up a React app that interacts with a MongoDB database using an Express Server. The pagination feature is working smoothly, but I encountered an issue with the search function. It only works when typing in the input box; deleting characters d ...