tryingtogethelp
tryingtogethelp

Reputation: 101

send user to payments URL via form

I want users to be able to enter custom amounts in order to be redirected to paypal. I already have a form for myself to generate payment links that then generate buttons to be sent there but I want a client form to automatically send them to PayPal.

The user should fill out the form and then be sent to paypal.me/username/12 if 12 was the entered amount entered.

function process()
{
var url="https://paypal.me/username/" + document.getElementById("url").value;
location.href=url;
return false;
}
<form onsubmit="return process();" autocomplete="off">
<input type="text" name="pay" id="payamount" autofocus="">
<br><br>
<input type="submit" value="Make Payment" id="pay">
</form>

It does send the user to PayPal but to paypal.me/username/?pay=12 when it needs to be paypal.me/username/12 Not too sure how to fix this but help would be appreciated.

Thanks in advance.

Upvotes: 0

Views: 212

Answers (1)

Mosia Thabo
Mosia Thabo

Reputation: 4267

This is where the problem is:

var url="https://paypal.me/username/" + document.getElementById("url").value;

where is the element with "url" id?

Now with regards to this:

It does send the user to PayPal but to paypal.me/username/?pay=12 when it needs to be paypal.me/username/12 Not too sure how to fix this but help would be appreciated.

The reason you're getting ?pay=12 is because your form is by default submitting with get method and that is how properties are embedded into your submitted url. so pay is the name attribute value of the payamount element.

By understanding your code, below is what should happen:

target the id="payamount" input and not id="url" which does not exist on the html you provided.

var url = "https://paypal.me/username/" + document.getElementById("payamount").value;

Upvotes: 1

Related Questions