Ways to secure the formdata message when sending it through the submit action

I have a unique script that modifies the behavior of submit buttons on social networking sites, such as Facebook. The script sets an "onclick" attribute that triggers a specific function when a user clicks on a submit button. This function is designed to encrypt messages that users send through these platforms.

The idea is to intercept the click event, prevent the default action from happening, extract the message being sent (possibly using formData), encrypt the text, and then allow the submit action to proceed with the encrypted message.

Below is a simplified version of the script:

$('textarea', window.content.document)
.closest('form')
.find('input[type=submit]')
.attr("onclick","encryptMessage();");

function encryptMessage(){
// Logic to access formData, encrypt the message, and continue submit action
};

Answer ā„–1

It appears that a crucial return statement is missing in your code snippet. Without the return keyword before the function call in the onclick attribute, the action may proceed regardless of the function's outcome.

$('textarea', window.content.document)
.closest('form')
.find('input[type=submit]')
.attr("onclick","return validate();");

function validate(){
//Retrieve form data and encrypt the message before submission
//Remember to explicitly return true or false to control the submission
};

Answer ā„–2

To avoid adding an onclick function to the submit buttons, you can directly incorporate a call to the function that collects and encrypts data within the form itself, as demonstrated below:

<form name="sampleForm" method="post" onsubmit="return dont()" enctype="multipart/form-data" action="someurl">


function dont()
{
    //code to access the form data and encrypt

    if(error)
       return false;  //Prevents form submission
    else
      return true;

}

Alternatively,

You can use a button (input type=button) instead of a submit button (input type=submit) and define the onclick function like this:

<input type="button" value="submit" onclick="dont()" />

function dont()
{
    //code to access the form data and encrypt

    if(error)
       alert("Failed")  //Stops the submission of the form
    else
      document.forms[0].submit();

}

I hope this explanation is helpful.

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

Preventing attribute or tag cloning in Jquery: What you need to know

I'm having some trouble with JQuery. I want to clone an attribute or tag that is dragged from one div1 to another div2, which is working fine. But the issue arises when I drag or move the position of an existing tag on div2, then the cloning process s ...

Retrieve information from a variety of selected checkboxes

Is there a way to retrieve the values of check boxes that are generated dynamically? @ $db = mysql_connect("abc", "abc", ""); mysql_select_db("abc"); $strSQL = "SELECT * FROM student"; ...

Easily retrieve the value of a form control when it is changed with Form

Currently, I am in the process of reviewing Formly. In my initial attempt, I am trying to trigger a method when the content of a textarea changes. The code snippet below shows what I have tried so far. Unfortunately, it seems like the functionality is no ...

Experiencing a problem with the bundle.js file in Angular Universal

My Angular build is giving me this error in the web browser: Uncaught SyntaxError: expected expression, got '<' in bundle.js When I run the command npm run dev:ssr, no errors are generated. However, when I try to access the application, the ...

Why is AJAX failing to execute PHP file from JavaScript?

The Issue: Hello, I have confirmed that my PHP file is functioning properly. However, the AJAX code connected to my JavaScript function seems to be malfunctioning. Even though the function is triggered, nothing happens as expected. The Script: Check o ...

PHP Server with MySQL Form Builder

I seem to be stuck and unable to make progress with this issue. I am facing a roadblock in my project and would appreciate it if someone could take a look and provide some insights. Despite verifying the connection to MySQL, I am encountering an unusual ...

The scrollOverflow feature in fullPage.js is not properly detecting the height of dynamically generated content

I have successfully implemented fullpage.js on my website. Everything is working perfectly with fullpage.js, but I am facing an issue when trying to open a div on click of pagination. Specifically, the browser scroll gets disabled when the div containing a ...

Countdown component in Ant Design failing to display correct date

Iā€™m currently working on developing a specific date component using react in conjunction with antd. Below is the code snippet I am utilizing: import { Statistic, Col, Row } from 'antd'; const { Countdown } = Statistic; const deadline = Date.pa ...

Uploading multiple images using AngularJS and Spring framework

I'm encountering an issue with uploading multiple images using AngularJS and Spring MVC. While I can successfully retrieve all the files in AngularJS, when I attempt to upload them, no error is displayed, and the process does not reach the Spring cont ...

How come the instanceof operator returns false for a class when its constructor is present in its prototype chain?

While working on a NodeJS app, I encountered unexpected behavior when trying to verify that a value passed into a function is an instance of a specific Class. The issue arises when using instanceof between modules and checking the equality of the Class. e ...

What is the method for ensuring text remains within a square while it is being relocated?

Check out this jsfiddle where you can interact with a moving square: http://jsfiddle.net/helpme128/3kwwo53t/2/ <div ng-app="test" ng-controller="testCtrl"> <div id="container"> <div class="shape" ng-draggable='dragOptions& ...

How to Implement Flexbox Display Across Various Browsers Using jQuery?

Running into an issue here. I'm trying to switch from display: none to display: flex, display: flex-box, and display: -ms-flexbox but for some reason it's causing errors in my code.. Here's what I've been working on: $(elem).css({&apos ...

Error encountered while updating in the midst of an ongoing state transition, e.g. within the `render` method

class MyComponent extends React.PureComponent { constructor(props) { super(props); this.state = { obj: [], externalObj: [], }; } fetchData = (external) => { ... arr = arr.filter(a => a.toLowerCase().includes(&ap ...

Organize the array by property name and include a tally for each group

My current data structure looks like this: var data = [ { MainHeader: Header1, SubHeader: 'one'}, { MainHeader: Header1, SubHeader: 'two'}, { MainHeader: Header2, SubHeader: 'three'}, { MainHeader: Header2, SubHea ...

Is there a way to activate a click event on a particular child element within a parent element?

When attempting to click on a specific descendant of an element, I located the ancestor using ng-class since it lacked an id. yes = document.querySelectorAll('[ng-controller = "inventoryController"]') Having found the desired ancestor, ...

Angular @Input set function not being activated during unit testing

Within my Component @Input('price') set setPrice(price) { this.price = price; this.modifyTotalAmount(); } Unit Testing (component.spec.ts) it('should execute function ', () => { spyOn(fixture.componentInstance, ' ...

Executing JavaScript code within a class object using ASP-VB

I'm looking to create a function that will show a Javascript-based notification. I already have the code for the notification, but I'm trying to encapsulate it in a class library as a function. However, I am unsure about what to return from the f ...

Modify hyperlink address according to chosen option

Looking to incorporate a select input in Laravel using the latest alpine.js build. Here's what I have in mind: {{ Form::select('dogs', $dogs) }} This utilizes LaravelCollective HTML for streamlined form creation. Once an option is chosen, ...

React Material UI unselect

I have been exploring a MUI select field implementation using this example as a guide. While I was able to make the code work, there are a few issues that I encountered. One of them is the inability to deselect a value once it has been selected. Additional ...

What is the process for performing the "extract function" refactoring in JavaScript?

Are there any tools for extracting functions in JavaScript similar to the "extract function" refactoring feature available for Java and jQuery developers in Eclipse or Aptana? Or perhaps in another JavaScript/jQuery IDE? ...