Jump to content

Listbox to database


anshuverma1989

Recommended Posts

Hey people..

 

I have this hobby listbox with up to near about 15 items where a user can select multiple item.

How can i insert all the items selected in the listbox?

 

Example Hobbies: If multiple hobbies are selected in the listbox and on press button how should i insert into database?

 

I saw this tutorial on the website.. in which .. if one item is select it will save one item only and if multiple item is selected, it puts "," between the items selected and then save into database.

but i cannot find it rite now.. so please help me .. how can i do it??

 

Link to comment
https://forums.phpfreaks.com/topic/144791-listbox-to-database/
Share on other sites

Page: test.php

<?php
if (isset($_POST['list']) && is_array($_POST['list'])) {
    $items = implode(", ", $_POST['list']);
    echo $items;
}
?>
<form action="test.php" method="POST">
<select size="4" name="list[]" multiple>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
</select>
<input type="submit" value="submit" />
</form>

 

Should do it, making the name of the select an array (adding the []) allows for you to post more than one element. Using the implode on that posted item will make it into one line, comma separated.

Link to comment
https://forums.phpfreaks.com/topic/144791-listbox-to-database/#findComment-759807
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.