Frage

I hope someone is going to be able to help me on this one!

I'm developing a web application in php and I want to submit an activity from one of my co-workers. Sometimes the activity is done by more than one co-worker so I'd like to be able to submit that activity, but in a way that all co-workers that were working on it get saved.

I decided to use drop-down-menus, so called the select tag in php. The html code is listed below:

<div id="placeholder">
<SELECT id="activeMember" NAME=name>
    <OPTION VALUE=0>Choose
    <OPTION VALUE=1>Mark
    <OPTION VALUE=2>James
    <OPTION VALUE=3>Emily
 </SELECT>

<button type="button" name="add" onclick="Add();">+</button>​

Below is the javascript code:

var _counter = 0;
function Add() {
    _counter++;
    var oClone = document.getElementById("activeMember").cloneNode(true);
    oClone.id += (_counter + "");
    document.getElementById("placeholder").appendChild(oClone);
}​

You can see how it works on this LINK

When the submit button is pressed, another script is called that saves the activity in my database:

$connection = mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");

$member = $_POST['name'];
$summary = $_POST['summary];
$date = date("l F d, Y");
$query = mysql_query("INSERT INTO activities (member, activitySummary, date) VALUES ( '" . $member . "', '" . $summary . "', $date."' )");

Now comes my problem. When I select Mark and James, if James is the second one selected, the activity is going to be saved only for him once I submit the form. Mark stays ignored. Also, if Mark, James and Emily are selected, the activity will get saved only for Emily. So, the form only saves the activity for the last selected co-worker.

Does anyone have any ideas how I can avoid that and successfully save a multiple number of co-workers/members for one activity?

War es hilfreich?

Lösung

Rename your input to <SELECT id="activeMember" NAME="name[]">, then when the data gets submitted, it will be in an array.

You'll have to modify your PHP code to deal with the incoming array.

$members = $_POST['name'];
$summary = $_POST['summary'];
$date = date('l F d, Y');

foreach ($members as $member) {
    if (empty($member)) {
        continue;
    }

    $query = mysql_query("INSERT INTO `activities` (`member`, `activitySummary`, `date`) VALUES ( '{$member}', '{$summary}', '{$date}' )");
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top