/    Sign up×
Community /Pin to ProfileBookmark

I am trying to learn javascript via Tutorials online and have run into a problem I can’t seem to resolve. I have a form in my HTML Code that has two variables – item_number is originally set to ” ” and amount is set to “0”. A third variable is os0, and want to create a FUNCTION that can be accessed to seperate os0 at the “$” and put the first part in item_number and the last into amount – – CAN’T SEEM TO GET MY CODING TO WORK

The HTML Code is
<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post” target=”_blank”>
<input type=”hidden” name=”bn” value=”AMPPFPWZ.200″>
<input type=”hidden” name=”cmd” value=”_
cart”>
<input type=”hidden” name=”add” value=”1″>

<input type=”hidden” name=”currency_code” value=”USD”>
<input type=”hidden” name=”no_note” value=”1″>
<input type=”hidden” name=”image_url” value=””>
<input type=”hidden” name=”no_shipping” value=”0″>
<input type=”hidden” name=”undefined_quantity” value=”0″>
<input type=”hidden” name=”amount” value=”0″>
<input type=”hidden” name=”item_number” value=” “>
<!–webbot bot=”Include” U-Include=”../includes/paypal_email.htm” TAG=”BODY” –><tr>
<td class=’product_field’ nowrap>Product&nbsp;Name:</td>
<td>
<input readonly class=’product_attribute’ type=”text” name=”item_name” value=”Product Test” width=20 size=”18″></td>
<td align=right nowrap>
<!–webbot bot=”Include” U-Include=”../includes/cart_buttons.htm” TAG=”BODY” –></td>
</tr>
<tr>
<td><input type=”hidden” name=”on0″ value=”Model”><b>Model</b></td>
<td>
<select name=”os0″ size=”1″>
<option value=”Item 1 $50.00″>Item 1 $50.00
<option value=”Item 2 $75.00″>Item 2 $75.00
</select>

<!– NEED TO INCLUDE SCRIPT FUNCTION HERE –>

</td>
</tr>
<tr>
<td class=’product_field’>Product Description:</td>
<td colspan=2 class=’product_description’>Test
Product</td>
</tr>
</form>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorDec 04.2006 — First at all: take care how do you use the HTML and javascript terms. A form can not have [I]variables[/I] (a variable means something else), it has [I]controls (elements)[/I]. The form's elements have [I]names[/I] and [I]values[/I].

Thus you have some form's elements with the names: 'item_number','amount' and 'os0'.

Your problem is to change the values of the first two elements according to the value of the third. The value of the 'os0' named element must be splited upon the '$' character. 'item_number' element will take the first part of the string as value, while 'amount' element will take the second part of the split.

You have another problem. As the default values for those two hidden inputs are " " and "0", your select element must have also a first option with those values combined as a [B]value[/B].

When clear the aim like this, the solve is simple

Now, here's a simplified code for you. Note that I have used a type="text" for you to see the changes. You may put the javascript anywhere, if you can not put it in the HEAD section:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
var names=['item_number','amount'];
function changeVal(v){
for(var i=0;i&lt;names.length;i++){
document.getElementsByName(names[i])[0].value=v.split('$')[i];
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
&lt;input type="text" name="amount" value="0"&gt;
&lt;input type="text" name="item_number" value=" "&gt;
&lt;select name="os0" size="1" onchange="changeVal(this.value)"&gt;
&lt;option value=" $0"&gt;-- choose an item --&lt;/option&gt;
&lt;option value="Item 1 $50.00"&gt;Item 1 $50.00&lt;/option&gt;
&lt;option value="Item 2 $75.00"&gt;Item 2 $75.00&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@biloxitjauthorDec 04.2006 — Thanks very much KOR - Of course your right - after 40 years, calling something a variable is a matter of habit and I generally only have to answer to myself. IT WORKS GREAT - AGAIN THANKS
Copy linkTweet thisAlerts:
@KorDec 05.2006 — Oh, yes. Everything [I]can be turned[/I] into a variable (or an object in OOP), I know. But that does not mean they [I]are[/I] variables. Or better say, they might be variables as well, but this potential property is not useful when referencing objects or group of objects, classes and soub-classes. "variables" is a much to generalized group.

You are welcome. Best regards!
Copy linkTweet thisAlerts:
@biloxitjauthorDec 07.2006 — KOR - - iI really hate to be a pest but still need help on this problem

I copied the code as you suggested below and included it my my script

var names=['item_number','amount'];

function changeVal(v){

for(var i=0;i<names.length;i++){

document.getElementsByName(names[i])[0].value=v.split('$')[i];

}

}



AND changed my code accoringly (Below) - - EVERYTHING WORKS PERFECTLY (Well - Almost). IT WORKS PERFECTLY FOR THE FIRST PRODUCT ON MY PAGE - HOWEVER, IF I COPY THE "EXACT" SAME CODING AND USE IT FOR THE NEXT PRODUCT ON MY PAGE THEN I ALWAYS GET A amount OF "0" AND item_number OF "INVALID SELECTION" REGARDESS OF WHAT ITEM IS SELECTED.



I have tryed for 2 days now to figure this out !!!!





<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal">



<input type="hidden" name="bn" value="AMPPFPWZ.200">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="image_url" value="">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="undefined_quantity" value="0">
<input type="hidden" name="amount" value="0">

<!--webbot bot="Include" U-Include="../includes/paypal_email.htm" TAG="BODY" --><tr>
<td class='product_field' nowrap>Product&nbsp;Name:</td>
<td valign="top">
<input readonly class='product_attribute' type="text" name="item_name" value="Test Product" width=20 size="30"></td>
<td align=right nowrap valign="top">
<!--webbot bot="Include" U-Include="../includes/cart_buttons1.htm" TAG="BODY" --></td>
</tr>

<tr>
<input type="hidden" class='product_attribute' type="text" name="item_number" value="Invalid Selection" width=20 size="18">

<!-- INITIAL VALUE FOR item_number IN CASE SOMEONE TRYS TO ADD WITHOUT SELECTING -->


<td height="42"><input type="hidden" name="on0" value="Model"><b>Model</b></td>

<td height="42">

<select name="os0" size="1" onchange="changeVal(this.value)">

<option value="Invalid Selection $0">-- Please Select a Model --</option>

<option value="item 1 $190.00">item 1 - $190.00</option>

<option value="item 2 $262.00">item 2 $262.00</option>

</select>

<input type="hidden" name="on0" value=" ">

<input type="hidden" name="os0" value=" ">

<!-- I NULL OUT on0 and os0 SO THEY DON'T APPEAR ON THE PAYPAL CART -->

<!-- THIS CODE WORKS PERFECTLY FOR ONLY THE FIRST PRODUCT - - >

</td>

</tr>

</form>
Copy linkTweet thisAlerts:
@KorDec 07.2006 — Should work. What is wrong?
Copy linkTweet thisAlerts:
@biloxitjauthorDec 07.2006 — KOR - I can't figure it out - been working on it for days - - It works exactly correct for the 1st product on the page - - but doesn't for the second. FOR a test - I copied the code for the 1st product and pasted it to reflect the second product. try it and see what happens - All of the code is below.

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>Product Name</title>

</head>

<script type="text/javascript">

var names=['item_number','amount'];

function changeVal(v){

for(var i=0;i<names.length;i++){

document.getElementsByName(names[i])[0].value=v.split('$')[i];

}

}

</script>





<body>





<table border="0" width="100%" cellpading=0 cellspacing=0 id="table18">



<td valign=top>
<table cellpadding=2 cellspacing=2 border=0 width=100% id="table19">
<!-- BEGIN PAYPAL FORM --> <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal">

<input type="hidden" name="on0" value=" ">
<input type="hidden" name="bn" value="AMPPFPWZ.200">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="image_url" value="">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="undefined_quantity" value="0">
<input type="hidden" name="amount" value="0">

<input type="hidden" name="business" value="[email protected]" >
<tr>
<td class='product_field' nowrap>Product&nbsp;Name:</td>
<td valign="top">
<input readonly class='product_attribute' type="text" name="item_name" value="Product" width=20 size="30"></td>
<td align=right nowrap valign="top">
<a href="#" onclick="window.open('https://www.paypal.com/cart/display=1&[email protected]','paypal','width=200,height=133,scrollbars,location,resizable,status');"><img src="../template_dwt/graphics/view_cart_button.jpg" border=0></a>&nbsp;&nbsp;&nbsp; <input type=image src="../template_dwt/graphics/add_to_cart_button.jpg">

</td>
</tr>

<tr>
<input type="hidden" class='product_attribute' type="text" name="item_number" value="Invalid Selection" width=20 size="18">


<td height="42"><input type="hidden" name="on0" value="Model"><b>Model</b></td>

<td height="42">

<select name="os0" size="1" onchange="changeVal(this.value)">

<option value="Invalid Selection $0">-- Please Select a Model --</option>

<option value="Item 1 $190.00">Item 1 - $190.00</option>

<option value="Item 2 $262.00">Item 2 $262.00</option>

</select>

<input type="hidden" name="on0" value=" ">

<input type="hidden" name="os0" value=" ">

</td>

</tr>

<!-- END PAYPAL FORM --> </form>

</table>
</td>
</tr>
</table>



<table border="0" width="100%" cellpading=0 cellspacing=0 id="table18">

<td valign=top>
<table cellpadding=2 cellspacing=2 border=0 width=100% id="table19">
<!-- BEGIN PAYPAL FORM --> <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal">

<input type="hidden" name="on0" value=" ">
<input type="hidden" name="bn" value="AMPPFPWZ.200">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="image_url" value="">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="undefined_quantity" value="0">
<input type="hidden" name="amount" value="0">

<input type="hidden" name="business" value="[email protected]" >
<tr>
<td class='product_field' nowrap>Product&nbsp;Name:</td>
<td valign="top">
<input readonly class='product_attribute' type="text" name="item_name" value="Product" width=20 size="30"></td>
<td align=right nowrap valign="top">
<a href="#" onclick="window.open('https://www.paypal.com/cart/display=1&[email protected]','paypal','width=200,height=133,scrollbars,location,resizable,status');"><img src="../template_dwt/graphics/view_cart_button.jpg" border=0></a>&nbsp;&nbsp;&nbsp; <input type=image src="../template_dwt/graphics/add_to_cart_button.jpg">

</td>
</tr>

<tr>
<input type="hidden" class='product_attribute' type="text" name="item_number" value="Invalid Selection" width=20 size="18">


<td height="42"><input type="hidden" name="on0" value="Model"><b>Model</b></td>

<td height="42">

<select name="os0" size="1" onchange="changeVal(this.value)">

<option value="Invalid Selection $0">-- Please Select a Model --</option>

<option value="Item 1 $190.00">Item 1 - $190.00</option>

<option value="Item 2 $262.00">Item 2 $262.00</option>

</select>

<input type="hidden" name="on0" value=" ">

<input type="hidden" name="os0" value=" ">

</td>

</tr>

<!-- END PAYPAL FORM --> </form>

</table>
</td>
</tr>
</table>


<p></p>

<p></p>

</body>

</html>
×

Success!

Help @biloxitj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...