Jump to content

Having a randomly generated number appear in a form field


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?

Edited by toonz
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.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

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

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.