普通网友 2017-04-17 00:42
浏览 150
已采纳

php电子邮件表单/ if / else if / error messages

I have tried to create php email form. The form was basically working, but I wanted to add validation functions like I did on 'name' However, It doesn't work when I empty 'name' and just sent an email. Any help would be really appreciated.

htmlfile

<form action="php_mini.php" method="post">

    Name:
    <input type="text" name="name">
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>

    Email:
    <input type="text" name="email">
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>

    Phone:
    <input type="text" name ="phone">
    <br><br>

    Comment:
    <!--<textarea name="comment"></textarea>-->
    <!--<input type = "text" name = "comment">-->
    <textarea name="comment" rows="5" cols="40"></textarea>
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>

    <input type="submit" value="Submit">
</form>

php

echo '<pre>';
print_r( $_POST );
echo '</pre>';

$headers = 'MIME-Version: 1.0' . "
" .
       'Content-type: text/html; charset=iso-8859-1' . "
" . 
       'From:  Design_customers <customers.com' . " 
" .
       //'Reply-To: vader@deathstar.com' . "
" .
       'X-Mailer: PHP/' . phpversion();
//error msg
$nameErr = $emailErr = $phoneErr = "";
$name = $email = $comment = $phone = "";    

//receiver   

if (empty($_POST["name"])) {
    $nameErr = "Name is required";
} else {
    $name = test_input($_POST["name"]);
}

$email = $_POST['email'];
/*$message = $_POST['comment'];*/
$message = 'You got a message from a customer.:

Name: '.$_POST['name'].' 
Email: '.$_POST['email'].'
Phone: '.$_POST['phone'].'
Comment: '.$_POST['comment'];
//sender
$from = 'From: Customer'; 

$subject = 'Customer Inquiry';

mail( $email, $subject, $message, $from );
  • 写回答

1条回答 默认 最新

  • dongqie2010 2017-04-17 02:36
    关注

    use if(isset($_POST["name"])) to stop the error. [Notice: Undefined index: name in C........]

    better you have validate this form with java Script. i have attached a snipped part of you form with javascript.

    <form action="" method="post" onsubmit="return(Validate());" name="myform">
    
        Name:
        <input type="text" name="name">
        <span class="error">* <div id="name_error" style="color:red;"></div> </span>
        <br><br>
    
    </form>
    
    
    
    
    
    <script type="text/javascript">
       <!--
          // Form validation code will come here.
    
          function Validate()
          {
    
             if( document.myform.name.value == "" )
             {
                alert( "Please provide your name!" );
    
                name_error.textContent = "Name is Required.!"; 
                document.myform.name.focus() ;
                return false;
             }
    
    
             return( true );
          }
       //-->
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题