1

Hola buenas estoy creado un script donde se le pide al usuario un Input donde pueda poner una palabra o frase y que vaya disminuyendo la cantidad de caracteres.

pero me sale del todo bien
<?php 
if(isset($_POST['enviar'])){
if(isset($_POST['texto'])){
$texto = trim($_POST['texto']);
$count = strlen($texto);
} 
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>
<form method="post">
<input type="text" placeholder="Texto aqui" name="texto" id=""> <br>
<input type="submit" name="enviar" value="Enviar"><br>
</form>
<table>
<tr>
<th>Numero</th>
<th>Frase</th>
</tr>

<?php 
for ($i=1; $i < $count; $i++) { 
echo '<tr>
<td>'.$i.'</td>
<td>'.substr($texto,$i-1).'</td>
</tr>'; 
}

?>

</table>

</body>
</html>
0

2 respuestas 2

1

Sé que la pregunta tiene un aire de ir por la solución en PHP, sin embargo... Para quienes busquen una alternativa con JavaScript y algo de jQuery:

$(document).ready(function() {
    var text_max = 99;
    $('#textarea_feedback').html('Quedan ' + text_max + ' caracteres');

    $('#textarea').keyup(function() {
        var text_length = $('#textarea').val().length;
        var text_remaining = text_max - text_length;

        $('#textarea_feedback').html('Quedan ' + text_remaining + ' caracteres');
    });
});
html { margin:11px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="textarea" rows="8" cols="30" maxlength="99" ></textarea>
<div id="textarea_feedback"></div>

0

Estas muy cerca de lo que quieres. Solo tienes que verificar que la variable $count este definida y cambiar el < en tu bucle por <=. Algo asi:

<?php 
if(isset($_POST['enviar'])){
if(isset($_POST['texto'])){
$texto = trim($_POST['texto']);
$count = strlen($texto);
} 
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>
<form method="post">
<input type="text" placeholder="Texto aqui" name="texto" id=""> <br>
<input type="submit" name="enviar" value="Enviar"><br>
</form>
<table>
<tr>
<th>Numero</th>
<th>Frase</th>
</tr>

<?php 
if(isset($count)) {
for ($i=1; $i <= $count; $i++) { 
echo '<tr>
<td>'.$i.'</td>
<td>'.substr($texto,$i-1).'</td>
</tr>'; 
}
}
?>

</table>

</body>
</html>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.