Is it possible that $ _POST only select textarea that have a value?

2

I have 2 web pages, one that is the main one (a form), which I am asking for some data by textarea and with a button I give them the option to generate more textarea to be able to continue writing.

<script language="JavaScript">
        window.onbeforeunload = confirmExit;
        function confirmExit(){
            return "Ha intentado salir de esta pagina. Si ha realizado algun cambio en los campos sin hacer clic en el boton Guardar, los cambios se perderan. Seguro que desea salir de esta pagina? ";
        }

        var table_inputs = 0;
        function addTextarea(id, name) {
            var table     = document.getElementById(id);
            var row       = table.insertRow(2);
            var cell      = row.insertCell(0);
            var input     = document.createElement("textarea");
            table_inputs++;
            input.rows    = "4";
            input.cols    = "170";
            input.name    = name + table_inputs;
            input.id      = id + table_inputs;
            var campo     = document.createElement("input");
            campo.type    = "button";
            campo.value   = "Borrar";
            campo.onclick = function () {
                var fila  = this.parentNode.parentNode;
                var tbody = table.getElementsByTagName("tbody")[0];
                tbody.removeChild(fila);
            }
            cell.appendChild(input);
            cell.appendChild(campo);
        }

        function vaciar_campo(input1) {
            input1.value = "";
        }
    </script>
</HEAD>
<BODY>
    <form name="formulario" id="formulario" action="creador.php" method="post" width="30%">
        <table id="tablafecha" border="3" width="100%">
            <tr>
                <td><h1>Fecha y número</h1>
                </td>
            </tr>
            <tr>
                <td>Fecha: <input type="text" name="fecha" id="fecha"><br>
                    Número: <input type="number" name="numero" id="numero">
                </td>
            </tr>
        </table>

        <table id="tesisTable" border="3" width="100%">
            <tr>
                <td><h1>TESIS DOCTORAL</h1>
                </td>
            </tr>
            <tr>
                <td>
                    <textarea name="tesis" id="tesis" rows="4" cols="170"></textarea>
                    <button onclick="addTextarea('tesisTable', 'tesis')" type="button" name="tesis2" id="tesis2">Añadir</button>
                </td>
            </tr>
        </table>

On this other page, what I do is retrieve the content of all the textarea and implement them on a page with some styles that I assign to it.

  <?php
     $post = [];

  if( !empty( $_POST ) )
  {
    $post = $_POST;
  }

  $tesis        = array_intersect_key( $post, array_flip( preg_grep( '/^tesis[0-9]*/i', array_keys( $post ) ) ) );
  $subvenciones = array_intersect_key( $post, array_flip( preg_grep( '/^subvenciones[0-9]*/i', array_keys( $post ) ) ) );
  $otrainfo     = array_intersect_key( $post, array_flip( preg_grep( '/^otrainfo[0-9]*/i', array_keys( $post ) ) ) );
  $actualidad   = array_intersect_key( $post, array_flip( preg_grep( '/^actualidad[0-9]*/i', array_keys( $post ) ) ) );
?>

<script type="text/javascript">
var fecha ="<?php echo $_POST['fecha']; ?>";
  var numero ="<?php echo $_POST['numero']; ?>";
  var datos ="<?php foreach( $tesis as $key => $value ): ?><ul style='margin-top:0cm' type=disc><li class=MsoNormal style='margin-right:11.35pt;mso-list:l0 level1 lfo1;tab-stops:list 36.0pt'><span style='font-family:'Arial','sans-serif';mso-fareast-font-family:'Times New Roman''><?php echo $value; ?></span></li></ul><?php endforeach; ?>";

My question is, is there any way to modify or tell $ _POST not to take the textarea that have not been filled out in the form? Or is there another way to perform this operation more easily? Thanks.

    
asked by Oscar Sanclemente 23.03.2017 в 12:59
source

2 answers

1

A quick and direct way is array_filter .

if( !empty( $_POST ) )
{
    $filter = array_filter($_POST); # <-- me falto cerrar
    $post   = $filter;
}

I have not tried it, but I think I should do what you want to do.

    
answered by 23.03.2017 / 13:09
source
1

The response of @OscarR. Solve your problem very well. However, it is always important to know different ways. Here I show you a way to do it from the client:

function sendTexts (e) {
  e.preventDefault();
  let form = e.target;

  let textareas = form.querySelectorAll('textarea');
  let empty = [].filter.call(textareas, function (textarea) {
    if (textarea.value === '') { return textarea; }
  });

  empty.forEach(function (textarea) {
    form.removeChild(textarea);
  });

  form.submit();
}

The event submit of the form is simply captured and the textareas that are empty are obtained; These elements are deleted from the form and sent. This will happen very quickly that will not be noticed, so it will be transparent to the end user.

    
answered by 23.03.2017 в 14:00