Form in HTML with Automatic Multiplication Functionality in pure JavaScript

I'm struggling with integrating a simplified HTML form with JavaScript to dynamically adjust and multiply the entered amount by 100 before sending it via the GET method to a specific endpoint.

Below is the HTML form:

<body>
    <form method="get" action="https://endpoint/purchase.php">
        <input type="text" name="description" value="description">
    <input type="text" name="amount">
    <input type="submit" value="pay">
    </form>
</body> 

And here is the corresponding JavaScript code:

var $output = $("#output-value");
$("#input-value").keyup(function() {
    var value = parseFloat($(this).val());
    $output.val(amount*100);
});

I need help connecting these pieces to ensure the correct value is sent where I want it to go. Any suggestions?

Thank You. Another issue I'm facing is that the URL passed in the GET request gets cut or shortened sometimes. Below is an example of an original URL:

https://sklep.przelewy24.pl/zakup.php?z24_id_sprzedawcy=88696&z24_kwota=10000&z24_currency=pln&z24_nazwa=test&z24_opis=test&z24_return_url=http%3A%2F%2Fwww.przelewy24.pl&z24_language=pl&k24_kraj=PL&z24_crc=ca056736&

I am looking for a way to dynamically transfer z24_kwota (/z24_amount) based on my form input. After customizing the code above, I have:

<html>
<head>
<meta charset="utf-8">
<title>Simple Form</title>
</head>

<body>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    
    </script>
<form id="testForm" method="get" action="https://sklep.przelewy24.pl/zakup.php?z24_id_sprzedawcy=88696&z24_currency=pln&z24_nazwa=test&z24_opis=test&z24_return_url=http%3A%2F%2Fwww.przelewy24.pl&z24_language=pl&k24_kraj=PL&z24_crc=ca056736&">
  <input type="text" name="z24_kwota" value="Amount">
  
  <input type="submit" value="pay">
  <script>
        $("#testForm").on('submit', function() {
  var amount = $("#testForm input[name='z24_kwota']")
  var value = parseFloat(amount.val());
  value = value * 100 || 0; // set zero for non number

  if (prompt("Amount transferred to Przelewy24: ", value)) {
    amount.val(value);
    return true
  } 
  else {
    console.log('Submission aborted, value not multiplied')
    return false;
  }

});</script>
</form>
    
</body>
</html>

However, only the initial part of the URL appears in the browser address bar:'https://sklep.przelewy24.pl/zakup.php?z24_kwota=2000' Could someone please explain why this happens and how I can resolve it?

Answer №1

This issue is quite common. To troubleshoot, open your browser console (F12) and look for any errors related to missing elements with IDs #input-value or #output-value, as well as the variable amount.

If you need to modify the input name amount before submitting, you can utilize the submit event.

$("#testForm").on('submit', function() {
  var amount = $("#testForm input[name='amount']")
  var value = parseFloat(amount.val());
  value = value * 100 || 0; // assign zero if not a number

  if (prompt("Current amount value: ", value)) {
    amount.val(value);
    return true
  } 
  else {
    console.log('Submission cancelled, value remains unchanged')
    return false;
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="testForm" method="get" action="https://endpoint/purchase.php">
  <input type="text" name="description" value="description">
  <input type="text" name="amount">
  <input type="submit" value="Pay with przelewy24.pl">
  </form>

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

What are the various ways to display time zone in a different format?

I need to display the timezone in a unique manner. I have captured the user's timezone in a variable called timeZone, which currently returns the value as Asia/Calcutta. console.log(timeZone) // "Asia/Calcutta" Is there a way to showcase the timezon ...

Error message: Next.js - Unable to access properties of an undefined object (user)

I am currently utilizing Next.js and Next-auth in my current project. Within this project, I am working on creating a Sidebar component that will display a list of items specific to each user. To achieve this, I am using the useSession hook to retrieve t ...

Aligning a block of text containing two varying font sizes in the center

I'm attempting to center a paragraph element with a span inside that has a different font size, causing the alignment to be slightly off. Below is the HTML code: <p class="priceWrap"><span class="moneySign">$</span>60000.50</p> ...

A web application using JavaScript and Node.js with a focus on creating a REST

After extensive research, I am eager to develop a web application using JavaScript and Node.js with an SQL back-end. However, the abundance of frameworks and tools available has left me feeling overwhelmed. While I have identified some that I would like to ...

How to Disable a Dynamically Generated <li> Element Using jQuery on Click Event

Script Query: Creating <li> Elements Dynamically echo '<div class="pagination"><ul>' . "\n"; // Previous Post Link. if ( get_previous_posts_link() ) { printf( '<li>%s</li>' . "\n", get_previ ...

Error encountered in the main thread: Fail to click on element at current position

An error occurred in thread "main" org.openqa.selenium.WebDriverException: The element located at point (126, 7.98333740234375) is not clickable. Another element is intercepting the click: <div class="_1H5F__" data-reactid="10"></div> The com ...

Changing the value of the static key boolean within a react-grid-layout's layout object results in a misalignment of all grid items

tl;dr: React-grid-layout has an issue where grid items are incorrectly moved around when the static option is toggled. I want the grid items to stay in place when static and only move when users interact with them while static mode is disabled. Check out ...

What is the best way to retrieve a response from a modal dialog using jQuery?

Is there a way to utilize an add-in such as simple-modal or the dialog add-in in the UI kit to achieve AJAX interaction with the server? I am looking for a solution that allows the modal to communicate with the server and return the result back to the ca ...

Adjust the x and y coordinates of the canvas renderer

Manipulating the dimensions of the canvas renderer in Three.js is straightforward: renderer = new THREE.CanvasRenderer(); renderer.setSize( 500, 300 ); However, adjusting the position of the object along the x and y axes seems more challenging. I've ...

Fade-in animation of a clock on an SVG image

I am trying to achieve a unique fade-in effect for an SVG image in my HTML. The challenge is to make the fade-in start from the top of the image and progress in a circular motion until it completes a full circle. An example of the effect I am aiming for is ...

Express does not handle get requests to .html files

const express = require('express'); const app = express(); const port = 3000; const bodyPar=require('body-parser'); const session = require('express-session'); const path=require('path'); var user=["Jared","Bill","Ja ...

What is the best way to break down this function from props in React?

Forgive me if this question sounds naive, but as I delve into the world of React and useState, I am encountering a scenario where I have a signup function coded. Upon sending a username and password through a POST request to an API endpoint, a response mes ...

Learn how to implement interconnected dropdown menus on an html webpage using a combination of JavaScript, AngularJs, and JSON dataset

Incorrect HTML code example: <div ng-app="myApp" ng=controller="myCtrl"> States : <select id="source" name="source"> <option>{{state.name}}</option> </select> Districts: <select id="status" name="status"> ...

Exploring the possibilities of integrating CSS and JavaScript in Node.js with Express

Here is the folder structure I am working with: lifecoding |login2 |index.html |css style.css |js index.js I have all the necessary modules installed for express to work properly. However, when I try to set the static path as shown below, it keeps ...

onmouseleave event stops triggering after blur event

I am facing an issue with a mouseleave event. Initially, when the page loads, the mouseleave event functions correctly. However, after clicking on the searchBar (click event), and then clicking outside of it (blur event), the mouseleave functionality stops ...

The console is showing the Ajax Get request being logged, but for some reason it is not displaying on the

Could someone please explain why this response isn't displaying on the page? $.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { var http = (window.location.protocol === 'http:' ? &apos ...

step-by-step guide on showcasing and concealing textbox borders

Within a table, I have a column with a text box for users to edit the text as needed. The edited text is properly being saved using the onkeyup event. However, I want to hide the border of the text box after editing, so only the edited text is visible. If ...

Is it true that eliminating white spaces can enhance a website's loading speed?

I'm curious about something. Can removing white space actually make a website load faster? For instance, take a look at the following CSS snippet - body{ overflow-wrap:break-word; word-break:break-word; word-wrap:break-word } .hidden{ display:none } . ...

Adding a caption aligned to the right edge of an image in Wordpress

Any suggestions on aligning an image caption under an image tag to its right hand edge? I attempted using a div, but it seems it's not allowed in WordPress. Can anyone recommend alternative CSS or tags that I could use for this purpose? ...

Which comes first in AngularJS: ng-include or ng-repeat?

What is the outcome if I have a template containing ng-repeat and include it using ng-include? Will the included template have the completed ng-repeat, or will it be included without the ng-repeat being complete (and then completed after inclusion)? ...