Post registration error with empty text

Asked

Viewed 36 times

1

The registration code of the post is this:

                    <?php
    if(isset($_POST['cadastrar'])){
        $titulo         = trim(strip_tags($_POST['titulo']));
        $data           = trim(strip_tags($_POST['data']));
        $descricao      = $_POST['descricao'];
        $exibir         = trim(strip_tags($_POST['exibir']));




        //INFO IMAGEM
    $file       = $_FILES['img'];
    $numFile    = count(array_filter($file['name']));

    //PASTA
    $folder     = '../upload/postagens/';

    //REQUISITOS
    $permite    = array('image/jpeg', 'image/png');
    $maxSize    = 1024 * 1024 * 5;

    //MENSAGENS
    $msg        = array();
    $errorMsg   = array(
        1 => 'O arquivo no upload é maior do que o limite definido em upload_max_filesize no php.ini.',
        2 => 'O arquivo ultrapassa o limite de tamanho em MAX_FILE_SIZE que foi especificado no formulário HTML',
        3 => 'o upload do arquivo foi feito parcialmente',
        4 => 'Não foi feito o upload do arquivo'
    );

    if($numFile <= 0){
        echo '<div class="alert alert-danger">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    Selecione uma imagem e tente novamente!
                </div>';
    }
    else if($numFile >=2){
        echo '<div class="alert alert-danger">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    Você ultrapassou o limite de upload. Selecione apenas uma foto e tente novamente!
                </div>';
    }else{
        for($i = 0; $i < $numFile; $i++){
            $name   = $file['name'][$i];
            $type   = $file['type'][$i];
            $size   = $file['size'][$i];
            $error  = $file['error'][$i];
            $tmp    = $file['tmp_name'][$i];

            $extensao = @end(explode('.', $name));
            $novoNome = rand().".$extensao";

            if($error != 0)
                echo $msg[] = "<b>$name :</b> ".$errorMsg[$error];
            else if(!in_array($type, $permite))
                echo $msg[] = "<b>$name :</b> Erro imagem não suportada!";
            else if($size > $maxSize)
                echo $msg[] = "<b>$name :</b> Erro imagem ultrapassa o limite de 5MB";
            else{

                if(move_uploaded_file($tmp, $folder.'/'.$novoNome)){


        $insert = "INSERT into tb_postagens (titulo, data, imagem, descricao, exibir) VALUES (:titulo, :data, :imagem, :descricao, :exibir)";

    try{
        $result = $conexao->prepare($insert);
        $result->bindParam(':titulo', $titulo, PDO::PARAM_STR);
        $result->bindParam(':data', $data, PDO::PARAM_STR);
        $result->bindParam(':imagem', $novoNome, PDO::PARAM_STR);
        $result->bindParam(':descricao', $descricao, PDO::PARAM_STR);
        $result->bindParam(':exibir', $exibir, PDO::PARAM_STR);

        $result->execute();
        $contar = $result->rowCount();
        if($contar>0){
            echo '<div class="alert alert-success">
                  <button type="button" class="close" data-dismiss="alert">×</button>
                  <strong>Sucesso!</strong> O post foi cadastrado.
            </div>';
        }else{
            echo '<div class="alert alert-danger">
                  <button type="button" class="close" data-dismiss="alert">×</button>
                  <strong>Erro ao cadastrar!</strong> Não foi possível cadastrar o post.
            </div>';
        }           
    }catch(PDOException $e){
        echo $e;
    }   






                }else
                    $msg[] = "<b>$name :</b> Desculpe! Ocorreu um erro...";

            }

            foreach($msg as $pop)
            echo '';
                //echo $pop.'<br>';
        }
    }


    }


 ?>

The form to register the post is this:

<div class="w3-card-4 w3-margin w3-white">

<div class="w3-container" style="padding-top: 20px; padding-left: 20px;">                                       
<h2><b> Adicionar Post </b></h2><br>
<div class="tab-pane" id="formcontrols">
<form id="edit-profile" class="form-horizontal" action="" method="post" enctype="multipart/form-data">
</div>

<div class="w3-container" style="padding-left: 20px;">                                       
    <p>
        <label>Título</label> <br>
        <input style="width: 300px; height: 40px; border-top: 1px; border-left: 1px; border-right: 1px; border-color: lightgray;" 
        type="text" name="titulo" placeholder="Digite o título" id="titulo" required>
    </p>
</div>

<div class="w3-container" style="padding-left: 20px;">                                       
    <p>
        <label>Data</label> <br>
        <input style="width: 120px; height: 40px; height: 40px; border-top: 1px; border-left: 1px; border-right: 1px; border-color: lightgray;" type="text" name="data" id="data" placeholder="dd/mm/aaaa"
        maxlength="10" onkeypress="mascaraData( this, event )" required>
    </p>    
</div>  

<div class="w3-container" style="padding-left: 20px;">                                       
    <p>
        <label>Imagem</label> <br>
        <input style="padding-top: 10px;" type="file" id="imagem" name="img[]" required>
    </p>                                
</div>

<?php if($nivelLogado == 1) { ?>
<div class="w3-container" style="padding-left: 20px;">                                       
    <p>
        <label>Exibir</label> <br>
        <select style="width: 80px; height: 35px;" id="exibir" name="exibir">
            <option>Sim</option>
            <option>Não</option>
        </select>
    </p>     
</div>  
<?php } ?>                                     

<div class="w3-container" style="padding-left: 20px;">                                                           
    <p>
        <textarea style="width: 1000px;" name="descricao" id="descricao" rows="15"></textarea>
    </p>
</div>

<div class="w3-container" style="padding-left: 20px;">                                                                         
    <table>
        <tr>
            <td style="padding-bottom: 30px;">
                <p><input style="width:100px; height:40px; background:#333; color:white; border:none;"
                type="reset" name="cancelar" value="Cancelar" class="w3-button w3-black w3-section"/></p>
            </td> 
            <td style="padding-bottom: 30px; padding-left: 10px;">
                <p><input style="width:100px; height:40px; background:#333; color:white; border:none;"
                type="submit" name="cadastrar" value="Salvar" class="w3-button w3-black w3-section"/></p>
            </td>
        </tr>
    </table>
</div>

<div class="w3-container" style="padding-left: 20px;">                                       
</form>  


                    </div> <!-- /widget-content -->
                </div> <!-- /widget -->
            </div><!-- span 12 -->


    </div><!-- row -->        



        </div>
        <!-- /span6 --> 
      </div>
      <!-- /row --> 
    </div>
    <!-- /container --> 
  </div>
  <!-- /main-inner --> 
</div>
<!-- /main -->
</div>
</div>
</div>
</div>

<script type="text/javascript" src="editor/nicEdit.js"></script>
<script type="text/javascript"> bkLib.onDomLoaded(function() { nicEditors.allTextAreas() }); </script>
<script type="text/javascript">  
function mascaraData( campo, e )
{
    var kC = (document.all) ? event.keyCode : e.keyCode;
    var data = campo.value;

    if( kC!=8 && kC!=46 )
    {
        if( data.length==2 )
        {
            campo.value = data += '/';
        }
        else if( data.length==5 )
        {
            campo.value = data += '/';
        }
        else
            campo.value = data;
    }
}
</script>

However descricao is not being saved in the database.

Note: I have other registration systems: login, comments, etc and are working correctly.

  • Is there a mistake in $conexao->errorInfo (after the prepare)?

  • Puts the table structure.

  • Probably the error is in the table column name or the size defined in it is smaller than the value sent, for example, has been set descricao varchar(25) and sent a string with 30 characters

1 answer

1


You are using a plugin that does not send the value of textearea. It replaces the textarea by a div editable, soon the textarea does not contain the typed text.

To circumvent this, you can by submitting the form, insert the content of the div in the textarea instantiated by the plugin.

Enter an event onclick on the button submit to call a function that will do this:

               função
                 ↓
<input onclick="nic()" style="width:100px; height:40px; background:#333; color:white; border:none;" type="submit" name="cadastrar" value="Salvar" class="w3-button w3-black w3-section"/>

And the function in script:

<script>
function nic(){
   var texto = nicEditors.findEditor('descricao').nicInstances[0].editorContain.innerHTML;
   document.body.querySelector("#descricao").value = texto;
}
</script>

In this way PHP:

$descricao = $_POST['descricao'];

will receive the value of textarea name="descricao" normally.

Browser other questions tagged

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