How to pass list of elements to database?

0

I have a tag in HTML and several of them I use these tags because I want to simulate a list, where I can add elements, select them and modify them as well as delete (I already do this by JavaScript). at the time of passing them to the database through PHP I use POST to send data and these labels only send the data that is selected (I did not know that) so I am looking for a way to pass all the data of the data regardless of whether they are selected or not, however I could not pass the data to the database, I hope you can help me solve this, or another way to do this even if they are other tags.

Html

<form action="regSala.php" method="POST" class="form-horizontal">
    <select multiple class="form-control" id="elementosHTML" name="elementos[]">
        <option></option>
        <option></option>
        <option></option>
        <option></option>
    </select>               
<input class="seleccion" type="submit" value="Guardar">
</form>

PHP

foreach ($_REQUEST["elementos"] as $option_value){
      echo $option_value;
 }
    
asked by Alberto zt 09.04.2018 в 05:17
source

2 answers

1

I do not think the <option> tag is the right one to do what you say. Anyway here I leave a way to fix it with native javascript and an input of type hidden :

<form onsubmit="return wtf_is(this);" action="regSala.php" method="POST" class="form-horizontal">
    <select multiple class="form-control" id="elementosHTML" name="elementos[]">
        <option values="uno">Uno</option>
        <option values="dos">Dos</option>
        <option values="tres">Tres</option>
        <option values="cuatro">Cuatro</option>
    </select>
    <input type="hidden" name="listValues">               
<input class="seleccion" type="submit" value="Guardar">
</form>


<script>
    var wtf_is = function(e){
        var values = [];
        for (var i = 0; i<e.elementosHTML.length; i++) {
            values[i] = e.elementosHTML[i].value;
        }
        e.listValues.value = values;
    };
</script>

What it does is send 2 variables, one called "elements" that contains the selected options in case you need to use them, and the other one is called "listValues" that contains all the selected and unselected options.

It is very important that in PHP you store the variable listValues with an explode (). Ex:

$elementos = isset($_POST['elementos']) ? $_POST['elementos'] : null;
$listValues = isset($_POST['listValues']) ? explode(',', $_POST['listValues'][0]) : null;

echo "<pre>";
var_dump($elementos);
var_dump($listValues);
    
answered by 09.04.2018 / 14:56
source
0

I can think of a couple of things:

  • Make sure the <option> tags have values, if not as a value then as an attribute ( value="algo" ).
  • I had never tried the select multiple , but you could well venture and use checkbox instead. If you do not like popcorn, you can delete them later with CSS, and / or assign the label tag so that when you click on it the checkbox is also activated or deactivated.
  • <input type="checkbox" name="elementos[a]" value="x" />
    <label for="elementos[a]">Elemento A</label>
    <input type="checkbox" name="elementos[b]" value="y" />
    <label for="elementos[b]">Elemento B</label>

    That should already throw values at the endpoint.

    By the way, try to stick to the query method always. In the form you are using POST , while in your PHP you are collecting both (GET and POST). Using $_REQUEST can be dangerous in production environments.

        
    answered by 09.04.2018 в 09:06