I have a client selling a t shirt. She wants a potential buyer to be able to choose size and color with a quantity option for each.
I have found some code that tallies the order total, but does not have the quantity option.
I have attached the code below. Can anyone steer me in the right direction on what to do with this? I am fairly new to customizing for PayPal, but I am a web designer so I am no beginner in figuring things out. I have also attached a screen shot of the customer's form she sent via email to show what she wants it to look like.
Any help would be greatly appreciated. Thanks.
alt text http://www.inauguraldayt-shirts.com/shirt.jpg
<!-- Start of Script -->
<SCRIPT type=text/javascript>
<!--
function Dollar (val) { // force to valid dollar amount
var str,pos,rnd=0;
if (val < .995) rnd = 1; // for old Netscape browsers
str = escape (val*1.0 + 0.005001 + rnd); // float, round, escape
pos = str.indexOf (".");
if (pos > 0) str = str.substring (rnd, pos + 3);
return str;
}function ReadForm (obj1, tst) { // process radio and checkbox
var i,j,amt=0,des="",obj,pos,val,tok,tag,
op1a="",op1b="",op2a="",op2b="",itmn="";
var ary = new Array ();
if (obj1.baseamt) amt = obj1.baseamt.value*1.0; // base amount
if (obj1.basedes) des = obj1.basedes.value; // base description
if (obj1.baseon0) op1a = obj1.baseon0.value; // base options
if (obj1.baseos0) op1b = obj1.baseos0.value;
if (obj1.baseon1) op2a = obj1.baseon1.value;
if (obj1.baseos1) op2b = obj1.baseos1.value;
if (obj1.baseitn) itmn = obj1.baseitn.value;
for (i=0; i<obj1.length; i++) { // run entire form
obj = obj1.elements[i]; // a form element
if (obj.type == "checkbox" || // checkboxes
obj.type == "radio") { // and radios
if (obj.checked) { // did user check it?
val = obj.value; // the value of the selection
ary = val.split (" "); // break apart
for (j=0; j<ary.length; j++) { // look at all items
// first we do single character tokens...
if (ary[j].length < 2) continue;
tok = ary[j].substring (0,1); // first character
val = ary[j].substring (1); // get data
if (tok == "#") amt = val * 1.0;
if (tok == "+") amt = amt + val*1.0;
if (tok == "%") amt = amt + (amt * val/100.0);
if (tok == "#") { // record item number
if (obj1.item_number) obj1.item_number.value = val;
ary[j] = ""; // zap this array element
}
// Now we do 3-character tokens...
if (ary[j].length < 4) continue;
tok = ary[j].substring (0,3); // first 3 chars
val = ary[j].substring (3); // get data
if (tok == "s1=") { // value for shipping
if (obj1.shipping) obj1.shipping.value = val;
ary[j] = ""; // clear it out
}
if (tok == "s2=") { // value for shipping2
if (obj1.shipping2) obj1.shipping2.value = val;
ary[j] = ""; // clear it out
}
}
val = ary.join (" "); // rebuild val with what's left tag = obj.name.substring (obj.name.length-2); // get flag
if (tag == "1a") op1a = op1a + " " + val;
else if (tag == "1b") op1b = op1b + " " + val;
else if (tag == "2a") op2a = op2a + " " + val;
else if (tag == "2b") op2b = op2b + " " + val;
else if (tag == "3i") itmn = itmn + " " + val;
else if (des.length == 0) des = val;
else des = des + ", " + val;
}
}
}
// Now summarize stuff we just processed, above
if (op1a.length > 0) obj1.on0.value = op1a;
if (op1b.length > 0) obj1.os0.value = op1b;
if (op2a.length > 0) obj1.on1.value = op2a;
if (op2b.length > 0) obj1.os1.value = op2b;
if (itmn.length > 0) obj1.item_number.value = itmn;
obj1.item_name.value = des;
obj1.amount.value = Dollar (amt);
if (obj1.tot) obj1.tot.value = "$" + Dollar (amt);
}
//-->
</SCRIPT>
<!-- End of Script -->
<div id="button">
<!-- Another method for the View Cart Form - add this code anywhere within the web page -->
<!-- This is the FORM to view the cart contents -->
<!-- Note: target="paypal" was replaced with the variable target="_self" -->
<!-- Note: shopping_url also added to code -->
<!-- These two changes allow better functionality with IE and Firefox -->
<form target="_self" action="https://www.paypal.com/cgi-bin/webscr" method="post" id="viewcart" name="viewcart">
<p>
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="display" value="1">
<input type="hidden" name="business" value="you#you.com">
<input type="hidden" name="shopping_url" value="http://www.yourwebsite.com/your_page.html">
</p>
</form>
<!-- End of the viewcart FORM -->
<!-- Start of Form -->
<!-- Note: target="paypal" was replaced with the variable target="_self" -->
<!-- Note: shopping_url also added to code -->
<!-- These two changes allow better functionality with IE and Firefox -->
<FORM onsubmit="this.target='_self';
return ReadForm(this, true);" action=https://www.paypal.com/cgi-bin/webscr method=post>
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="business" value="you#you.com">
<input type="hidden" name="item_name" value="">
<input type="hidden" name="amount" value="">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="bn" value="PP-ShopCartBF">
<input type="hidden" name="shopping_url" value="http://www.yourwebsite.com/your_page.html">
<input type="hidden" name="cancel_return" value="http://www.yourwebsite.com/Cancel.html">
<input type="hidden" name="return" value="http://www.yourwebsite.com/Success.html">
<input type="hidden" value="0.00" name="baseamt">
<input type="hidden" value="Inaugural Day T-Shirt - #17.99" name="basedes">
Inaugural Day T-Shirt - $17.99
<br><br>
Colors
<br><br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="White +17.99"> White - $17.99
Select Quantity:
<select name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="Blue +17.99"> Blue - $17.99
<br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="Black +17.99"> Black - $17.99
<br><br><br>
Sizes
<br><br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="Small +17.99"> Small - $17.99
<br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="Medium +17.99"> Medium - $17.99
<br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="Large +17.99"> Large - $17.99
<br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="XL +19.99"> XL - $19.99
<br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="XXL +19.99"> XXL - $19.99
<br>
<input onclick="ReadForm (this.form, false);" type=checkbox value="XXXL +19.99"> XXXL - $19.99
<br><br><br>
<input onclick="this.form.reset ();" type=button value="Reset Form"> Item Total >
<input class=nbor size=6 value=$0.00 name=tot>
<br><br>
<input type=image alt="cart add" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" name=submit>
<br><br>
<input onclick="document.forms.viewcart.target = '_self';
document.forms.viewcart.submit ();
return false;" type=image alt="cart view" src="https://www.paypal.com/en_US/i/btn/btn_viewcart_LG.gif"">
</FORM>
<!-- End of Form -->
For a basic HTML page, use PayPal's own button wizard. Just go to Merchant Services -> Add to Cart button. It will allow you to customize the button to specify different sizes via a drop down and even select a quantity when the item is added to the cart. You can also add a "view cart" button as well and everything is taken care of for you. I used this on a recent site.
Granted adding items and viewing the cart takes the user to a PayPal page, but unless they're looking for a fully integrated site, this option is fast, easy, and robust. Plus PayPal does a good job at tracking where the order page is coming from so the user can easily navigate back.
Related
I have created an initial version, however you have to type the individual inputs for each output. I want to be able to just type into one input box and then it calculates all other outputs. How would i do this?
This is the code i have currently, im very very new to this :)
<form onsubmit="return false" oninput="o.value = (a.valueAsNumber /0.7).toFixed(0)" <br>
NHS <input name="a" id="a" type="number" step="any" placeholder="NHS" <br> TO <input name="o" id="o" type="number" for="a" placeholder="RETAIL"></output> Retail
</form>
<form onsubmit="return false" oninput="o.value = (a.valueAsNumber *1).toFixed(0)" <br>
NHS <input name="a" id="a" type="number" step="any" placeholder="NHS" <br> TO <input name="o" id="o" type="number" for="a" placeholder="Schools & CDC's"></output> Schools & CDC's
</form>
<form onsubmit="return false" oninput="o.value = (a.valueAsNumber / 0.7 -(a.valueAsNumber / 0.7 / 100*40)).toFixed(0)" <br>
NHS <input name="a" id="a" type="number" step="any" placeholder="NHS" <br> TO <input name="o" id="o" type="number" for="a" placeholder="Trade - Band A" Math.round></output> Trade - Band A
</form>
<form onsubmit="return false" oninput="o.value = (a.valueAsNumber *1).toFixed(0)" <br>
NHS <input name="a" id="a" type="number" step="any" placeholder="NHS" <br> TO <input name="o" id="o" type="number" for="a" placeholder="Trade - Band B"></output> Trade - Band B
</form>
<form onsubmit="return false" oninput="o.value = (a.valueAsNumber / 0.7 -(a.valueAsNumber / 0.7 / 100*20)).toFixed(0)" <br>
NHS <input name="a" id="a" type="number" step="any" placeholder="NHS" <br> TO <input name="o" id="o" type="number" for="a" placeholder="Trade - Band C" Math.round></output> Trade - Band C
</form>
<form onsubmit="return false" oninput="o.value = (a.valueAsNumber / 0.7 -(a.valueAsNumber / 0.7 / 100*10)).toFixed(0)" <br>
NHS <input name="a" id="a" type="number" step="any" placeholder="NHS" <br> TO <input name="o" id="o" type="number" for="a" placeholder="Trade - Band D" Math.round></output> Trade - Band D
</form>
https://jsfiddle.net/5gpLbj80/2/
You can use the same input to update all your fields, no need to use a form.
const calc = () => {
let nhs = document.getElementById('nhs').value;
document.getElementById('retail').innerHTML = (nhs / 0.7).toFixed(0);
document.getElementById('school').innerHTML = (nhs * 1).toFixed(0);
//If you want an input:
document.getElementById('retailInput').value = (nhs / 0.7).toFixed(0);
document.getElementById('schoolInput').value = (nhs * 1).toFixed(0);
}
NHS<input id='nhs' oninput='calc()'><br>
<span>Retail:</span><span id='retail'></span><br>
<span>School:</span><span id='school'></span><br>
<span>Retail input:</span><input id='retailInput' disabled='true' /><br>
<span>Retail input:</span><input id='schoolInput' disabled='true' />
First piece of advice would be to remove all of those inline event handlers.
Attach a named function, then do all of your grunt-work inside it.
By using addEventListener to connect the two, the function 'knows' which element it's attached to. That is to say, we can simply use this.value to get it's value, rather than having to use document.getElementById('nhs').value
"use strict;"
function byId(id){return document.getElementById(id)}
window.addEventListener('load', onLoaded, false);
function onLoaded(evt)
{
let input = byId('myInput');
input.addEventListener('input', onInputReceived, false);
}
function onInputReceived(evt)
{
let outputs = [byId('output1'), byId('output2'), byId('output3')];
let value = parseFloat(this.value);
if (!isNaN(value))
{
outputs[0].textContent = value * 2;
outputs[1].textContent = value * 3;
outputs[2].textContent = parseInt( (value * 3)/4 );
}
else
{
outputs[0].textContent =
outputs[1].textContent =
outputs[2].textContent = "*invalid input (enter a number)*";
}
}
<label>Enter data: <input id='myInput'/></label><br>
<ul>
<li id='output1'></li>
<li id='output2'></li>
<li id='output3'></li>
</ul>
Alternate snippet
<!doctype html>
<html>
<head>
<script>
"use strict;"
function byId(id){return document.getElementById(id)}
window.addEventListener('load', onLoaded, false);
function onLoaded(evt)
{
let input = byId('myInput');
input.addEventListener('input', onInputReceived, false);
}
function onInputReceived(evt)
{
let outputs = [byId('output1'), byId('output2'), byId('output3')];
let value = parseFloat(this.value);
if (!isNaN(value))
{
outputs[0].textContent = value * 2;
outputs[1].textContent = value * 3;
outputs[2].textContent = parseInt( (value * 3)/4 );
}
else
{
outputs[0].textContent =
outputs[1].textContent =
outputs[2].textContent = "*invalid input (enter a number)*";
}
}
</script>
</head>
<body>
<label>Enter data: <input id='myInput'/></label><br>
<ul>
<li id='output1'></li>
<li id='output2'></li>
<li id='output3'></li>
</ul>
</body>
</html>
I have a simple product selection form where user gets to select how many products to buy. Based on his selection, I would need to append the URL with his selection. If he selects Product1 and Product2 the submit button should add those two products to the cart with the following parameters https://example.com?add-to-cart=10,20
Here is example of my form
<form action="/action_page.php">
<input type="checkbox" id="10" name="vehicle1" value="Bike">
<label for="vehicle1"> Product 1</label><br>
<input type="checkbox" id="20" name="vehicle2" value="Car">
<label for="vehicle2"> Product 2</label><br>
<input type="checkbox" id="30" name="vehicle3" value="Boat">
<label for="vehicle3"> Product 3</label><br><br>
<input type="submit" value="Submit">
</form>
How can I achieve this?
Found something similar here but don't know how to modify it per my example.
This will redirect to example.com?add-to-cart=10,20
<script>
$('form').on('submit', function(e){
e.preventDefault();
let params = ''
const redirect_url = 'axample.com?add-to-cart'
let count = $('input[name^="vehicle"]:checked').length
$('input[name^="vehicle"]:checked').each(function(index) {
params += $(this).attr('id')
if(index !== count - 1) {
params += ','
}
});
// Amend redirect url in form by adding fields from form
window.location.href = redirect_url + '=' + params
});
<script>
I'm working on a pure C99 practising project which can do a login simulation for our school's CAS login system.
Now I'm trying to use Gumbo HTML parser to parse our school's login page. Here is the form section and I need to get the login ticket from it before I run the POST request to submit the form, which is the "hidden" type input element with name "lt". (i.e. line with <input type="hidden" name="lt" value="LT-000000-b4LktCXyzXyzXyzXyzXyzXyz" />, and I need to parse the "value").
I've wrote some code, but it seems cannot find out this input element. Here is my C program's function:
const char * parse_login_ticket_old(char * raw_html)
{
// Parse HTML into Gumbo memory structure
GumboOutput * gumbo_output = gumbo_parse(raw_html);
// Prepare the node
GumboNode * gumbo_root = gumbo_output->root;
assert(gumbo_root->type == GUMBO_NODE_ELEMENT);
assert(gumbo_root->v.element.children.length >= 2);
const GumboVector* root_children = &gumbo_root->v.element.children;
GumboNode* page_body = NULL;
for (int i = 0; i < root_children->length; ++i)
{
GumboNode* child = root_children->data[i];
if (child->type == GUMBO_NODE_ELEMENT && child->v.element.tag == GUMBO_TAG_BODY)
{
page_body = child;
break;
}
}
assert(page_body != NULL);
GumboVector* page_body_children = &page_body->v.element.children;
for (int i = 0; i < page_body_children->length; ++i)
{
GumboNode* child = page_body_children->data[i];
GumboAttribute * input_name_attr = gumbo_get_attribute(&child->v.element.attributes, "name");
if (child->type == GUMBO_NODE_ELEMENT && child->v.element.tag == GUMBO_TAG_INPUT && strcmp(input_name_attr->value, "lt") == 0)
{
GumboAttribute * input_value_attr = gumbo_get_attribute(&child->v.element.attributes, "value");
return input_name_attr->value;
}
}
return NULL;
}
In case someone need for debugging, here is a example of our schools page. Possible sensitive data has been removed.
<body>
<div id="wrapper">
<div id="contentArea" role="main">
<div class="form login" role="form">
<h2 class="hidden">Login</h2>
<form id="fm1" class="fm-v clearfix" action="/schoolcas/login?jsessionid=1234567890" method="post"><div class="formRow">
<label for="username" class="label">Student ID</label>
<div class="textBox">
<input id="username" name="username" class="schoolcas text" aria-required="true" type="text" value="" size="25" maxlength="25"/></div>
</div>
<div class="formRow">
<label for="password" class="label">Password</label>
<div class="textBox">
<input id="password" name="password" class="schoolcas text" aria-required="true" type="password" value="" size="25" autocomplete="off"/></div>
</div>
<div class="formRow">
<input type="hidden" name="lt" value="LT-000000-b4LktCXyzXyzXyzXyzXyzXyz" />
<input type="hidden" name="execution" value="e2s1" />
<input type="hidden" name="_eventId" value="submit" />
<input class="button grey submit" name="submit" value="Login" type="submit" />
</div>
</form>
</div>
</div>
</div>
</body>
Anyway, my program seems just stop at the top of the body element and it returns NULL later on.
So I would like to know how to do a correct search, and find out the input element I need?
I've figured it out by myself from the Google sample code (https://github.com/google/gumbo-parser/blob/master/examples/find_links.cc).
Here's the code. It's crappy but it works anyway.
const char * find_attribute(GumboNode * current_node, GumboTag element_tag_type,
char * element_term_key, char * element_term_value, char * desired_result_key)
{
const char * lt_token = NULL;
// Return NULL if it is in WHITESPACE
if (current_node->type != GUMBO_NODE_ELEMENT)
{
return NULL;
}
// Set the element's term key,
// e.g. if we need to find something like <input name="foobar"> then element search term key is "name",
// and element search value is "foobar"
GumboAttribute* lt_attr = gumbo_get_attribute(¤t_node->v.element.attributes, element_term_key);
if (lt_attr != NULL && current_node->v.element.tag == element_tag_type && (strcmp(lt_attr->value, element_term_value) == 0))
{
lt_token = gumbo_get_attribute(¤t_node->v.element.attributes, desired_result_key)->value;
return lt_token;
}
GumboVector* children = ¤t_node->v.element.children;
for (unsigned int i = 0; i < children->length; ++i)
{
lt_token = find_attribute(children->data[i], element_tag_type,
element_term_key, element_term_value, desired_result_key);
// Force stop and return if it gets a non-null result.
if(lt_token != NULL)
{
return lt_token;
}
}
}
I recently made a Drop Down type form where a user can select one of the listed items and hit "Add to Cart" where it brings them to another page.
Is it possible to change this into a Radio type form where they can just select which one they would like and hit "Add to Cart" instead of a drop down type? I looked through almost every page on Google and all the Radio Type help sections basically just writes down the information in a file. I don't need anything that writes in a file, just basically redirects to another page when a user selects a choice and hit "Add to cart:
My current Drop Down menu form is:
<form>
<p align="center"><b>Select a Payment:</b>
<select id="setit" style="color: #000" size="1" name="test" onchange="displayValue();">
<option value="/">Select one</option>
<option value="/">1 Month: $4.99</option>
<option value="/">3 Months: $14.99</option>
<option value="/">6 Months: $29.99</option>
</select>
<br />
<div id="displayValue"></div>
<br />
<center><input type="button" value="Add to Cart" onclick="window.open(setit.options[setit.selectedIndex].value)"></center>
</p>
</form>
Thank you for your time!
Yes, you can. Try this:
HTML
<form>
<p align="center">
<b>Select a Payment:</b>
<input type="radio" id="setit1" name="setit" value="1" /><label for="setit1">1 Month: $4.99</label>
<input type="radio" id="setit2" name="setit" value="3" /><label for="setit2">3 Months: $14.99</label>
<input type="radio" id="setit3" name="setit" value="6" /><label for="setit3">6 Months: $29.99</label>
<br />
<div id="displayValue"></div>
<br />
<center><input type="button" id="button-add-to-cart" value="Add to Cart"></center>
</p>
</form>
JS
var app = {
payments: {
option1: "1 Month: $4.99",
option3: "3 Months: $14.99",
option6: "6 Months: $29.99"
},
init: function () {
var button = document.getElementById( 'button-add-to-cart' ),
radios = document.forms[0].setit,
i = 0;
// This handles the button click
button.onclick = function ( e ) {
var selected = app.getCheckedRadio( document.forms[0].setit );
if ( selected ) {
window.open( '//someurl.com?val=' + selected.value ); // Open window
return false;
}
alert( "No payment selected." ); // Else notify user
};
// This handles the selection change
for ( ; i < radios.length; i++ ) {
radios[ i ].onclick = app.radioChange;
}
},
getCheckedRadio: function ( radio_group ) {
for ( var i = 0; i < radio_group.length; i++ ) {
var button = radio_group[ i ];
if ( button.checked ) {
return button;
}
}
return undefined;
},
radioChange: function ( e ) {
var display = document.getElementById( 'displayValue' ),
radio = app.getCheckedRadio( document.forms[0].setit ),
value = radio.value;
display.innerHTML = app.payments[ "option" + value ];
}
};
window.load = app.init();
Try this:
<form name="form1">
<p align="center">
<b>Select a Payment:</b>
<input type="radio" name="setit" value="1" /><label for="setit1">1 Month: $4.99</label>
<input type="radio" name="setit" value="2" /><label for="setit2">3 Months: $14.99</label>
<input type="radio" name="setit" value="3" /><label for="setit3">6 Months: $29.99</label>
<br />
<div id="displayValue"></div>
<br />
<center><input type="button" value="Add to Cart" onclick="window.open(GetRadioValue())"></center>
</p>
</form>
You need a js function to get the checked radio's value:
<script type="text/javascript">
function GetRadioValue() {
len = document.form1.setit.length;
for (i = 0; i <len; i++) {
if (document.form1.setit[i].checked) {
return document.form1.setit[i].value;
}
}
}
</script>
This is in file: DrinkOrder.asp
<form action="DrinkResult.asp" method="post">
Drink:
<select name="drink">
<option>Coffee</option>
<option>Tea</option>
<option>Hot Chocolate</option>
</select>
<p/>
Sugar:
<input type="radio" name="sugar" value="1"> 1
<input type="radio" name="sugar" value="2"> 2
<input type="radio" name="sugar" value="3"> 3
<p/>
Milk: <input type="checkbox" name="milk">
<p/>
<input type="submit" value="Submit Order"><input type="reset" value="Reset">
</form>
This is in file: DrinkResult.asp
<%#language="Javascript"%>
<%
function milkOn(form){
var with = "";
if(form == "1") { with = "With milk"; }
else { with = "No milk"; }
return with;
}
%>
<%=("<table border=\"1\">")%>
<%=("<tr><th><i>Drink:</i></th>" + "<td> " + (Request.Form("drink")) + "</td>")%>
<%=("<tr><th><i>Sugar:</i></th>" + "<td> " + (Request.Form("sugar")) + "</td>")%>
<%=("<tr><th><i>Milk:</i></th>" + "<td>" + (milkOn(Request.Form("milk"))) + "</td>")%>
<%=("</table>")%>
After submitting form from DrinkOrder.asp to DrinkResult.asp, I received an error which clearly I don't know how to fix it.
After a few modification I made to detect the error location, I'm pretty sure it is located at milkOn(Request.Form("milk")) in file DrinkResult.asp because when I remove the statement, the result shows up.
What's wrong with my code here?
change
if(form == "1")
to
if(form == "on")
otherwise the code seems to test fine for me.