Tuesday, April 22, 2025

PHP If Else Combinations

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

form.html 


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

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

This code is an HTML form that allows the user to input a number and submit it to the server using the HTTP POST method.

The form has an action attribute that specifies the URL of the script that will handle the form submission. In this case, the script is "grabthings.php".

The form also has a method attribute that specifies the HTTP method to be used for submitting the form data. In this case, the method is "post".

There is a label element that displays the text "Number:" and an input element of type "text" that allows the user to input a number. The name attribute of the input element is "number".

Finally, there is an input element of type "submit" that creates a submit button which the user can click to submit the form data to the server. The value attribute of the submit button is "Submit", which will be displayed on the button.

grabthings.php


<?php 

$get_number = $_POST["number"];

if ($get_number < 100) {
	echo "Number less than 100";	
}
else if ($get_number > 100) {
	echo "Number higher than 100";
}
else {
	echo "Number same, 100"	;
}
 
?>

This code reads the input from a form with one input field named "number". It then checks the value of the input against the number 100 using an if-else statement.

If the value of the input is less than 100, it will print "Number less than 100". If the value of the input is greater than 100, it will print "Number higher than 100". Finally, if the value of the input is exactly 100, it will print "Number same, 100".

This code demonstrates the use of an if-else-if-else statement, which allows for multiple conditions to be checked in a single statement.

form.html


<form action="grabthings.php" method="post">
<label>First Number:</label>
<input type="text" name=first>
<br><br>

<label>Second Number:</label>
<input type="text" name=second>
<br><br>

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

The form contains the following input fields:

  • "First Number": A text input field with the name "first".
  • "Second Number": A text input field with the name "second".
  • "Submit": A submit button that users can click to submit the form.

The PHP script "grabthings.php" can then retrieve the data entered into the form using the $_POST superglobal array and perform various operations with the data

grabthings.php


<?php 

$first_number = $_POST["first"];
$second_number = $_POST["second"];

if ($first_number < $second_number) {
	echo "First Number is smaller";	
}
else if ($first_number > $second_number) {
	echo "First Number is bigger";
}
else {
	echo "Numbers are Same"	;
}
 
?>

In the PHP code, the values of the "first" and "second" fields are retrieved using the $_POST superglobal array. Then, an if-else statement is used to compare the values of the two numbers.

If the first number is smaller than the second number, the code will output "First Number is smaller".

If the first number is larger than the second number, the code will output "First Number is bigger". If the two numbers are equal, the code will output "Numbers are Same".

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 .  ...