Log in

View Full Version : help with multi select transfer code



sarahmx
07-29-2013, 03:47 AM
Hello DD

im a php newbie

i need some help with this multiselect transfer code..the code is working.. i just have problem in figuring on how to add/update/delete data to mysql

a. when user add in any value from select1 to select2 and click submit..it will insert data to mysql
b. else when user remove any value from select2 to select 1, it will update/delete in database

i have two tables unit and user

unit is to store the list of unit
user is to when data will be insert/update/delete





<html>
<head>
<script src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type="text/javascript">
$().ready(function() {
$('#add').click(function() {
return !$('#select1 option:selected').remove().appendTo('#select2');
});
$('#remove').click(function() {
return !$('#select2 option:selected').remove().appendTo('#select1');
});
});
</script>

<style type="text/css">
a {
display: block;
border: 1px solid #aaa;
text-decoration: none;
background-color: #fafafa;
color: #123456;
margin: 2px;
clear:both;
}
div {
float:left;
text-align: center;
margin: 10px;
}
select {
width: 100px;
height: 80px;
}
</style>

</head>

<body>
<div>
<form action="insert.php" method="post">

<?php

include "connect.php";
echo "<select id='select1' multiple>";


$result = mysql_query("SELECT * FROM unit ORDER BY id ASC");

while($row = mysql_fetch_array($result))
{
$list=$row['list_unit'];
echo "<option value='$nama'>$list</option>";
}


echo "</select>";
?>

<a href="#" id="add">add &gt;&gt;</a>
</div>
<div>
<select multiple id="select2"></select>
<a href="#" id="remove">&lt;&lt; remove</a>
</div>

<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

traq
07-29-2013, 03:56 AM
when user add in any value from select1 to select2 and click submit..it will insert data to mysql
You'll need to process the data submitted by the form and use it to update your database.


else when user remove any value from select2 to select 1, it will update/delete in database
This won't happen unless you submit the form (whether by clicking the submit button, or another method, such as AJAX). You need to get the data to your insert.php script.

...speaking of

<form action="insert.php" method="post">

Where is your insert.php script? You need to post the code you're asking for help with.

--------------------------------------------------
# If at all possible, you should avoid using the mysql_* functions. #
Existing code should be updated to avoid performance and security problems.


Warning
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli (http://php.net/mysqli) or PDO_MySQL (http://php.net/PDO) extension should be used. See also the MySQL API Overview (http://php.net/mysqlinfo.api.choosing) for further help while choosing a MySQL API.