Create a PHP file and use it to render simple web valid web page.
Create following properties and render them. Do one property at a time. You can employ following fragment of code.
You can upload the file to webik. Alternatively you can run following command to run PHP server locally. You have to run the command in directory where you PHP files are located.
PHP -S 127.0.0.1:8080
Always use htmlspecialchars to sanitize output of dynamic content.
You can extend previous code or create a new PHPfile. Utilize $_GET global property to access URL query arguments. Implement following functionality (Demo).
Remember you can utilize var_dump to get debug output for a variable, employ http_build_query to build URL query part, and use filter_input to read values from $_GET.
$counter = filter_input(INPUT_GET, "counter", FILTER_VALIDATE_INT);
if ($counter !== false && $counter !== null) {
echo("Variable is an integer");
} else {
echo("Variable is not an integer");
}
Create two files: `form.php` contain HTML form, `submit.php` handles submissions.
The form uses GET method and submits (using action) to the `submit.php file. The form must contain the following fields: name, age, luck, and inteligence. Where age, luck and intelligence are numbers between 0 and 100.
In the `submit.php` read arguments and check that all data were provided and that they meet requirements from the previous section. Render a page with information about invalid data and link back to the original article. If data are valid render a hello message based on age:
Demo.
Build on the previous exercise. When a user submits invalid data redirect the user back to the original form. You can utilize header and exit functions. Setting the location header automatically sets the response code. The redirect code may look like the example below:
header("Location: ./form.php?name=petr&luck=30&invalid=submit");
exit();
Modify `form.php` so that it loads form values from the URL query and uses them during rendering the form. In addition, use an additional URL query argument (like in the example above) to indicate invalid submission.
As a result, when I provide invalid values to the form and submit it, I get redirected back to the form. Yet the form contains the values as I provided them. In addition, there is the message that a submission failed.
Change the form method to POST. Try to submit the form and refresh the page. Make sure you understand what is happening and why.
Create a new page `success.php` and redirect to this page from `submit.php` for successful submit. You need to move the rendering of the submission message and utilize the URL query to pass the state.
Merge all files into one while preserving the same functionality. You may need to add some more query arguments. Utilize $_SERVER['REQUEST_METHOD'] to read the HTTP method.
Leave an anonymous feedback.