0

I would like to be able to send multiple dropdown values with the same name to insert them in the database

can somebody help me??

<?php
session_start();
require_once('Connections/koneksi.php');

if($_REQUEST['submit'] == "Submit")
{   
    $name       = $_POST["user"];       
    $masukdatabase = "INSERT INTO `com`(`user`) Values('$ame')";        
}
?>

<html>
<head>
<script>
function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    for(var i=0; i<colCount; i++) {
        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;

        switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }

    }
}
function deleteRow(tableID) {
    try {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {
                alert("Cant delete all rows");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
    }catch(e) {
        alert(e);
    }
}


</script>

</head>
<body>
<form name="form1" method="post" action="adduser.php">                                 
    <table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
    <tr>
    <td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
    <td><select name="user" id="user">
    <option value="tono">tono</option>
    <option value="tini">tini</option>
    </select></td>                           
    </tr>
    </table>
<INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />                 


</form>

</body>
</html>
3
  • Can you explain a little bit more? do you want to add what value on what dropdown list? the new one? Commented Jun 3, 2014 at 10:28
  • i want add value from dropdown menu <select name="user" id="user"> <option value="tono">tono</option> <option value="tini">tini</option> </select> to database. i use addrow function, i just can get the first row from dropdown value but i can't get value from the second and other row Commented Jun 4, 2014 at 2:19
  • halooo... is there anyone can help me to solve this... thank you Commented Jun 4, 2014 at 8:46

1 Answer 1

1

What you need to do to send more than 1 select with the same name is make it an array by adding [] and the end of its name name="user" becomes name="user[]". Then when you receive the variable via POST it will be an array

Using your example:

adduser.php

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST')
{   
    foreach($_POST['user'] as $name)
    {
        $masukdatabase = "INSERT INTO `com`(`user`) Values('${name}')";
    }
}
?>

index.html

<html>
<head>
<script>
function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    for(var i=0; i<colCount; i++) {
        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;

        switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }

    }
}
function deleteRow(tableID) {
    try {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {
                alert("Cant delete all rows");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
    }catch(e) {
        alert(e);
    }
}


</script>

</head>
<body>
<form name="form1" method="post" action="adduser.php">                                 
    <table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
    <tr>
    <td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
    <td><select name="user[]" id="user">
    <option value="tono">tono</option>
    <option value="tini">tini</option>
    </select></td>                           
    </tr>
    </table>
<INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />                 
<input type="submit" value="Submit" />
</form>

</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

IT WORKS!!!! yessss.... many thankss SERPRO... i was suprised if it just need adding a simple code.... thank you so much...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.