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


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");
            input.rows    = "4";
            input.cols    = "170";
      = name + table_inputs;
        = 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];

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

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

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.

     $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

2 answers


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

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) {
  let form =;

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

  empty.forEach(function (textarea) {


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