Site icon T4Tutorials.com

Length of a string PHP Program with form and database

Length of a string PHP Program with form and database
In this tutorial, we will learn about the followings.

  1. Length of a string- PHP Program
  2. Length of a string PHP Program with form and database

Length of a string- PHP Program

In this example, we will take the inputs from the user and store them in variables.

<html>
<head><title>Reverse String</title></head>

<body>

<form method="post">
Enter String:
<input type="text" name="str"/>
<input type="submit" name="ok"/>

</form>
<?php


if(isset($_POST['ok'])){

$data = $_POST['str'];
echo" <h3>$data</h3> <br/>";
foreach (count_chars($data, 1) as $i => $val) {

   echo "There were $val  \"" , chr($i) , "\" in the string.<br/>";
}

}
?>
</body>
</html>

Length of a string PHP Program with form and database

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>
<head><title>Reverse String</title></head>

<body>

<form method="post">
Enter String:
<input type="text" name="str"/>
<input type="submit" name="ok"/>

</form>
<?php

if(isset($_POST['ok'])){

$data = $_POST['str'];
echo" <h3>$data</h3> <br/>";
$fr=0;
foreach (count_chars($data, 1) as $i => $val) {

$fr=$fr+$val;
   echo "$fr There were $val  \"" , chr($i) , "\" in the string.<br/>";
}

$conn = new mysqli("localhost", "root", "","test");

$sql = "INSERT INTO table1 (StringInput, Frequency) VALUES ('$data', '$fr')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

}
?>
</body>
</html>

Database

Figure: Length of a string PHP Program with form and databases
Figure: length of a number program in Javascript JS with the flowchart.
Exit mobile version