Jump to content

Having a randomly generated number appear in a form field


toonz

Recommended Posts

Thanks in advance to those who can help with this!!

I have code in a php file that pulls values from a table to display in a dropdown field in a form

     $rarity = $dbObj->cgs("tbl_rarity", "", "del_status", "0","rarity_name","",false);
  while($row_rarity = @mysql_fetch_array($rarity))
  {   
      $rarity_id[] = $row_rarity['id'];
      $rarity_value[] = $row_rarity['rarity_name'];
      $rarity_min[] = $row_rarity['price_from'];
      $rarity_max[] = $row_rarity['price_to'];
  }
  $smarty->assign("rarity_id", $rarity_id);
 $smarty->assign("rarity_value", $rarity_value);
 $smarty->assign("rarity_min", $rarity_min);
 $smarty->assign("rarity_max", $rarity_max);

The code works fine and does what I need on the user form. There is also a field on the user form for PRICE, which is
 

   <input type="text" name="price" id="exD_price"/>

 

Right now, I have to manually enter a value in that field of my choosing. The goal is to have it take the $rarity_min and $rarity_max values associated with the chosen rarity from the dropdown menu, and automatically insert a value in the price field that falls within the range of the min and max numbers, and each time I change a value in the dropdown it generates a new number and inserts it in to the price field.

In the php code I tried creating something
 

 $rprice=rand($rarity_min, $rarity_max);

and had added a value to the <input /> like value="{$rprice}", but after I select an option in the dropdown, nothing appears in the price field. I imagine there has to be an OnUpdate sort of function that needs to be called after the dropdown value is changed, but how do I do that in php?

Link to comment
Share on other sites

what is the overall goal/purpose of what you are doing and who/what type of visitor will be doing it?

 

if this is to let a general user select an item and have a random price generated, between the min/max values, you would NOT use the price from the form, since it can be manipulated by the visitor and be any value. you can display the range/min/max price for each choice, but you would generate the actual price value, on the server, once the form has been submitted. you can use ajax to submit the selected item id to the server, generate the random price on the server, save the chosen item's id and generated price on the server, and return and display the generated price in the browser, but this value returned to the browser is only for display purposes.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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