September 19, 2017

Core PHP: PHP Forms (7.4)

Forms


Now, when we have an HTML form with the action attribute set to our PHP file, we can access the posted form data using the $_POST associative array.
In the first.php file:
<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br />
Your age: <?php echo $_POST["age"]; ?>

</body>
</html>

The $_POST super global array holds key/value pairs. In the pairs, keys are the names of the form controls and values are the input data entered by the user.
We used the $_POST array, as the method="post" was specified in the form.
To learn more about the form methods, press Continue!

Core PHP: PHP Forms (7.3)

Forms

The action attribute specifies that when the form is submitted, the data is sent to a PHP file named first.php. 
HTML form elements have names, which will be used when accessing the data with PHP.
The method attribute will be discussed in the next lesson. For now, we'll set the value to "post".

Core PHP: PHP Forms (7.2)

Forms

The purpose of the PHP superglobals $_GET and $_POST is to collect data that has been entered into a form.
The example below shows a simple HTML form that includes two input fields and a submit button:
<form action="first.php" method="post">
  <p>Name: <input type="text" name="name" /></p>
  <p>Age: <input type="text" name="age" /></p>
  <p><input type="submit" name="submit" value="Submit" /></p>
</form>

Result:
figure: PHP forms
Figure: PHP forms

August 13, 2017

Core PHP: $_SERVER Variables: Host Name (7.1)

$_SERVER


$_SERVER['HTTP_HOST'] returns the Host header from the current request.
<?php
echo $_SERVER['HTTP_HOST'];
//Outputs "localhost"
?>

This method can be useful when you have a lot of images on your server and need to transfer the website to another host. Instead of changing the path for each image, you can do the following:
Create a config.php file, that holds the path to your images:
<?php
$host = $_SERVER['HTTP_HOST'];
$image_path = $host.'/images/';
?>

Use the config.php file in your scripts:
<?php
require 'config.php';
echo '<img src="'.$image_path.'header.png" />';
?>

The path to your images is now dynamic. It will change automatically, based on the Host header.

This graphic shows the main elements of $_SERVER.
Figure: the main elements of $_SERVER
Figure: the main elements of $_SERVER

Core PHP: $_SERVER Variables: Script Name (7.0)

Predefined Variables


A superglobal is a predefined variable that is always accessible, regardless of scope. You can access the PHP superglobals through any function, class, or file.

PHP's superglobal variables are $_SERVER, $GLOBALS, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION.

$_SERVER

$_SERVER is an array that includes information such as headers, paths, and script locations. The entries in this array are created by the web server.
$_SERVER['SCRIPT_NAME'] returns the path of the current script:
<?php
echo $_SERVER['SCRIPT_NAME'];
//Outputs "/somefile.php"
?>

Our example was written in a file called somefile.php, which is located in the root of the web server.