Jump to content

Two Values In Onchange


timmah1

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.