Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // define variables and set to empty values
- $imieErr = $nazwiskoErr = $wiekErr = $adresEmailErr = $hasloErr = $hasloRepeatErr = "";
- $imie = $nazwisko = $wiek = $adresEmail = $haslo = $hasloRepeat = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- //imie
- if (empty($_POST["imie"])) {
- $imieErr = "imie is required";
- } else {
- $imie = test_input($_POST["imie"]);
- // check if imie only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z-' ]*$/",$imie)) {
- $imieErr = "Only letters and white space allowed";
- }
- }
- //nazwisko
- if (empty($_POST["nazwisko"])) {
- $nazwiskoErr = "nazwisko is required";
- } else {
- $nazwisko = test_input($_POST["nazwisko"]);
- // check if nazwisko only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z-' ]*$/",$nazwisko)) {
- $nazwiskoErr = "Only letters and white space allowed";
- }
- }
- //wiek
- if (empty($_POST["wiek"])) {
- $wiekErr = "wiek is required";
- } else {
- $wiek = test_input($_POST["wiek"]);
- // check if wiek only contains numbers
- if (!is_numeric($wiek)) {
- $wiekErr = "wiek is not a number";
- }
- }
- //adresEmail
- if (empty($_POST["adresEmail"])) {
- $adresEmailErr = "adresEmail is required";
- } else {
- $adresEmail = test_input($_POST["adresEmail"]);
- // check if e-mail address is well-formed
- if (!filter_var($adresEmail, FILTER_VALIDATE_EMAIL)) {
- $adresEmailErr = "Invalid adresEmail format";
- }
- }
- //haslo
- if (strcmp($_POST["haslo"], $_POST["hasloRepeat"]) == 0) {
- if (empty($_POST["haslo"])) {
- $hasloErr = "haslo is required";
- } else {
- $haslo = test_input($_POST["haslo"]);
- }
- if (empty($_POST["hasloRepeat"])) {
- $hasloRepeatErr = "you need to repeat password";
- } else {
- $hasloRepeat = test_input($_POST["hasloRepeat"]);
- }
- } else {
- $hasloErr = "Passwords must match!";
- $hasloRepeatErr = "Passwords must match!";
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <style>
- #lubiemBox {
- justify-content: center;
- align-items: center;
- }
- .error {
- color: red;
- }
- </style>
- <h2>Formularz fajny</h2>
- <p><span class="error">* wymagane pole</span></p>
- <form method="post" action="">
- <!-- imie -->
- Imię: <input type="text" name="imie" placeholder="Jan" value="<?php echo $imie;?>">
- <span class="error">* <?php echo $imieErr;?></span>
- <br><br>
- <!-- nazwisko -->
- Nazwisko: <input type="text" name="nazwisko" placeholder="Kowalski" value="<?php echo $nazwisko;?>">
- <span class="error">* <?php echo $nazwiskoErr;?></span>
- <br><br>
- <!-- wiek -->
- Wiek: <input type="text" name="wiek" placeholder="18" value="<?php echo $wiek;?>">
- <span class="error">* <?php echo $wiekErr;?></span>
- <br><br>
- <!-- adresEmail -->
- E-mail: <input type="text" name="adresEmail" placeholder="[email protected]" value="<?php echo $adresEmail;?>">
- <span class="error">* <?php echo $adresEmailErr;?></span>
- <br><br>
- <!-- haslo -->
- Hasło: <input type="password" name="haslo" placeholder="**********" value="<?php echo $hasloEmail;?>">
- <span class="error">* <?php echo $hasloErr;?></span>
- <br><br>
- Powtórz hasło: <input type="password" name="hasloRepeat" placeholder="**********" value="<?php echo $hasloRepeatEmail;?>">
- <span class="error">* <?php echo $hasloRepeatErr;?></span>
- <br><br>
- <!-- checkbox -->
- <div id="lubiemBox">
- Co lubisz: <br>
- <input type="checkbox" name="lubiem[]" id="lubiem" value="Placki"> Placki<br>
- <input type="checkbox" name="lubiem[]" id="lubiem" value="Kasztany"> Kasztany<br>
- <input type="checkbox" name="lubiem[]" id="lubiem" value="Pyry"> Pyry<br>
- <input type="checkbox" name="lubiem[]" id="lubiem" value="Kartofelki"> Kartofelki
- <br><br>
- </div>
- <!-- radio -->
- <div id="nieLubiemBox">
- Czego nie lubisz: <br>
- <input type="radio" name="nieLubiem" id="nieLubiem" value="Ananas na picy"> Ananas na picy<br>
- <input type="radio" name="nieLubiem" id="nieLubiem" value="Brokuly"> Brokułów<br>
- <input type="radio" name="nieLubiem" id="nieLubiem" value="Bruksela"> Brukseli<br>
- <br><br>
- </div>
- <!-- submit -->
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $imie;
- echo "<br>";
- echo $nazwisko;
- echo "<br>";
- echo $wiek;
- echo "<br>";
- echo $adresEmail;
- echo "<br>";
- echo $haslo;
- echo "<br>";
- $fajenowix = $_POST['lubiem'];
- if(isset($_POST['lubiem'])) {
- echo "Lubisz: ";
- foreach ($fajenowix as $elomix) {
- echo $elomix.", ";
- }
- } else {
- echo "Nie wybrałeś co lubisz.";
- }
- echo "<br>";
- $siemanix = $_POST['nieLubiem'];
- if(isset($_POST['nieLubiem'])) {
- echo "Nie lubisz: ";
- echo $_POST['nieLubiem'];
- } else {
- echo "Nie wybrałeś czego nie lubisz.";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment