Ways to efficiently convert HTML form data into JSON format

Currently, I am in the process of developing an ecommerce platform. As part of this project, I have created a billing form to gather essential information such as email addresses and physical addresses from users. The intention behind collecting this data is to structure it into a JSON format that can then be seamlessly transmitted to the payment gateway.

I initially attempted to store this information in a variable before passing it along. However, upon reaching the redirected modal form on the payment gateway interface, I encountered an error message indicating an invalid email input.

Snippet of Code

const publicKey = "{{ key }}";

var email = document.getElementById('email').value;
var fullname = document.getElementById('fullName').value;
var address1 = document.getElementById('custAdd').value;
var address2 = document.getElementById('custAdd2').value;
var country = document.getElementById('country').value;
var state = document.getElementById('state').value;
var postcode = document.getElementById('postCode').value;

function payWithRave() {

  var x = getpaidSetup({
    PBFPubKey: "xxxxxx",
    email: email,
    amount: '{{cart.get_total_price}}',
    customer_phone: "234099940409",
    currency: "USD",
    address: 'address1',
    address2: 'address2',
    country: 'country',
    state: 'state',
    postcode: 'postCode',
  })
<div class="col-sm-7">
  <label for="firstName" class="form-label">Full Name</label>
  <input type="text" class="form-control" id="fullName" placeholder="" required>
  <div class="invalid-feedback">
    Valid first name is required.
  </div>
</div>
<div class="col-12">
  <label for="email" class="form-label">Email <span class="text-muted">(Optional)</span></label>
  <input type="email" class="form-control" id="email" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86ffe9f3c6e3fee7ebf6eae3a8e5e9eb">[email protected]</a>">
  <div class="invalid-feedback">
    Please enter a valid email address for shipping updates.
  </div>
</div>
<div class="col-12">
  <label for="address" class="form-label">Address</label>
  <input type="text" class="form-control" id="custAdd" placeholder="1234 Main St" required>
  <div class="invalid-feedback">
    Please enter your shipping address.
  </div>
</div>
<div class="col-12">
  <label for="address2" class="form-label">Address 2 <span
                            class="text-muted">(Optional)</span></label>
  <input type="text" class="form-control" id="custAdd2" placeholder="Apartment or suite">
</div>
<div class="col-md-5">
  <label for="country" class="form-label">Country</label>
  <select class="form-select" id="country" required>
    <option value="">Choose...</option>
    <option>United States</option>
  </select>
  <div class="invalid-feedback">
    Please select a valid country.
  </div>
</div>
<div class="col-md-4">
  <label for="state" class="form-label">State</label>
  <select class="form-select" id="state" required>
    <option value="">Choose...</option>
    <option>California</option>
  </select>
  <div class="invalid-feedback">
    Please provide a valid state.
  </div>
</div>
<div class="col-md-3">
  <label for="Postcode" class="form-label">Postcode</label>
  <input type="text" class="form-control" id="postCode" placeholder="" required>
  <div class="invalid-feedback">
    Zip code required.
  </div>
</div>
</div>


<hr class="my-4">
<button type="button" class="btn btn-primary w-100 fw-bold" onClick="payWithRave()">Pay ${{cart.get_total_price}}</button>

Answer №1

const publicKey = "{{ key }}";

const payWithRave = (ev) => {
  ev.preventDefault();
  let x = {
    PBFPubKey: "xxxxxx",
    fullname: document.getElementById('fullName').value,
    email: document.getElementById('email').value,
    amount: '{{cart.get_total_price}}',
    customer_phone: "234099940409",
    currency: "USD",
    address: document.getElementById('custAdd').value,
    address2: document.getElementById('custAdd2').value,
    country: document.getElementById('country').value,
    state: document.getElementById('state').value,
    postcode: document.getElementById('postCode').value,
  }
  console.log(x);
}

document.addEventListener('DOMContentLoaded', () => {
  document.getElementById('btn').addEventListener('click', payWithRave);
})
<!DOCTYPE html>
<html>
<body>

<div class="col-sm-7">
  <label for="firstName" class="form-label">Full Name</label>
  <input type="text" class="form-control" id="fullName" placeholder="" required>
  <div class="invalid-feedback">
    Valid first name is required.
  </div>
</div>
<div class="col-12">
  <label for="email" class="form-label">Email <span class="text-muted">(Optional)</span></label>
  <input type="email" class="form-control" id="email" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3baacb683a6bba2aeb3afa6eda0acae">[email protected]</a>">
  <div class="invalid-feedback">
    Please enter a valid email address for shipping updates.
  </div>
</div>
<!-- More HTML code here -->
  


<hr class="my-4">
<button type="button" id="btn" class="btn btn-primary w-100 fw-bold">Pay ${{cart.get_total_price}}</button>

</body>
</html>

strong text

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

Switch between displaying the HTML login and register forms by clicking a button

Currently, I am working on a website that requires users to either log in or register if they do not have an account. Below is the code I have developed for the login page: <span href="#" class="button" id="toggle-login">Log in</span> <di ...

Is it possible to disable the save option on a PDF popup window?

When displaying a PDF using an embed tag, I have managed to disable print and content copying through document properties. However, I am struggling to find a solution to disable the save option that pops up. Is there a way to achieve this using JavaScrip ...

The image is failing to animate according to the PNG sequence function

Enhanced Functionality: Upon clicking the "Tap Here" image button, a function called "GameStart()" is triggered. This function ensures that the main image "Star" descends down the page from the top through an animated sequence using png logic. The propose ...

Can you explain the purpose of :not(style) ~ :not(style) in CSS?

Upon investigating, I found that Mui automatically included a :not(style) ~ :not(style) selector which required the use of !important in my sx to override. .css-1rb8ayf-MuiStack-root > :not(style) ~ :not(style) { margin-top: 40px; } .css-d13d9m-MuiSta ...

The current issue lies with the validation process of the SerializerMethodField

Currently, I am developing my Django DRF application and dealing with two models - Organization and BankAccount class Organization(models.Model): ... class BankAccount(models.Model): is_main = mode ...

Learn the process of generating dynamic rows in HTML

Currently, I'm working on a website where I am incorporating the Flipkart API to display a list of products. However, the products are not appearing in the well-formatted view that I had designed. Specifically, I want only 4 products to show per row b ...

Scrolling to a specific anchor in a React webpage when the URL is opened in

Imagine having a component "Post" that contains multiple components "Comment". How can we make the application automatically scroll down to a specific comment when entering a URL like this: /post/:postId/#commentId The route for postId /post/:postId is a ...

How can I ensure that the search form stays aligned with the other elements in the navbar using Bootstrap CSS?

I am new to CSS and I am facing an issue with my search form in the navbar. When I initially load the page, the search form appears on 2 lines but when I refresh the page, it aligns properly with the other elements. How can I ensure that the search form st ...

Mobile responsiveness issues with Bootstrap 4 row

<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS ...

Provide the option to assign values on select options in order to choose specific JSON data

When working with JSON data from an API, I am creating a dynamic select element. The goal is to change some content (text and image src) based on the option selected from this select element. I have successfully populated the select options with names usi ...

What is the correct way to denote a CSS class in SASS syntax?

Can you show me how to code this in SASS? .solo:hover > .solo-pane, .solo-pane.active, .solo-pane.lighten { opacity: 0.5; } I'm having trouble separating the classes using commas. ...

Guide to Aligning Divs at the Center in Bootstrap 4

I've been attempting to center the div on the page using Bootstrap 4, however, it's not cooperating. I've tried using the margin:0 auto; float:none property as well as the d-block mx-auto class, but neither are working. Below is my HTML code ...

Link to toggle a CSS spoiler button

Currently, I have Ajax set up to replace the main div with a link. In addition, there's a spoiler-type navigation that allows you to hide or show the menu. I have my links inserted in this structure and am trying to figure out how to make it so that c ...

Is there a way to compel the browser or JavaScript to delete/disregard a cached file?

I've encountered an issue with my Javascript program that extracts data from a text file and displays it in a table. The problem arises when I update the text file - the changes don't reflect on the table because the browser is caching the file. ...

Animating background color change with scroll in React using fade effect

Can someone help me with implementing a fading animation for changing the background color on scroll in React? I have successfully achieved the background change effect, but I'm struggling to incorporate the fading effect. import React from "reac ...

Instructions on how to automatically navigate to a different tab upon clicking an <a> element on a separate webpage, and subsequently display the designated tab on that

I have a button on my home page that, when clicked, should open a specific tab section on another page. The button is located on one page and the tabs are on a different page. On the second page, I have multiple tabs but will only mention one here, which ...

Styling a parent element when it is in focus using CSS3

Is there a way to apply a style to the parent element when an input is in focus? input:focus { background-color:yellow; } For example, if we have the following HTML: <div class="foo"> <input type="text /> Hello </div> I ...

You may only insert a single image into a container

My goal is to display two images side by side with text centered between them. However, when I add the first image using CSS and the background property, I run into issues adding a second image as it overrides the first one. Placing the images directly in ...

Efficiently shrink column width in Material-UI's <TableRow/> using ReactJS and Material-UI

At the moment, I am utilizing ReactJS along with Material-UI. One issue I am encountering is that when using Material-UI's <Table>, the columns' width are automatically set based on content which results in all columns having equal width. H ...

Creating movement in three distinct divisions

I am seeking a way to have three divs flying in upon click. The first DIV is positioned at the top, followed by one on the left and one on the right (both being below the top one). I wish for them to fly in from their respective directions - the top div fr ...