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

Asked

Viewed 227 times

0

I have a code on page1.php where I have an array of checkboxes filled with Mysql data through a query. I can send the value of checkboxes marked to the page2.php. But I want the order to be the same in which the checkboxes were selected.

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

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

The 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(); ?>
  • What the function setChecks() does? Your jQuery function handles a array that is never used. The way it is $_POST['check_order'] page2.php receives all Ids, as all fields Hidden are sent

  • The setChecks() function was there to check the number of selections by the user, but I don’t need it in this case (already taken from the code). I don’t know how to program in Jquery, but I’m trying to understand. Should my Jquery function take the values of checkboxes or Hidden fields? Should my checkboxes still be arrays? do I really need checkboxes and Hidden fields? I’m sorry about all the questions, but I’m really confused about this.

  • I’ve found the right code and I’ve made it work. I just need to put commas between the numbers. Help?

  • Problem solved, it was just take ' r n' of . Join()

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.