Keresés

Új hozzászólás Aktív témák

  • sztanozs

    veterán

    válasz lanszelot #21060 üzenetére

    Oké, megpróbáltam összedobni W3-ban, de nem jó ott a callback, viszont ez alapján látnod kellene mit kell csinálni:
    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    .error {color: #FF0000;}
    </style>
    </head>
    <body>
    <?php

    // betöltés szimulálása
    $array = array();
    $array["google"] = ['kep' => "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", 'link' => "www.google.com"];
    $array["startlap"] = ['kep' => "https://lap.hu/images/startlap-logo.png", 'link' => "www.lap.hu"];
    // betöltés vége

    // define variables and set to empty values
    $nameErr = $kepErr = $linkErr = "";
    $name = $kep = $link = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
    $nameErr = "Name is required";
    } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z_]*$/",$name)) {
    $nameErr = "Only letters and underscore allowed";
    }
    }

    if (empty($_POST["kep"])) {
    $kepErr = "Email is required";
    } else {
    $kep = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$kep)){
    $kepErr = "Invalid URL format";
    $kep = "";
    }
    }

    if (empty($_POST["link"])) {
    $link = "";
    } else {
    $link = test_input($_POST["link"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$link)) {
    $linkErr = "Invalid URL";
    $link = "";
    }
    }

    if ($name > "" && $kep > "" && $link > "") {
    $array[$name] = ['kep' => $kep, 'link' => $link];
    }

    }

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    ?>

    <h2>PHP Form Validation Example</h2>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    Name: <input type="text" name="name" value="">
    <br><br>
    Kep: <input type="text" name="kep" value="">
    <br><br>
    Link: <input type="text" name="link" value="">
    <br><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    <span class="error"><?php echo $nameErr;?></span>
    <br><br>
    <span class="error"><?php echo $kepErr;?></span>
    <br><br>
    <span class="error"><?php echo $linkErr;?></span>
    <br><br>

    <textarea>
    <?php
    var_dump($array);
    ?>
    </textarea>
    </body>
    </html>

Új hozzászólás Aktív témák

Hirdetés