Tuesday, April 22, 2025

PHP For Loop, Html Form

You are advised to check corresponding YouTube video at the end of this article.

form.html


<form action="grabthings.php" method="post">
<label>Lower:</label>
<input type="text" name=lower>
<br><br>

<label>Upper:</label>
<input type="text" name=upper>
<br><br>

<input type="submit" value="Submit">
</form>

This HTML form is used to collect data from the user and send it to the server using the POST method.

The form has two text input fields named "lower" and "upper", which allow the user to enter lower and upper bounds for a range of values. These fields are labeled using the HTML <label> element.

The form also has a submit button labeled "Submit". When the user clicks this button, the form data is sent to a PHP script located at "grabthings.php" using the POST method.

grabthings.php


<?php 

$get_lower = $_POST["lower"];
$get_upper = $_POST["upper"];

for ($x = $get_lower; $x <= $get_upper; $x = $x + 1) {
	echo "x at the moment: <b>$x</b>";
	echo "<br>";
}

if ($get_lower >= $get_upper) {
	echo "<h2>Error: Lower number must be smaller than Upper</h2>";
}

?>

This PHP script is used to process the data submitted by the HTML form that collects the lower and upper bounds of a range of values.

The script first retrieves the lower and upper bounds from the $_POST superglobal array, using the keys "lower" and "upper" that correspond to the name attributes of the input fields in the HTML form.

Next, the script uses a for loop to iterate through the range of values from the lower bound to the upper bound. The loop uses the retrieved values of the lower and upper bounds as the start and end points of the loop, respectively. For each iteration, the current value of x is printed to the screen using the echo statement, along with some HTML tags to format the output.

The script checks whether the lower bound is greater than or equal to the upper bound. If so, it prints an error message to the screen to indicate that the input is invalid.

This script demonstrates how to process form data in PHP and use it to perform a task such as iterating through a range of values. It also includes error checking to handle cases where the input data is invalid.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...