You are advised to check corresponding YouTube video at the end of this article.
An index is a numeric value used to identify and access elements within an array. Each element in an array is associated with a unique index, which is used to retrieve or modify the element's value.
<?php
// Positions: 0 1 2
$programs = array("calc", "notepad", "mspaint");
echo "First element: " . $programs[0] . "<br>";
echo "Second element: " . $programs[1]. "<br>";
echo "Third element: " . $programs[2]. "<br>";
?>
This PHP code declares an indexed array called $programs
and assigns it three string values: "calc", "notepad", and "mspaint".
Then, the code uses the index of each element to access and print their values using echo
. The first element, which is "calc", is accessed using $programs[0]
, the second element, which is "notepad", is accessed using $programs[1]
, and the third element, which is "mspaint", is accessed using $programs[2]
.
In PHP, as well as in many other programming languages, indexing starts from 0 by convention.
form.html
<form action="grabthings.php" method="post">
<label>Name:</label>
<input type="text" name="name">
<br><br>
<label>Lastname:</label>
<input type="text" name="lastname">
<br><br>
<label>Years:</label>
<input type="text" name="years">
<br><br>
<input type="submit" value="Submit">
</form>
This HTML form will create a form with three input fields for the user to enter their name, last name, and age. The form will use the HTTP POST method to send the data to the server-side script "grabthings.php" for processing.
The input fields will be labeled with "Name:", "Lastname:", and "Years:". Each input field will have a corresponding "name" attribute that will be used to identify the input field and its value when the form is submitted.
The form will include a "Submit" button that the user can click to submit the form data. When the button is clicked, the form data will be sent to the "grabthings.php" script using the POST method, and the script will be responsible for processing the data and performing any necessary actions.
grabthings.php
<?php
$get_name = $_POST["name"];
$get_lastname = $_POST["lastname"];
$get_years = $_POST["years"];
$data = array($get_name, $get_lastname, $get_years);
echo "Name: $data[0]" . "<br>";
echo "Lastname: $data[1]" . "<br>";
echo "Years: $data[2]" . "<br>";
?>
This PHP script retrieves data from an HTML form submitted via POST method and assigns it to variables $get_name, $get_lastname, and $get_years using the $_POST superglobal. It then creates an array called $data, which includes the values of these variables.
Finally, the script outputs the values of the array using echo statements.
The output shows the submitted name, lastname, and years.
No comments:
Post a Comment