Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

Using Jquery to populate hidden form field for Paypal integration.

Hi-

I work fo a non-profit that runs a couple of therapeutic day schools for children with emotional and behavioral needs.

As part of our back to school appeal I've built a drag and drop shopping cart for a 'virtual school supply drive' that enables folks to place 'school supplies' into a 'backpack.' The jquery that I have allows for the drag and drop of the different items and updates the 'total.'

I would like to add to that script so that the value generated for the 'total' is also added into the code for a paypal button so that I can use that service to process monetary donations to our school program. What I want is for thew value of .total to be entered into the hidden amount field as the value so that paypal can process it.

Here's the jquery:

<script>
		var data = {"total":0,"rows":[]};
		var totalCost = 0;
		
		$(function(){
			$('#cartcontent').datagrid({
				singleSelect:true
			});
			$('.item').draggable({
				revert:true,
				proxy:'clone',
				onStartDrag:function(){
					$(this).draggable('options').cursor = 'not-allowed';
					$(this).draggable('proxy').css('z-index',10);
				},
				onStopDrag:function(){
					$(this).draggable('options').cursor='move';
				}
			});
			$('.cart').droppable({
				onDragEnter:function(e,source){
					$(source).draggable('options').cursor='auto';
				},
				onDragLeave:function(e,source){
					$(source).draggable('options').cursor='not-allowed';
				},
				onDrop:function(e,source){
					var name = $(source).find('p:eq(0)').html();
					var price = $(source).find('p:eq(1)').html();
					addProduct(name, parseFloat(price.split('$')[1]));
				}
			});
		});
		
		function addProduct(name,price){
			function add(){
				for(var i=0; i<data.total; i++){
					var row = data.rows[i];
					if (row.name == name){
						row.quantity += 1;
						return;
					}
				}
				data.total += 1;
				data.rows.push({
					name:name,
					quantity:1,
					price:price
				});
			}
			add();
			totalCost += price;
			$('#cartcontent').datagrid('loadData', data);
			$('div.cart #total').html('$'+totalCost);
}
		
	</script>

Open in new window

0
jcourtice
Asked:
jcourtice
  • 2
1 Solution
 
Julian HansenCommented:
Is it not as simple as the following (comments inline)

...
// After this line
$('div.cart #total').html('$'+totalCost);

// Where amount is the name of the paypal button?
$('input[name="amount"]').val(totalCost);
...

Open in new window

0
 
jcourticeAuthor Commented:
Thank you so much. I'm more a designer than a developer so anything beyond manipulating existing code usually takes me about 10x longer than it should.
0
 
Julian HansenCommented:
No problem - any time. I work a lot with designers who don't want to code so familiar with how it works.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now