在php中验证textarea

时间:2014-12-09 10:39:49

标签: php jquery

我在php中尝试取消订阅表单。我在php中显示错误消息时遇到问题。当单击第三个选项单选按钮时,将显示texatarea,当单击第三个选项时textarea为空时,我希望错误消息显示为文本,并与php中的其他错误消息一起显示。 我如何验证textarea?

这是代码。

   <script type="text/javascript">
        $(function() {
    $(document).ready(function() {
$("#otherAnswer").hide();
        $(".opt").change(function(){
            var id=$(this).attr("id");
            if(id=="mail2")
            {
                $("#otherAnswer").show();
            }
            else
            {
                $("#otherAnswer").hide();
            }
            });


                });


    </script> 
    <?php
     if (isset($_POST['submit'])) {
        $error = "";

    if (!empty($_POST['email'])) {
        $email = $_POST['email'];
        } else {
        $error .= "Email ID. <br />";
        }
    if (!empty($_POST['mail'])) {
      $mail = $_POST["mail"];
        $otherAnswer=$_POST["otherAnswer"];
    }
    else {
        $error .= "Other reason <br />";
        }

    <div id="contactForm">

           <?php
          if (!empty($error)) {
          echo '<p class="error"><strong>Your request has NOT been sent.<br/> Please correct / re-enter the following and try again</strong><br/>' . $error . '</p>';
          } elseif (!empty($success)) {
          echo $success;
          }
        ?>

                    <form action="" method="post"  style="margin-top:20px;" id="feedbackform">
                      <input type="hidden" name="subject" value="Form Submission" />
                      <input type="hidden" name="redirect" value="" />
    <label>Email ID:</label>
                      <input type="text" name="email" value="" style="margin-left:30px;"<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" />
                        <br/><br/>

                <label >
        <input name="mail" type="radio" id="mail" value="I receive too many e-mails" class="opt"/>I receive too many e-mails<br /></label>
    <label ><input name="mail" id="mail1" type="radio" value="I don’t find the e-mails interesting or useful"class="opt" />I don’t find the e-mails interesting or useful</label>
    <br/>
      <label >
    <input name="mail" type="radio" id="mail2" value="other" class="opt"/>Other, please give details:</label><br/><br/>
   <textarea class="textbox" style="display:none;width: 485px; margin-left:10px; height:110px;" type="text" name="otherAnswer" id="otherAnswer"/></textarea><br/>
     <span style=" width:700px; text-align:center;">      
    <input type="submit" class="submit" name="submit" value="Unsubscribe" /> 

2 个答案:

答案 0 :(得分:0)

在您的php代码中添加此验证检查

if($_POST["mail"]=='other' && empty($_POST["otherAnswer"])){
    $error .= "Other, please give details. <br />";
}

答案 1 :(得分:0)

我知道,这不是一个答案,但对于格式化,我想放在这里:

您的代码中有一行代码,出现了错误:

<input type="text" name="email" value="" style="margin-left:30px;"<?php if (isset($_POST['email'])) {
    echo $_POST['email'];
} ?>" />

应该是什么:

<input type="text" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" style="margin-left:30px;" />

您回复了value属性中没有的电子邮件。