I have a form and have two input listbox .

When i select the list valuse left list box and then perss the button selected values are move in the right list box
and then perss the Update button right list box values are storted in the database

If any butter way of doing what i need and thank you for any suggestion,

thanks
vssp

Dani AI

Generated

Brief summary and correction to earlier replies from , and : moving options between two listboxes is best handled client-side with JavaScript, then persist the final right-hand list to the server. Note that browser JavaScript cannot "execute PHP" directly — PHP runs on the server. The usual, simple flow is: move options in the DOM, give the right list a name like selected[], and ensure its options are selected before the form is submitted so PHP receives them as an array.

Example HTML + JavaScript (move options, then select all on submit):

<form method="post" action="save.php" onsubmit="selectAll('right');">
<select id="left" multiple size="8">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>

<button type="button" onclick="moveSelected('left','right')">Move &gt;&gt;</button>

<select id="right" name="selected[]" multiple size="8"></select>

<input type="submit" value="Update">
</form>

<script>
function moveSelected(fromId,toId){
var from=document.getElementById(fromId), to=document.getElementById(toId);
for(var i=from.options.length-1;i>=0;i--){
var opt=from.options[i];
if(opt.selected){
to.add(new Option(opt.text,opt.value));
from.remove(i);
}
}
}
function selectAll(id){
var s=document.getElementById(id);
for(var i=0;i<s.options.length;i++) s.options[i].selected=true;
}
</script>

Minimal server-side handling in PHP (validate and use prepared statements):

<?php
if (!empty($_POST['selected']) && is_array($_POST['selected'])) {
$ids = array_filter($_POST['selected'], 'ctype_digit'); // example validation
if ($ids) {
$pdo = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8mb4','user','pass',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('INSERT INTO selections (item_id) VALUES (:id)');
foreach ($ids as $id) $stmt->execute([':id'=>$id]);
}
}
?>

Quick tips: ensure every option has a value; guard server-side against tampered values; preserve order if needed by adding an index in the value or sending a JSON payload via AJAX; add keyboard support and a no-JS fallback (submit with action flags and let PHP re-render the lists). This approach combines the client responsiveness suggested by and with secure server-side persistence.

Recommended Answers

All 2 Replies

You can use javascript onClick function (or onChange if to remove the need of the first button) to execute php code/function to print/echo the post /get value to the next box.

yes it is easy

use javascript onclick function
do ti you can succeed!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.