Character counter in input with PHP

1

Hello, I'm creating a script where the user is asked for an Input where he can put a word or phrase and that decreases the number of characters.

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>
    
asked by Matias 13.07.2018 в 01:20
source

2 answers

0

You are very close to what you want. You just have to verify that the variable $count is defined and change the < in your loop by <= . Something like this:

<?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>
    
answered by 13.07.2018 / 01:27
source
1

I know that the question has an air of going for the solution in PHP, however ... For those looking for an alternative with JavaScript and some 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>
    
answered by 13.07.2018 в 01:38