edit/operacoes.php
header('Content-Type: application/json');
include('../../lib/mysql.class.php');
$db = new MySQL();
if($db->Error()) $db->Kill();
//VARIÁVEIS DA FORM
$id = intval($_POST['id']);
$sql = "SELECT * FROM operacoes WHERE id=$id";
if(!$db->Query($sql)) $db->Kill();
$db->MoveFirst();
$row = $db->Row();
$data['nome'] = $row->nome;
$data['produto'] = $row->produto;
$data['componente'] = $row->componente;
$data['materiaprima'] = $row->materiaprima;
echo json_encode($data);
jQuery:
$.post('edit/operacoes.php',{id:id},function(data){
$('div.form-container h1.title').text("Editar Existente");
$('input#idbd').empty().val(id);
$('input#nome').empty().val(data.nome);
$('input#produto').attr('checked', (data.produto==1)?true:false);
$('input#componente').attr('checked', (data.componente==1)?true:false);
$('input#materiaprima').attr('checked', (data.materiaprima==1)?true:false);
$('div#addbuttons').css('display','none');
$('div#editbuttons').css('display','block');
showPopup();
});
HTML:
<tr>
<td colspan="3" class="input listas">
<select multiple="multiple" name="escolha_ferramentas" id="escolha_ferramentas" size="15" class="list">
<option value="3">Ferramenta 3</option>
<option value="4">Ferramenta 4</option>
<option value="5">Ferramenta 5</option>
<option value="6">Ferramenta 6</option>
</select>
<div class="list-controls">
<input type="button" value=">" id="next-ferramentas"><br><br>
<input type="button" value="<" id="prev-ferramentas">
</div>
<select multiple="multiple" name="ferramentas[]" id="ferramentas" size="15" class="list">
</select>
<div style="clear:both;"></div>
</td>
</tr>
This works great, except for the selects.
The logic is, the ids that are in the BD (a different one), are the options that need to be in the right select and removed from the left select (append to the right select and remove from the left select).
My question is, how, using json, can I achieve this? Get the ids to the jquery and then loop them to move the options from one select to another?
Edit
I created another php file just to give the ids I need. PHP returns ["2","3","4"]
. How do I loop through that in jQuery.post function(data){loophere}