Re: Passing HTML array index to JS?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

> 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
>>> 
>>> <input type="text" name="qty[]" value="!!quantity!!" size="4" 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.
>>> 
>>> <input type="text" name="bid_unit_value[]" value="" size="4">
>>> 
>>> 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:
>> <input type="text" name="qty[]" value="!!quantity!!" onblur="calculateBidUnit();" id="qty" />
>> <input type="text" name="bid_unit_value[]" value="" id="bid_unit_value" />
>> <script type="text/javascript">
>> function calculateBidUnit () {
>>    var qty = document.getElementById('qty');
>>    var buv = document.getElementById('bid_unit_value');
>>    buv.value = qty.value;
>> }
>> </script>
>> 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

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...

<input type="text" name="qty[]" value="1" id="qty1" onblur="calc(this.value);" />
<input type="text" name="qty[]" value="2" id="qty2" onblur="calc(this.value);" />
<input type="text" name="qty[]" value="3" id="qty3" onblur="calc(this.value);" />

<input type="text" name="bid_unit_value[]" value="" id="buv" />

<script type="text/javascript">
function calc (value) {
    document.getElementById('buv').value = value;
}
</script>

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)...

<script type="text/javascript">
function calc (value) {
    var buv = document.getElementById('buv');
    if (buv.value == '') buv.value = value;
}
</script>

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

Array
(
    [qty] => Array
        (
            [0] => 1
            [1] => 2
            [3] => 3
        )
    [bid_unit_value] => Array
        (
            [0] => 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...

<script type="text/javascript">
function calc (value) {
    var buv = document.getElementById('buv');
    if (buv.value == '') buv.value = value;
    else buv.value += '|'+value;
}
</script>

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

Array
(
    [qty] => Array
        (
            [0] => 1
            [1] => 2
            [3] => 3
        )
    [bid_unit_value] => Array
        (
            [0] => 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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux