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

I'm wondering if anyone has any insights as to why the CSS rule `body{ overflow: auto;}` is not functioning properly for me. Despite my attempts of adding a class to the body tag and

I am facing an issue where adding body tags to my code in Visual Studio code does not yield the desired results. Interestingly, it doesn't seem to be a syntax error in CSS. body{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI&apo ...

Using a combination of radio buttons and JavaScript to adjust the color of a div element

Currently, I am experimenting with radio buttons to modify the background color of my div tag. My goal is to incorporate more than one condition. For example, if button A and button B are both clicked, then change the color to green. This is what I have im ...

PHP error: Index not defined

One challenge I’m encountering is with a dating site form that includes the following categories: firstname,lastname,username,password,email,mysex,yoursex,relationship,date of birth, and country I've completed the PHP code to send information to a ...

Trying to configure and use two joysticks at the same time using touch events

I have been grappling with this problem for the past two days. My current project involves using an HTML5/JS game engine called ImpactJS, and I came across a helpful plugin designed to create joystick touch zones for mobile devices. The basic concept is th ...

Having trouble with Simplemodal showing link content in the modal window

Having trouble getting the modal window to display content from another link? I'm pretty sure I've connected them correctly using the right classes. This is the basic JavaScript provided by . jQuery(function ($) { // Load dialog on page load //$ ...

Tips for positioning a label within the span element wpcf7-form-control-wrap

Is it possible to place the label for input elements inside the span element generated by the cf7 shortcode? I want to achieve this in order to make the label only visible when the input field is in focus. To accomplish this, both the label and the input ...

How can I align a sub-submenu horizontally using CSS?

I've built a nested menu using the ul/li tags, and I'm looking to align the second and third sub-menus horizontally with the first submenu. Here is my current visual layout: , and this is what I want it to look like: CSS/HTML: body { back ...

Looking to expand the width of the sub menu to reach the full width of the

Is there a way to use CSS to make a sub-menu start from the left side of the screen instead of starting it below the parent item? nav { margin: 0 auto; text-align: center; } nav ul ul { display: none; } nav ul li:hover > ul { di ...

Concealing scroll bars while still maintaining the ability to scroll by using overflow:scroll

Similar Question: How to hide the scrollbar while still allowing scrolling with mouse and keyboard I have created a sidebar for a web application that needs to enable users to scroll through it without displaying a scrollbar. The content is 500px high ...

Styling a Razor view using inline CSS and responsive media queries

I need to add some simple inline styling for print purposes to an .cshtml page I'm working on. I initially tried using the @media print media query, but it is causing issues with my cshtml page. Is there a workaround for this problem? The culprit see ...

Developing jpg/png images from .ppt/pptx files with Django

I currently have a PowerPoint file saved on Dropbox at "". My goal is to convert this presentation file into individual slide images (jpg/png..) within my Django template or using a Django def function. Once I have converted the slides, I plan to utilize ...

In order to enhance user experience, I would like the tabs of the dropdown in the below example to be activated by

function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } ...

What is the best way to ensure that my drop down select menu consistently displays the same data even after performing a query with the menu?

Hey there! I'm facing a little hiccup with a drop-down selection box. Here's what I'm trying to do - get someone to log into a database, and then display all available tables in the selection box. Once they choose a table, they hit select an ...

What are some strategies for speeding up CRUD operations in a Django app that uses Ajax and Json? Is the sluggishness related to handling 7000 records, and if so, what steps can be taken

I am currently working with a remote legacy MySQL database that contains around 7000 records. The Update / Delete / Create operations are taking approximately 1.5 minutes to complete. I have imported the necessary files which are not included in Views.py b ...

"Rotated element in Opera does not maintain event functionality when positioned outside its

Make sure to test this example on Opera (version 12.02) by clicking this link. In Opera, try clicking the red box inside the blue box (event is triggered), then click the red box outside the blue box (event isn't triggered). The container must have p ...

Enhance the appearance of Google Maps by incorporating gradient effects, box shadows, or other similar visual

How can I add a linear gradient to Google Maps? Below is my code snippet. HTML <div id="map"></div> CSS #map{ height:100vh; background: linear-gradient(90deg, #f7f8fa 0%, rgba(247, 248, 250, 0) 18.73%), linear-gradient(180deg, # ...

A straightforward jQuery conditional statement for dynamically generated content

If the div '#ajax' contains content, I want to make the div '.container' clickable. Here is the basic jQuery script I have implemented: if ('#ajax:empty') { $('.container').css('pointer-events', ' ...

Managing errors in jQuery's .ajax function

Having some issues with jQuery.ajax() while trying to fetch an html code snippet from table_snippet.html and replacing the element in my html code. The error handler in jQuery.ajax() gets triggered instead of the expected success handler. <!DOCTYPE H ...

:hover doesn't fully implement background color on list items

When I hover over the dropdown items in my navigation menu, I would like the background color to change for the entire list item. Currently, the background color only changes when hovering over the text itself and reverts back to its original color when mo ...

Designing a loading animation with rotating lines converging towards the center to form a circular motion

Check out my code snippet below: .circle { border: 1px solid transparent; border-radius: 50%; width: 100px; overflow: hidden; } .div7 { width: 100px; height: 10px; background: red; color: white; font-weight: bold; positi ...