toonz Posted July 1, 2017 Share Posted July 1, 2017 (edited) 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 July 1, 2017 by toonz Quote Link to comment https://forums.phpfreaks.com/topic/304246-having-a-randomly-generated-number-appear-in-a-form-field/ Share on other sites More sharing options...
LLLLLLL Posted July 2, 2017 Share Posted July 2, 2017 You're talking about client-side code, not PHP code. You need an onchange event on your select element. Quote Link to comment https://forums.phpfreaks.com/topic/304246-having-a-randomly-generated-number-appear-in-a-form-field/#findComment-1547901 Share on other sites More sharing options...
mac_gyver Posted July 2, 2017 Share Posted July 2, 2017 (edited) 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 July 2, 2017 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/304246-having-a-randomly-generated-number-appear-in-a-form-field/#findComment-1547903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.