gpt4 book ai didi

php - 我的php表单未检查在表单中输入的电子邮件是否有效

转载 作者:行者123 更新时间:2023-12-03 08:57:01 26 4
gpt4 key购买 nike

在以下几件事上,我需要一些帮助:

  • 我有一个称为isValid的函数,该函数不检查在表单中输入的电子邮件是否有效?
  • 如何在表单的文本字段内显示错误消息?

  • 任何帮助是极大的赞赏!

    下面是我的代码:
     <?php 

    //Set Variables to Empty String
    $Email = " ";
    $Subject = " ";
    $Name = " ";
    $Message = " ";

    $error = " ";


    if(isset($_POST['submit']) )
    {

    if (empty($_POST["Email"]))
    {
    $error = "** Enter a valid email";
    }
    else
    {
    $Email = isValid($_POST["Email"]);
    }
    if (empty($_POST["Subject"]))
    {
    $error = "** Enter a subject";
    }
    else
    {
    $Subject= test_input($_POST["Subject"]);
    }
    if (empty($_POST["Name"]))
    {
    $error = "** Enter your name";
    }
    else
    {
    $Name= test_input($_POST["Name"]);
    }
    if (empty($_POST["Message"]))
    {
    $error = "** Enter your message";
    }
    else
    {
    $Message= test_input($_POST["Message"]);
    }
    }


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

    //isValid checks if email address is a valid one
    function isValid($edata)
    {
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]* [[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][ 0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $edata));
    }

    ?>

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

    <p><label>From (Email):</label></p>
    <input type="text" size="35" name="Email">
    <span class="error"><?php echo $error;?></span>

    <br><br>

    <p><label>Subject:</label></p>
    <input type="text" size="35" name="Subject">
    <span class="error"><?php echo $error;?></span>

    <br><br>

    <p><label>Name:</label></p>
    <input type="text" size="35" name="Name">
    <span class="error"><?php echo $error;?></span>

    <br><br>

    <p><label>Message:</label></p>
    <textarea type="text" cols="38" rows="6" name="Message"></textarea>
    <span class="error"><?php echo $error;?></span>

    <br><br>

    <input type="submit" name="submit" value="submit">
    <input type="reset" value="Reset">

    </form>
    <?php

    if(empty($error))
    {
    // the email will be sent here
    $to = "@gmail.com";

    // the email subject
    $subject = 'Message from XXXX website from: ' . $Name;

    // the mail message

    $msg .= "\r\nEmail: $Email";
    $msg .= "\r\n\nSubject: $Subject";
    $msg .= "\r\n\nName: $Name";
    $msg .= "\r\n\nMessage: $Message";


    mail($to, $subject, $msg, "From: $Email\r\nReply-To: $Email\r\nReturn-Path: $Email\r\n");
    }

    ?>

    <p>Thank you <b><?=$Name;?></b> for your message. Expect a response in 1 - 3 business days</p>

    最佳答案

    validate functions

    <?php
    $email_a = 'joe@example.com';
    $email_b = 'bogus';

    if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
    }
    if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.";
    }
    ?>

    关于php - 我的php表单未检查在表单中输入的电子邮件是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21441025/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com