Click here to Skip to main content
15,907,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to PHP and trying to count all the uppercase letters in the text area, thought I am not able get anything when I hit the 'submit' button. Here is my code :

What I have tried:

PHP
<pre><!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['submit'])) {
 function caps($s) {
   $u = 0;
   $d = 0;
   $n = strlen($s);

   for ($x=0; $x<$n; $x++) {
       $d = ord($s[$x]);
       if ($d > 64 && $d < 91) {
           $u++;
       }
   } 

   return $u;
   }
  $n1=$_POST['n1'];
     echo 'caps: ' .  caps($n1) . "\n";
}
?>
  <form><textarea rows="4" cols="50" name="n1" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></textarea>
 <br><input type="submit" name="submit" value="Submit"></form>
 </body>
 </html>
Posted
Updated 25-Feb-17 18:45pm

You forgot to add the form method of post:
<form method="post">
At the moment, it is using the default get method and your PHP script is expecting $_POST variable. Learn Forms in HTML documents[^]
 
Share this answer
 
v2
The code look fine. Just need a small changes
1. Add POST to the form
2. move the if(isset($_POST['submit'])) { at the end of the function

HTML
<!DOCTYPE html>
<html>
<body>
<?php

 function caps($s) {
   $u = 0;
   $d = 0;
   $n = strlen($s);

   for ($x=0; $x<$n; $x++) {
       $d = ord($s[$x]);
       if ($d > 64 && $d < 91) {
           $u++;
       }
   } 

   return $u;
   }
if(isset($_POST['submit'])) {
  $n1=$_POST['n1'];
     echo 'caps: ' .  caps($n1) . "\n";
}
?>
  <form method="POST"><textarea rows="4" cols="50" name="n1" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></textarea>
 <br><input type="submit" name="submit" value="Submit"></form>
 </body>
 </html>
 
Share this answer
 
Another tip for you: Naming methods/variables is just as important as the code itself - it makes the code more readable and easier to debug. Correct spacing too.
HTML
<!DOCTYPE html>
<html>
<body>
<?php

if(isset($_POST['submit'])) {
    function CountUppercase($text) {
        $count = 0;
        $character = 0;
        $length = strlen($text);

        for ($index = 0; $index < $length; $index++) {
                $character = ord($text[$index]);
                if ($character > 64 && $character < 91) {
                    $count++;
                }
        } 
        return $count;
    }
    $n1=$_POST['n1'];
    echo 'caps: ' .  caps($n1) . "\n";
}

?>
    <form method="post">
        <textarea rows="4" cols="50" name="n1" value="<?php if(isset($_POST['n1'])){echo htmlspecialchars($_POST['n1']);}?>"></textarea><br>
        <input type="submit" name="submit" value="Submit">
    </form>
  </body>
</html
 
Share this answer
 
v3
you can remove this line of code:
PHP
$d = 0;

because the value is never used.
 
Share this answer
 
Comments
Graeme_Grant 26-Feb-17 0:51am    
Are you sure?
$d = ord($s[$x]);
Patrice T 26-Feb-17 1:09am    
Yes, the value 0 is never used. I didn't say the variable isn't used.
Graeme_Grant 26-Feb-17 1:09am    
:P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900