1

フィールドが入力されていない場合にエラーを表示する単純なフォームを作成したい。やり方がわかりません。これが私のコードです:phpコード:

<?php

    //include the connection file

    require_once('connection.php');

    //save the data on the DB and send the email

    if(isset($_POST['action']) && $_POST['action'] == 'submitform')
    {
        //recieve the variables

        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $ip = gethostbyname($_SERVER['REMOTE_ADDR']);

        //save the data on the DB

        mysql_select_db($database_connection, $connection);

        $insert_query = sprintf("INSERT INTO feedback (name, email, message, date, ip) VALUES (%s, %s, %s, NOW(), %s)",
                                sanitize($name, "text"),
                                sanitize($email, "text"),
                                sanitize($message, "text"),
                                sanitize($ip, "text"));

        $result = mysql_query($insert_query, $connection) or die(mysql_error());

        if($result)
        {
            //send the email

            $to = "abc@xyz.com";
            $subject = "New message from the website";

            //headers and subject
            $headers  = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers .= "From: ".$name." <".$email.">\r\n";

            $body = "New contact<br />";
            $body .= "Name: ".$name."<br />";
            $body .= "Email: ".$email."<br />";
            $body .= "Message: ".$message."<br />";
            $body .= "IP: ".$ip."<br />";

            mail($to, $subject, $body, $headers);

            //ok message

            echo "Your message has been sent";
        }
    }

    function sanitize($value, $type) 
    {
      $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value;

      switch ($type) {
        case "text":
          $value = ($value != "") ? "'" . $value . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $value = ($value != "") ? intval($value) : "NULL";
          break;
        case "double":
          $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";
          break;
        case "date":
          $value = ($value != "") ? "'" . $value . "'" : "NULL";
          break;
      }

      return $value;
    }
    ?>

<form id="ContactForm" method="post" action="mail.php">
                            <div class="wrapper"><input class="input" name="name" id="name" type="text" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" ></div>
                            <div class="wrapper"><input class="input" name="email" id="email" type="text" value="E-mail:" onBlur="if(this.value=='') this.value='E-mail:'" onFocus="if(this.value =='E-mail:' ) this.value=''" ></div>
                            <div class="textarea_box"><textarea cols="1" rows="1" onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''" >Message:</textarea></div>
                            <input type="hidden" id="action" name="action" value="submitform" />
                            <input type="submit" class="button" id="submit" name="submit" value="Submit" /> <input type="reset" class="button" id="reset" name="reset" value="Reset" />
                        </form>
4

2 に答える 2

1

情報をデータベースに保存する前に、送信された各値に有効なデータが含まれているかどうかを確認してください。そうでない場合は、フィールド名を配列に入れます。検証が完了したら、配列が空かどうかを確認します。空の場合は、情報をデータベースに保存します。入力されている場合は、送信されたデータが入力されたフォームを再表示し、どのようなエラーが発生したかを読みやすく通知して、何を修正すればよいかを理解できるようにします。

調べる必要があるいくつかの PHP 関数は次のとおりfilter_var()ですctype_*empty()

参考までに、mysql_* 関数は間もなく廃止されるため、これらの関数からの移行を検討する必要があります。

于 2012-05-31T14:00:22.903 に答える
0

これを実現するには、空のチェックを行うコード ブロックを作成する必要があります (チェックするのはこれだけであるため)。簡単なコード スニペットを以下に示します。

function checkFormErrors($name,$email,$message){
    //now we define a function to check for errors
    $errors = array();
    //define an error container
    if(empty($name)){
        //check the name field
        $errors[] = "You need to enter a name";
    }
    if(empty($email)){
        //check the email field
        $errors[] = "You need to enter an email address";
    }
    .... //do more checks for all fields here
    return $errors;
}
if(isset($_POST['action']) && $_POST['action'] == 'submitform'){
    //recieve the variables
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $ip = gethostbyname($_SERVER['REMOTE_ADDR']);

    $errors = checkFormErrors($name,$email,$message);
    //check for errors
    if(empty($errors)){
        //continue with the processing you did above
        .....
    }
}


In you HTML file/page, you then need to put this above the form (feel free to style it
with css)
<?php
    if(isset($errors)){
        //the variable exists
        echo implode("<br />", $errors);
    }
?>

ここにブログ投稿を投稿し、フォームの検証に役立つスクリプトもいくつか用意しています

お役に立てれば!

于 2012-05-31T14:07:13.713 に答える