Treatment of a textarea - PHP

0

If I submit an opinion about a reservation and the textarea of opinion is empty, you should not allow the opinion to be sent.

The problem is that although it is empty if it accepts the spaces as a " opinion ", because it does not recognize it as empty. (empty).

You should only be allowed to submit the opinion if you have a minimum of 5-10 characters or at least have a letter type, not counting spaces (because the spaces counts them as text).

Code:

echo "<textarea rows='4' cols='56' id='texto_opinion' name='texto_opinion'></textarea>";
echo "<input type='submit' name='add_opinion' id='add_opinion' value='Añadir opinión'>";

if(isset($_POST["add_opinion"])){
        if(isset($_POST['texto_opinion']) && !empty($_POST['texto_opinion'])){
            $idemail = $cliente;
            $idcabana = $_POST["a_anadir"];
            $opinion = $_POST["texto_opinion"];
            $fecha = new DateTime();
            $fecha_hoy = $fecha->format ('Y/m/d');
            $valoracion = isset($_POST["estrellas"])?$_POST["estrellas"]: "0"; //Sin marcar estrellas, valoracion=0.
            BD::insertarOpinionPorReserva($idemail, $idcabana, $opinion, $fecha_hoy, $valoracion);
        }else{
            echo '<script language="javascript">alert("¡Debes escribir una opinión!");</script>';
        }
    }
    
asked by omaza1990 03.01.2018 в 23:47
source

5 answers

3

Your problem may be resolved in the first instance by applying the trim () function, but this function will delete only the spaces at the beginning and end of the text.

But what happens if spaces enter between two letters? that is, m e applying trim () would return more than 5 when there are only two letters. For this I propose to replace first more than 1 blank space for only 1 and then perform the validation, This could be done by means of the function preg_replace () using the regular expression \s+

Another recommendation might be that the range of characters for a customer's opinion is not so short. (for the example it will be between 5 and 20)

Code

if(isset($_POST["add_opinion"])){
    if(isset($_POST['texto_opinion'])){
        $entrada = preg_replace('/\s+/', ' ', $_POST['texto_opinion']);
        if(strlen($entrada) >  4 && strlen($entrada) < 21){
            $valoracion = isset($_POST["estrellas"])? $_POST["estrellas"]: "0";
            echo "Gracias por Agregar su valoración :  " . $valoracion;
            echo "<br>";
            echo "Gracias por su Opinión ". $entrada;
        }
        else{
            echo "Debe agregar una Opinión Correcta para agregar la valoración  Min 5 - Max  20";
        }
    }
}
    
answered by 04.01.2018 / 03:58
source
2

To solve your problem you must store the information in a variable and then verify that it is not empty, as follows:

echo "<textarea rows='4' cols='56' id='texto_opinion' name='texto_opinion'></textarea>";
echo "<input type='submit' name='add_opinion' id='add_opinion' value='Añadir opinión'>";

if(isset($_POST["add_opinion"])){
    $texto_opcion = trim($_POST['texto_opinion']); /*Almacenas el textarea y le aplicas trim para que elimine los espacios vacios*/
        if($texto_opcion <> '' && strlen($texto_opcion) >= 5 && strlen($texto_opcion) <= 10){
            $idemail = $cliente;
            $idcabana = $_POST["a_anadir"];
            $opinion = $_POST["texto_opinion"];
            $fecha = new DateTime();
            $fecha_hoy = $fecha->format ('Y/m/d');
            $valoracion = isset($_POST["estrellas"])?$_POST["estrellas"]: "0"; //Sin marcar estrellas, valoracion=0.
            BD::insertarOpinionPorReserva($idemail, $idcabana, $opinion, $fecha_hoy, $valoracion);
        }else{
            echo '<script language="javascript">alert("¡Debes escribir una opinión!");</script>';
        }
    }
    
answered by 03.01.2018 в 23:54
0

You can use the attribute required or also pattern to specify what is allowed

    
answered by 03.01.2018 в 23:55
0

Let's see I think you are incorrectly raising your requirements:

  

Only the opinion should be allowed to be sent if it has a minimum of 5-10 characters or at least there is a letter type, without counting spaces

There are contradictory ideas, or you take a minimum of 5 characters or 1 character. I would keep the 5, although the one that requires at least one character is the simplest, if you are using HTML5 , just add the attribute required , the browser will automatically validate:

<textarea
    rows='4'
    cols='56'
    id='texto_opinion'
    name='texto_opinion'
    required></textarea>
<input
    type='submit'
    name='add_opinion'
    id='add_opinion'
    value='Añadir opinión'>

     Well, you do not want to send the textarea without content, that's part customer, the validation with PHP is a separate thing and would occur once the form is sent and the data reaches the server, if this fails you can not launch an alert without plus. For a minimum content of x characters in the textarea you need a bit of JS and some changes:

  • Pressing the "send button" does not send the form, it will launch the JS validations. If the validations pass, the data is sent, otherwise, that's where you can use the alert .

  • To validate the content of the textarea, you must recover it, for example using the ID, and save the content in a variable.

  • Remove blank spaces left over from the variable's content.

  • Count the characters stored in the variable to verify that they are at least the expected length.

  • answered by 04.01.2018 в 00:28
    0

    Place required on the following line.

     echo "<textarea rows='4' cols='56' id='texto_opinion' name='texto_opinion' required></textarea>";
    
        
    answered by 04.01.2018 в 01:28