Reputation: 4885
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}
Upvotes: 0
Views: 598
Reputation: 1523
Well, there are several options to achieve what you want.
In your ajax call, i would pass to php the selected id from the left select and the arrays of id's from the right select, and in the returning of the php you get an object with the data to be filled in the left select and in the right select again emptying them first, so you will never get confused and have the control with the elements on each select.
Upvotes: 0
Reputation: 16025
I would pass an array of strings from the PHP (the json looks like ['string','string','string']
and then use a loop on that to indicate what ones to put "on the other side".
I don't see where you're returning those from the server yet, so maybe the language translation on the passed elements is throwing me off, but it's evident that you're making one of these:
A B
[ ] [ ]
[ ] > [ ]
[ ] < [ ]
[ ] [ ]
Just pass the elements that need to be removed from A into B as a string array, and loop over the array elements.
Upvotes: 1