0

我无法解决代码中的逻辑错误。尽管逻辑似乎是正确的,但我不确定出了什么问题

这是我的 php:

<?php require_once("includes/connection.php"); ?>
<?php
include_once("includes/form_functions.php");
if(isset($_POST['submit']))
{
    $errors = array();
    if(isset($_POST['txtSpace']))
    {
        $choice_spc_port = $_POST["txtSpace"];
    }
    if(isset($_POST['txtNumber']))
    {
        $choice_no = $_POST["txtNumber"];
    }
    if(isset($_POST['txtLocation']))
    {
        $choice_loc = $_POST["txtLocation"];
        if($choice_loc =="txtSetXY")
        {
            $x = $_POST["txtXLocation"];
            $y = $_POST["txtYLocation"];
            if($x == "")
            {
                $message = "You forgot to enter X Value";
            }
            elseif($y == "")
            {
                $message = "You forgot to enter Y Value";
            }
            else
            {
                $choice_loc = $x . "," . $y;
            }
        }
    }
    $user_name = $_POST["txtUserName"];
    $user_email = $_POST["txtUserEMail"];
    $animal_name = $_POST["txtAnimalName"];
    $disp_msg = $_POST["txtDispMsg"];
    $comments = $_POST["txtComments"];
    if(!isset($_POST['txtSpace']))
    {
        $message = "Please select Space Portion";
    }
    elseif(!isset($_POST['txtNumber']))
    {
        $message = "Please select the number of animals";
    }
    elseif(!isset($_POST['txtLocation']))
    {
        $message = "Please select the desired location of animal";
    }
    elseif($user_name == "")
    {
        $message = "Please enter your name.";
    }
    elseif($user_email == "")
    {
        $message = "Please enter your email.";
    }
    elseif($animal_name == "")
    {
        $message = "Please enter the name of the animal.";
    }
    elseif($disp_msg == "")
    {
        $message = "What message you want to dedicate to the animal?.";
    }
    else
    {
        // validation
        $required_fields = array('txtUserName','txtUserEMail','txtAnimalName','txtDispMsg');
        $errors = array_merge($errors, check_required_fields($required_fields, $_POST));
        $user_name = trim(mysql_prep($_POST['txtUserName']));
        $user_email = trim(mysql_prep($_POST['txtUserEMail']));
        $animal_name = trim(mysql_prep($_POST['txtAnimalName']));
        $disp_msg = trim(mysql_prep($_POST['txtDispMsg']));

        if(empty($errors))
        {
            /*if($choice_loc == "txtSetXY")
            {
                $x = $_POST["txtXLocation"];
                $y = $_POST["txtYLocation"];
                $choice_loc = $x . "," . $y;
            }*/
            if($choice_no == "other")
            {
                $choice_no = $_POST["other_field"];
            }

            $insert = "INSERT INTO db_form (db_space_portion, db_number, db_location, db_user_name, db_user_email, db_animal_name, db_message, db_comments) VALUES ('{$choice_spc_port}', '{$choice_no}', '{$choice_loc}', '{$user_name}', '{$user_email}','{$animal_name}','{$disp_msg}','{$comments}')";
            $result = mysql_query($insert);
            if($result)
            {
                echo("<br>Input data is succeed");
            }
            else
            {
                $message = "The data cannot be inserted.";
                $message .= "<br />" . mysql_error();
            }
        }
        else
        {
            if(count($errors) == 1)
            {
                $message = "There was 1 error on the form.";
            }
            else
            {
                $message = "There were " . count($errors) ." errors on the form.";
            }
        }
    }
}
else
{
    $user_name = "";
    $user_email = "";
    $disp_msg = "";
    $comments = "";
}
    ?>
     <!DOCTYPE html>
     <html lang="en">
     <head>
     <title>Test Form</title>
     <meta charset="utf-8">
    <link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
    <link rel="stylesheet" href="css/layout.css" type="text/css" media="all">
    <link rel="stylesheet" href="css/style.css" type="text/css" media="all">
    <script type="text/javascript" src="js/jquery-1.9.0.min.js" ></script>
    <script type="text/javascript" src="js/cufon-yui.js"></script>
    <script type="text/javascript" src="js/cufon-replace.js"></script>
    <script type="text/javascript" src="js/Copse_400.font.js"></script>
    <script type="text/javascript" src="js/imagepreloader.js"></script>
    <script type="text/javascript" src="js/functions.js"></script>
     <!--[if lt IE 9]>
    <script type="text/javascript" src="js/ie6_script_other.js"></script>
       <script type="text/javascript" src="js/html5.js"></script>
     <![endif]-->
        </head>
       <body id="page5">
       <!-- START PAGE SOURCE -->

      <div class="body7">
       <div class="main">
      <section id="content">
      <div class="wrapper">
        <article class="col24">
       <div class="pad1">
        <h4>Kindly Fill the form</h4>
        <?php if(!empty($message)){ echo $message; } ?>
        <?php if(!empty($errors)){ echo display_errors($errors);}?>
        <form id="TestForm" name="TestForm" method="post" action="form.php">
          <div>

          <div  class="wrapper"> <strong><span>*</span> Desired Space</strong>
            <div class="formText">
              <input type="radio" name="txtSpace" value="RJ"/>Space Top<br />
              <input type="radio" name="txtSpace" value="SM" />Space Bottom<br />
              </div>
            </div>

            <div class="wrapper"> <strong><span>*</span> Select the Number</strong>
            <div class="formText">
              <input type="radio" name="txtNumber" value="100"/>100
              <input type="radio" name="txtNumber" value="200"/>200
              <input type="radio" name="txtNumber" value="500"/>500
              <input type="radio" name="txtNumber" value="1000"/>1000
              <input type="radio" name="txtNumber" value="10000"/>10000
              <input type="radio" name="txtNumber" value="other"/>other
              <input type="text" name="other_field" id="other_field" onblur="checktext(this);"/>
            </div>
            </div>

            <div class="wrapper"> <strong><span>*</span> Select X & Y Value</strong>
                <div class="formText">
                <input type="radio" name="txtLocation" value="txtSetXY"/> Specify Photo Location<br />
                    <div style="padding-left:20px;">
                    X: <input type="text" id="locField" name="txtXLocation"><br />
                    Y: <input type="text" id="locField" name="txtYLocation"><br />
                    </div>
                    <input type="radio" name="txtLocation" value="Default"/>Default
                </div>

            </div>


            <div class="wrapper"> <strong><span>*</span> Your Name:</strong>
              <div class="bg">
                <input type="text" class="input" name="txtUserName">
              </div>
            </div>
            <div class="wrapper"> <strong><span>*</span> Your Email:</strong>
              <div class="bg">
                <input type="text" class="input" name="txtUserEMail">
              </div>
            </div>
            <div class="wrapper"> <strong><span>*</span> Name of the animal:</strong>
              <div class="bg">
                <input type="text" class="input" name="txtAnimalName">
              </div>
            </div>

            <div class="wrapper">
            <div class="textarea_box"> <strong><span>*</span> The Message you want for your favourite animal:</strong>
                <textarea name="txtDispMsg" cols="1" rows="1"></textarea>
            </div>
            </div>

            <div class="wrapper">
            <div class="textarea_box"> <strong>Comments:</strong>
                <textarea name="txtComments" cols="1" rows="1"></textarea>
            </div>
            </div>

            <input type="submit" name="submit" value="Submit"> 
           </div>
        </form>
      </div>
    </article>
  </div>
</section>
 </div>
</div>
</body>
 </html>

错误:

在这里检查这个 php fiddle。

第 25 行。即使我将 x 文本字段留空 $message = "You forgot to enter X Value"; 也不会显示。

第 29 行也是如此。即使我将 y 文本字段留空 $message = "You forgot to enter Y Value"; 也不会显示。

但是,如果我在 x 和 y 文本字段中输入值,即在 txtXLocation 和 txtYLocation 中,它们将保存在 db 中,这意味着它只是不检查验证。

提前致谢

4

1 回答 1

0

确保您在包含文件夹中有 connection.php 文件,并且您提供了正确的路径来访问该文件。

于 2013-02-05T08:34:47.037 回答