PHP 学習リファレンス - フォームの作成

2020/4/21 23:51
-
$_GET と　＄_POST と　＄_REQUEST
https://blog.csdn.net/u013372487/article/details/46009075

-安全　P>G
-データ量の大きさ　P>G
-ブックマークの場合　Gの方が便利

2020/4/25 18:31
-
フォーム：

```php
$_SERVER["PHP_SELF"] //current php page (form action object)
$_SERVER["REQUEST_METHOD"]=="POST" //get form method
empty($_POST["gender"]) //empty function
```

文字列をチェックする

```php
if(!preg_match("/^[a-zA-Z ]*$/",$name)) //if not name
if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$mail)) //if not email
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$site)) //if not website

```


ref: https://www.runoob.com/php/php-form-url-email.html

2020/4/26 21:32
-

コード

```php
<!DOCTYPE html>

<head>
    <title>PHP FORM SAMPLE</title>
    <style>.error {color: #FF0000;}</style>
</head>

<body>
    <h1>PHP FORM SAMPLE</h1><br>
    <hr/>
    <h2>Form</h2>
    <span class = "error">*Required contents</span><br>
    <?php
        $name = $gender = $website = $email = $comment = "";
        $nameErr = $genderErr = $websiteErr = $emailErr = "";

        if($_SERVER["REQUEST_METHOD"] == "POST"){
            if(empty($_POST["Name"])){
                $nameErr = "Insert name.";
            }
            else{
                $name = text_input($_POST["Name"]);
                if(!preg_match("/^[a-zA-Z ]*$/",$name)){
                    $nameErr = "Name format required.";
                }
            }

            if(empty($_POST["Email"])){
                $emailErr = "Insert E-mail.";
            }
            else{
                $email = text_input($_POST["Email"]);
                if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
                    $emailErr = "E-mail format required.";
                }

            }

            if(empty($_POST["Website"])){
                $website = "";
            }
            else{
                $website = text_input($_POST["Website"]);
                if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)){
                    $websiteErr = "*Website format required.";
                }

            }

            $comment = text_input($_POST["Comment"]);

            if(empty($_POST["Gender"])){
                $genderErr = "Select gender.";
            }
            else{
                $gender = text_input($_POST["Gender"]);
            }

        }
        function text_input($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
    ?>
    <form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" method = "POST">
        Name:<input type = "text" name = "Name"><span class = "error">*<?php echo $nameErr;?></span><br>
        E-mail:<input type = "text" name = "Email"><span class = "error">*<?php echo $emailErr;?></span><br>
        Website:<input type = "text" name = "Website"><span class = "error"><?php echo $websiteErr;?></span><br>
        Comment:<br><textarea name = "Comment" cols = "40" rows = "5"></textarea><br>
        Gender:
        <input type = "radio" value = "Female" name = "Gender" <?php if(isset($_POST["Gender"]) && $_POST["Gender"] == "Female") {echo "checked";}?>>Female
        <input type = "radio" value = "Male" name = "Gender" <?php if(isset($_POST["Gender"]) && $_POST["Gender"] == "Male") {echo "checked";}?>>Male<span class = "error">*<?php echo $genderErr;?></span><br>
        <br>
        <input type = "submit" value = "Submit"><br>
    </form>
    <hr />
    <h2>Insert info:</h2><br>
    Name: <?php echo $name;?><br>
    E-mail: <?php echo $email;?><br>
    Website: <?php echo $website;?><br>
    Comment: <?php echo $comment;?><br>
    Gender: <?php echo $gender;?><br>

</body>
</html>

```