Passing HTML array index to JS?

Passing HTML array index to JS?

am 08.12.2009 00:02:39 von Skip Evans

Hey all,

I have an HTML field like this

style="text-align: right;" onblur="calculateBidUnit();">

.... and what I need to do is pass to the calculateBidUnit
function the value of quantity, do a calculation on it and
plug into this field.



Which of course I know how to do for non-array values, but not
sure how to get the values to do the calculation on the JS
side if the fields are in an array.

Any help, as always, is greatly appreciated,
Skip

--
====================================
Skip Evans
PenguinSites.com, LLC
503 S Baldwin St, #1
Madison WI 53703
608.250.2720
http://penguinsites.com
------------------------------------
Those of you who believe in
telekinesis, raise my hand.
-- Kurt Vonnegut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Passing HTML array index to JS?

am 08.12.2009 00:56:44 von Philip Thompson

On Dec 7, 2009, at 5:02 PM, Skip Evans wrote:

> Hey all,
>=20
> I have an HTML field like this
>=20
> style=3D"text-align: right;" onblur=3D"calculateBidUnit();">
>=20
> ... and what I need to do is pass to the calculateBidUnit function the =
value of quantity, do a calculation on it and plug into this field.
>=20
>
>=20
> Which of course I know how to do for non-array values, but not sure =
how to get the values to do the calculation on the JS side if the fields =
are in an array.
>=20
> Any help, as always, is greatly appreciated,
> Skip

This question is probably more appropriate for a JS list... but I'm not =
mean, so I'll let you know. The easiest way would be to update the blur =
event qty input:

onblur=3D"calculateBidUnit(this.value);"

Now you have the value sent to that function. Another way would be to =
give your input and output an id each - let's say 'qty' and =
'bid_unit_value', respectively. Then reference those ids in your =
function. This gives you the following:

onblur=3D"calculateBidUnit();" id=3D"qty" />
id=3D"bid_unit_value" />



I'll give you a small warning. All browsers are not alike, so my =
recommendation would be to use a library that handles the cross-browser =
compatibility portion of javascript (I use Mootools). Nonetheless, the =
above *should* work. Learning the bare bones of javascript will help =
with the more complicated stuff and you'll be smarter for it! =3DP

Hope that helps,
~Philip=

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Passing HTML array index to JS?

am 08.12.2009 01:32:03 von Skip Evans

Hey Philip,

But will that ID value identify the right member of each
array? I thought about that but just assumed that it would not.

Skip

Philip Thompson wrote:
> On Dec 7, 2009, at 5:02 PM, Skip Evans wrote:
>
>> Hey all,
>>
>> I have an HTML field like this
>>
>>
>>
>> ... and what I need to do is pass to the calculateBidUnit function the value of quantity, do a calculation on it and plug into this field.
>>
>>
>>
>> Which of course I know how to do for non-array values, but not sure how to get the values to do the calculation on the JS side if the fields are in an array.
>>
>> Any help, as always, is greatly appreciated,
>> Skip
>
> This question is probably more appropriate for a JS list... but I'm not mean, so I'll let you know. The easiest way would be to update the blur event qty input:
>
> onblur="calculateBidUnit(this.value);"
>
> Now you have the value sent to that function. Another way would be to give your input and output an id each - let's say 'qty' and 'bid_unit_value', respectively. Then reference those ids in your function. This gives you the following:
>
>
>
>
>
>
> I'll give you a small warning. All browsers are not alike, so my recommendation would be to use a library that handles the cross-browser compatibility portion of javascript (I use Mootools). Nonetheless, the above *should* work. Learning the bare bones of javascript will help with the more complicated stuff and you'll be smarter for it! =P
>
> Hope that helps,
> ~Philip

--
====================================
Skip Evans
PenguinSites.com, LLC
503 S Baldwin St, #1
Madison WI 53703
608.250.2720
http://penguinsites.com
------------------------------------
Those of you who believe in
telekinesis, raise my hand.
-- Kurt Vonnegut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Passing HTML array index to JS?

am 08.12.2009 04:07:22 von Philip Thompson

On Dec 7, 2009, at 6:32 PM, Skip Evans wrote:

> Hey Philip,
>=20
> But will that ID value identify the right member of each array? I =
thought about that but just assumed that it would not.
>=20
> Skip
>=20
> Philip Thompson wrote:
>> On Dec 7, 2009, at 5:02 PM, Skip Evans wrote:
>>> Hey all,
>>>=20
>>> I have an HTML field like this
>>>=20
>>> style=3D"text-align: right;" onblur=3D"calculateBidUnit();">
>>>=20
>>> ... and what I need to do is pass to the calculateBidUnit function =
the value of quantity, do a calculation on it and plug into this field.
>>>=20
>>>
>>>=20
>>> Which of course I know how to do for non-array values, but not sure =
how to get the values to do the calculation on the JS side if the fields =
are in an array.
>>>=20
>>> Any help, as always, is greatly appreciated,
>>> Skip
>> This question is probably more appropriate for a JS list... but I'm =
not mean, so I'll let you know. The easiest way would be to update the =
blur event qty input:
>> onblur=3D"calculateBidUnit(this.value);"
>> Now you have the value sent to that function. Another way would be to =
give your input and output an id each - let's say 'qty' and =
'bid_unit_value', respectively. Then reference those ids in your =
function. This gives you the following:
>> onblur=3D"calculateBidUnit();" id=3D"qty" />
>> id=3D"bid_unit_value" />
>>
>> I'll give you a small warning. All browsers are not alike, so my =
recommendation would be to use a library that handles the cross-browser =
compatibility portion of javascript (I use Mootools). Nonetheless, the =
above *should* work. Learning the bare bones of javascript will help =
with the more complicated stuff and you'll be smarter for it! =3DP
>> Hope that helps,
>> ~Philip

Each text input will only contain 1 value... and contain 1 unique "id." =
So, if you have multiple input boxes with the same name (qty[]), only =
the $_POST output will contain an array of each value. See below...

onblur=3D"calc(this.value);" />
onblur=3D"calc(this.value);" />
onblur=3D"calc(this.value);" />





When you blur qty1, buv will receive the value 1; blur qty2, buv will be =
2; and so on. The last input box that you blur on will determine buv's =
final value (unless you throw in some logic to handle this)...



So, let's say you blur on qty2 and then submit the form, your PHP script =
will have this in $_POST...

Array
(
[qty] =3D> Array
(
[0] =3D> 1
[1] =3D> 2
[3] =3D> 3
)
[bid_unit_value] =3D> Array
(
[0] =3D> 2
)
)

If this is not what you're wanting, you will need to modify the behavior =
above. If you're wanting the bid_unit_value to contain all the values of =
each blur, you may want to do something like this...



Which may result from each blur in $_POST to be...

Array
(
[qty] =3D> Array
(
[0] =3D> 1
[1] =3D> 2
[3] =3D> 3
)
[bid_unit_value] =3D> Array
(
[0] =3D> 2|1|3
)
)

Or something along those lines. Then you can just explode() on that =
first index to get individual values. Maybe you can clarify your =
ultimate goal and we can assist you in what the best plan is.

Hope that helps,
~Philip=

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Passing HTML array index to JS?

am 08.12.2009 13:33:49 von Ashley Sheridan

--=-/OLf0ggM4/a2EQM9iS9W
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2009-12-08 at 12:34 +0000, Ford, Mike wrote:

>
> > -----Original Message-----
> > From: Skip Evans [mailto:skip@bigskypenguin.com]
> > Sent: 07 December 2009 23:03
> > To: php-general@lists.php.net
> > Subject: [PHP] Passing HTML array index to JS?
> >
> > Hey all,
> >
> > I have an HTML field like this
> >
> > > > style="text-align: right;" onblur="calculateBidUnit();">
> >
> > ... and what I need to do is pass to the calculateBidUnit
> > function the value of quantity, do a calculation on it and
> > plug into this field.
> >
> >
> >
> > Which of course I know how to do for non-array values, but not
> > sure how to get the values to do the calculation on the JS
> > side if the fields are in an array.
>
> H'mm, in my experience the only surefire foolproof way to make sure you pick the correct "bid_unit_value[]" input to match the corresponding "qty[]" is to actually supply specific array indexes (so "qty[1]", "bid_unit_value[1]"; "qty[2]", "bid_unit_value[2]"; etc.). There are other Javascript approaches that work in theory, but I've never been convinced of their robustness.
>
> As to addressing these elements, I merely observe that in Javascript, by definition a.z is *identical* to a["z"]. Application of this to the current situation is left as an exercise for the reader.
>
> Cheers!
>
> Mike
> --
> Mike Ford,
> Electronic Information Developer, Libraries and Learning Innovation,
> Leeds Metropolitan University, C507, Civic Quarter Campus,
> Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
> Email: m.ford@leedsmet.ac.uk
> Tel: +44 113 812 4730
>
>
>
>
>
> To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
>


What about using the DOM for getting to these elements and their
properties?

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-/OLf0ggM4/a2EQM9iS9W--

RE: Passing HTML array index to JS?

am 08.12.2009 13:34:27 von M.Ford

> -----Original Message-----
> From: Skip Evans [mailto:skip@bigskypenguin.com]
> Sent: 07 December 2009 23:03
> To: php-general@lists.php.net
> Subject: [PHP] Passing HTML array index to JS?
>=20
> Hey all,
>=20
> I have an HTML field like this
>=20
> > style=3D"text-align: right;" onblur=3D"calculateBidUnit();">
>=20
> ... and what I need to do is pass to the calculateBidUnit
> function the value of quantity, do a calculation on it and
> plug into this field.
>=20
>
>=20
> Which of course I know how to do for non-array values, but not
> sure how to get the values to do the calculation on the JS
> side if the fields are in an array.

H'mm, in my experience the only surefire foolproof way to make sure you pic=
k the correct "bid_unit_value[]" input to match the corresponding "qty[]" i=
s to actually supply specific array indexes (so "qty[1]", "bid_unit_value[1=
]"; "qty[2]", "bid_unit_value[2]"; etc.). There are other Javascript approa=
ches that work in theory, but I've never been convinced of their robustness.

As to addressing these elements, I merely observe that in Javascript, by de=
finition a.z is *identical* to a["z"]. Application of this to the current s=
ituation is left as an exercise for the reader.

Cheers!

Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http:=
//disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Passing HTML array index to JS?

am 08.12.2009 18:10:57 von TedD

At 9:07 PM -0600 12/7/09, Philip Thompson wrote:
>
>-snip-

Good stuff.

Thanks,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Passing HTML array index to JS?

am 08.12.2009 18:43:17 von Philip Thompson

On Dec 8, 2009, at 11:10 AM, tedd wrote:

> At 9:07 PM -0600 12/7/09, Philip Thompson wrote:
>>
>> -snip-
>
> Good stuff.
>
> Thanks,
>
> tedd

You say so much with so little...

~Philip


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php