-3

Ive started the form I need, however having difficulty with the data being inserted into the URL.

I need to gather the following information and insert it into the following link, which the user can then copy upon clicking generate.

Appreciate support

Client First Name
Client Last Name
Order Reference
Main Amount
Todays Date

//payments.XXXXX.net/process/payments/choice?charset=UTF-8&billingemail=&billingfirstname=Joe&billinglastname=Bloggs&currencyiso3a=GBP&locale=en_GB&mainamount=1000.00&orderreference=12345&settleduedate=2022-07-27&settlestatus=0&sitereference=&stprofile=default&version=2

function process() {
  var url =
    location.href = url;
  return false;
}
<form onSubmit="return process();">
  <input type="text" name="Payment Amount" id="amount">
  <input type="text" name=“Client First Name” id="fname">
  <input type="text" name=“Client Last Name” id="sname">
  <input type="text" name=“Booking Reference” id="bookref">
  <input type="submit" value="Create Unique Payment Link">
</form>
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
BGflyer
  • 1
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 27 '22 at 19:47
  • There is no Java code here. The first step is to identify the programming language you're working in, which I suspect it Javascript based on the snippet. – Silvio Mayolo Jul 27 '22 at 19:48
  • Sorry - I need the inputted date from the form to be inserted in the placeholders that I have put in strong – BGflyer Jul 27 '22 at 19:49
  • this should be a html script – BGflyer Jul 27 '22 at 19:49
  • thanks for the answer but im unable to make this work. how do I make the completed string display. can you demonstrate – BGflyer Jul 27 '22 at 20:07
  • @BGflyer I have updated my answer to copy to clipboard and to write the link in case the clipboard is not available to the browser. Please look and comment – mplungjan Jul 30 '22 at 05:30

1 Answers1

1

You can use the URL api and the searchParams

I choose clipboard.js plugin since it seems to be quite cross browser

// Example link
//payments.XXXXX.net/process/payments/choice?charset=UTF-8&billingemail=&billingfirstname=Joe&billinglastname=Bloggs&currencyiso3a=GBP&locale=en_GB&mainamount=1000.00&orderreference=12345&settleduedate=2022-07-27

const yyyymmdd = () => new Date().toISOString().split('T')[0]; // format date
const generated = document.getElementById("generated");        // the textarea
const copyButton = document.getElementById("copy");            // the button that will copy from the textarea
const form = document.getElementById("urlForm");               // the form to use
const clipboard = new ClipboardJS('#copy');                    // instantiate the clipboard plugin

form.addEventListener("submit", (e) => { // when submitting
  e.preventDefault(); // stop the submit
  const url = new URL("https://payments.XXXXX.net/process/payments/choice?settlestatus=0&sitereference=&stprofile=default&version=2&currencyiso3a=GBP&locale=en_GB&charset=UTF-8"); // as much static parameters as needed
  const sp = url.searchParams; // the searchParams object to modify
  sp.set("billingemail", "");  // perhaps you want to ask for that too?
  sp.set("billingfirstname", form.fname.value);
  sp.set("billinglastname", form.sname.value);
  sp.set("mainamount", form.amount.value);
  sp.set("settleduedate", yyyymmdd());
  sp.set("orderreference", form.bookref.value)
  generated.value = url.toString(); // show the URL
  // form.action = url.toString();  // uncomment to go to the link instead 
})
#generated,
#copy {
  display: inline-block;
  vertical-align: middle;
}
<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.10/dist/clipboard.min.js"></script>

<form id="urlForm">
  Amount:<br>
  <input type="text" name="Payment Amount" id="amount"><br/> First name:<br/>
  <input type="text" name="Client First Name" id="fname"><br/> Surname:
  <br/>
  <input type="text" name="Client Last Name" id="sname"><br/> Booking reference:<br/>
  <input type="text" name="Booking Reference" id="bookref"><br/>
  <input type="submit" value="Create Unique Payment Link">
</form>
<hr/>

<textarea id="generated"></textarea><button id="copy" data-clipboard-target="#generated"><b>⧉</b></button>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    If I am not wrong the task is to allow the user to copy the url so it can be used somewhere else. I would add a call to navigator.clipboard.copyText(url.toString()) in the submit action here. You can find more details on using the clipboard from here https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript – guilleamodeo Jul 28 '22 at 07:45
  • @guilleamodeo Ok added clipboard – mplungjan Jul 28 '22 at 08:24
  • 1
    Well I think you answered the question @mplungian, thus I voted it useful with disregard of its question being a bit unpopular because people think he could have researched a bit more. Well, to me it could be a lost new arrival to the JS/DOM world. :-) – guilleamodeo Jul 29 '22 at 16:04