How to keep the order of the selected checkboxes and send by form to another page

1

I have a code in page1.php where I have an array of checkboxes populated with MySQL data through a query. I can send the value of checked checkboxes to page2.php . But I want the order to be the same as the checkboxes were selected.

My intention is to show in page2.php what the user selected in page1.php , following the order he clicked.

MODIFIED: Well, I was able to create the list and send via form. The problem now is to put a comma between the numbers. I know this should be the easiest problem to solve, but I'm not getting it yet.

SQL Query is getting like this, for example:

  

SELECT * FROM emp WHERE id IN (7,9,30) ORDER BY FIELD (id,   30 9 7)

page1.php

<script type="text/javascript">
var listCheckedOptions = [];

function addToList(checkObj, outputObjID) {
    //Remove from array if the element already in.
    if (listCheckedOptions.indexOf(checkObj) >= 0) {
        listCheckedOptions.splice(listCheckedOptions.indexOf(checkObj), 1);
    } 

    listCheckedOptions.push(checkObj);

    document.getElementById(outputObjID).value = ""; //Clean textarea
    document.getElementById(outputObjID).value = listCheckedOptions.map(function (o) {
        return o.value;
    }).join('\r\n'); //Add to textarea
    if (!checkObj.checked) checkObj.nextSibling.querySelector('span').innerHTML = '';
    listCheckedOptions.forEach(function (el, i) {
        var span = el.nextSibling.querySelector('span').innerHTML = i + 1;
    });

    return;
}
</script>

    <?php
    $query = "SELECT * FROM imoveis";
    $result = mysql_query($query,$conn);
    ?>

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

    <input type="submit" value="Create" id="enviarButton"/></td>

    <br><br>

    <table border="1">
        <?
        $i = 0;

        echo "<tr>";

             while($imovel = mysql_fetch_array($result)){

                $name = $imovel['emp'];
                $id = $imovel['id'];

    ?>   

            <td>
                <?=$name;?><br>
                <input type="checkbox" name="op_imovel[]" value="<?=$id;?>" onClick="addToList(this, 'txt1')">
            </td>
    <?
               $i++;
               if(($i % 3) == 0){
                      echo "</tr><tr>";
                      }
               }

    mysql_close();

    ?>

    </table>
    <br>
    <input type="hidden" name="txt1" id="txt1">

    </form>

page2.php

<?php
  $checkbox = $_POST['op_imovel'];
  $lista = $_POST['txt1'];

  if (is_array($checkbox) && count($checkbox) > 0){
        $res = '';
        $tot = count($checkbox) - 1;
        for ($y = 0; $y <= $tot; $y++) {

            $res .=$checkbox[$y];

            if ($y < $tot)
                $res .= ",";
        }

    }


  $query = "SELECT * FROM emp WHERE id IN ($res) ORDER BY FIELD (id, $lista)";
  $result = mysql_query($query,$conn);
?>

<br>


<div align="center">

        <table border="1">

<?
$i = 0;
echo "<tr>";

    if (is_array($checkbox) && count($checkbox) > 0){
        while($imovel = mysql_fetch_array($result)){ 

            $name = ($imovel['emp']);
?>
                <td>
                    <p>
                        <?= $name;?>
                    </p>

                </td>
<?
           $i++;
           if(($i % 3) == 0){
                  echo "</tr><tr>";
           }


        }       

    }   
?>      


</div>

<?  mysql_close(); ?>
    
asked by anonymous 25.08.2015 / 04:01

0 answers