Tekky Posted July 30, 2022 Share Posted July 30, 2022 Hello, i'm a bit blocked, i'm trying to make a form, which display a list of options (the options would be retrieved from a data base), then a url corresponding to the option choosen which is saved in the database would be displayed. Basically : 1) Php script will load in the <input list> as much options as data is available in the database (if we have 10 names stored, then the 10 names will show up in the list!) 2)Once we choose the name we want and click on send, the name choosen have to show under the form with a url linked to it. My data base will have 3 data in one row. First is ID, second is Name, and third is URL. The url have to be recovered from it. Here's what i'm starting with from scratch : <form action="" method="get"> <label for="ListSaint">Choisis le Saint qui correspond a la date souhaité :</label> <input list="ListSaints" name="ListSaint" id="ListSaint"> <datalist id="ListSaints"> <option value="test1"> <option value="test2"> </datalist> <input type="submit"> </form> <h1>L'url est : <a href="url/"></a></h1> But i can not figure out how to add an option to the <datalist> with a php command which is connected to the data base. Thank you for paying attention to this. Quote Link to comment https://forums.phpfreaks.com/topic/315110-form-list/ Share on other sites More sharing options...
mac_gyver Posted July 30, 2022 Share Posted July 30, 2022 seems you are asking how to loop over the result from an sql query to dynamically produce a section of html markup? since you are using the PDO extension, i recommend that you fetch all the data from the query into an appropriately named php variable, then test and loop over that variable at the correct location in the html document - <?php // in the code querying for the data $result_data = $stmt->fetchAll(); // at the point of outputting the data if(empty($result_data)) { echo 'There is no data to display.'; } else {?> <form action="" method="get"> <label for="ListSaint">Choisis le Saint qui correspond a la date souhaité :</label> <input list="ListSaints" name="ListSaint" id="ListSaint"> <datalist id="ListSaints"> <?php foreach($result_data as $row) { echo "<option value='{$row['name']}'>"; } ?> </datalist> <input type="submit"> </form> <?php } ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/315110-form-list/#findComment-1598802 Share on other sites More sharing options...
Tekky Posted August 1, 2022 Author Share Posted August 1, 2022 Hello and thank you for the time spent helping me. But i have another question, in your example, the <form> method is "get". But i also need to have a "post" method to recover the option used and sent and then display it somewhere else in the page. Do i have to make a <form> in another form (but i believe it's not really working) so is there any way to use both get and post or something? Quote Link to comment https://forums.phpfreaks.com/topic/315110-form-list/#findComment-1598845 Share on other sites More sharing options...
mac_gyver Posted August 1, 2022 Share Posted August 1, 2022 (edited) 12 hours ago, Tekky said: in your example it's your example. all I did is show how to fetch, test, and loop over data to dynamically produce the <option> tags. the <form markup is just what you had in the starting code. i/we don't know what the intent is of having that form tag in the code. as to the rest of your statement, a description like that doesn't help. post an example showing what piece of data you have, what processing you want to do based on that piece of data, and what end result you want. Edited August 1, 2022 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/315110-form-list/#findComment-1598873 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.