Jump to content

Form List


Tekky

Recommended Posts

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.

Link to comment
Share on other sites

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
}
?>

 

  • Like 1
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by mac_gyver
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.