Jump to content


Photo

Two Values In Onchange


  • Please log in to reply
5 replies to this topic

#1 timmah1

timmah1

    Advanced Member

  • Members
  • PipPipPip
  • 1,147 posts
  • LocationOhio

Posted 05 December 2012 - 04:22 PM

I'm trying to pass two different values from the drop down option to two different hidden fields

I have drop down like:

function Exchange(First, Second){
var Elm1 = document.getElementById('on0');
var Elm2 = document.getElementById('amount')
Elm2.value = Elm1.value;
}

<select name="on0" onchange="Exchange('on0', 'amount');">'

<?php
while($rows = mysql_fetch_object($sql)){
echo '<option value="'.$rows->name.'-'.$rows->price.'">'.$rows->name.'-'.$rows->price.'</option>';
}
echo '</select>';

Then I need the values to be put into these fields

echo '<input type="text" name="amount" id="amount" value="" />';
echo '<input type="text" name="on0" id="on0" value="" />';


I really have no idea how to make this work.

Can anybody help me out?

thanks in advance

If you at first you don't succeed, quickly deny you were even trying!

#2 Xaotique

Xaotique

    Advanced Member

  • Members
  • PipPipPip
  • 563 posts
  • LocationPennsylvania, United States
  • Age:22

Posted 05 December 2012 - 05:01 PM

If you're making it a hidden value for a form, why not just pass the select rather than doing extra work?

#3 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,821 posts
  • LocationCanada

Posted 05 December 2012 - 05:58 PM

First off, if you have a Javascript issue post the HTML source code - not the PHP code that creates the HTML. It is difficult to read the PHP and then in your head convert it to what the HTML will be, then determine what the Javascript is doing.

Second, I agree completely with Xaotique. It usually makes no sense to use JS to auto-populate hidden fields since you can (and should) do that determination on the server. What is worse is that you appear to be populating an amount field. If this was for a shopping cart, it would be very easy for a user to override the value you set in that hidden field to change the price of the item.

However, if you were using the logic to populate some read-only fields so the user can see the price or to auto-populate some fields you want the user to edit that would be fine. But, what it appears you are trying to use it for would open a gigantic hole for a malicious user. You are better off just having the select list and using the ID of the product as the value. Then pass that when the user submits to get the current price and do any calculations on the server.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#4 timmah1

timmah1

    Advanced Member

  • Members
  • PipPipPip
  • 1,147 posts
  • LocationOhio

Posted 06 December 2012 - 03:24 PM

Thanks for the replies, but to be honest, I didn't post on here for advice, good advice nontheless, but not the reason I posted.

They are going to be hidden fields, they are just text so I can view the results.

This is for a site that was already done with PayPal shopping cart, they are wanting to be able to edit the information for the items through a backend instead of replacing the html code everytime they change something.

Thanks anyhow, I'll find the solution

If you at first you don't succeed, quickly deny you were even trying!

#5 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,821 posts
  • LocationCanada

Posted 06 December 2012 - 03:55 PM

Then you should provide that information up front. There are a lot of people that are new to coding that come to this forum and, many times, they are asking for a solution that is a bad one and will set a bad precedent for their learning. So, when responding to requests I feel obligated to prevent propagating bad habits before they start. But, what you said still makes no sense. There should be nothing in the HTML code for the product and/or price other than an ID for the product and the quantity. But, no matter, you think you know what you are doing so I'll give you a potential solution. Just give me a URL to the site so I can go and purchase whatever they are selling and I can change the price to $0.01

Use an onchange trigger to take the value of the select field as you currently have it and split() the string based upon the dash. If any product names can contain a dash then you would have to use logic to split it only at the last dash. Then take those two values and populate into the hidden fields.

<html>
<head>
<script type="text/javascript">

function populateNamePrice(value)
{
    var name, price;
    if(value=='')
    {
        document.getElementById('name').value = '';
        document.getElementById('price').value = '';
        return
    }

    var value_parts = value.split(' - ');
    document.getElementById('name').value = value_parts[0];
    document.getElementById('price').value = value_parts[1];
    return;
}

</script>
</head>

<body>

Select a product:
<select name="product" id="product" onchange="populateNamePrice(this.value);">
    <option value="">-- Select a Product --</option>
    <option value="Product 1 - 1.00">Product 1 - 1.00</option>
    <option value="Product 2 - 2.00">Product 2 - 2.00</option>
    <option value="Product 3 - 3.00">Product 3 - 3.00</option>
    <option value="Product 4 - 4.00">Product 4 - 4.00</option>
    <option value="Product 5 - 5.00">Product 5 - 5.00</option>
    <option value="Product 6 - 6.00">Product 6 - 6.00</option>
</select>
<br><br>
Name: <input type="text" name="id" id="name" readonly="readonly"><br>
Price: <input type="text" name="price" id="price" readonly="readonly"><br>
</body>
</html>

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#6 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 06 December 2012 - 04:12 PM

Why stop at $0.01? Why not set the price to $-10000? ;)
Keeping it simple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com