PHP logic error in a php form code
<p>I'm unable to solve the logical error in the code. I'm not sure what is wrong though it seems the logic is correct</p>

<p>This is my php:</p>

<pre><code>&lt;?php require_once("includes/connection.php"); ?&gt;
&lt;?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("&lt;br&gt;Input data is succeed");
            }
            else
            {
                $message = "The data cannot be inserted.";
                $message .= "&lt;br /&gt;" . 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 = "";
}
    ?&gt;
     &lt;!DOCTYPE html&gt;
     &lt;html lang="en"&gt;
     &lt;head&gt;
     &lt;title&gt;Test Form&lt;/title&gt;
     &lt;meta charset="utf-8"&gt;
    &lt;link rel="stylesheet" href="css/reset.css" type="text/css" media="all"&gt;
    &lt;link rel="stylesheet" href="css/layout.css" type="text/css" media="all"&gt;
    &lt;link rel="stylesheet" href="css/style.css" type="text/css" media="all"&gt;
    &lt;script type="text/javascript" src="js/jquery-1.9.0.min.js" &gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/cufon-yui.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/cufon-replace.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/Copse_400.font.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/imagepreloader.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/functions.js"&gt;&lt;/script&gt;
     &lt;!--[if lt IE 9]&gt;
    &lt;script type="text/javascript" src="js/ie6_script_other.js"&gt;&lt;/script&gt;
       &lt;script type="text/javascript" src="js/html5.js"&gt;&lt;/script&gt;
     &lt;![endif]--&gt;
        &lt;/head&gt;
       &lt;body id="page5"&gt;
       &lt;!-- START PAGE SOURCE --&gt;

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

          &lt;div  class="wrapper"&gt; &lt;strong&gt;&lt;span&gt;*&lt;/span&gt; Desired Space&lt;/strong&gt;
            &lt;div class="formText"&gt;
              &lt;input type="radio" name="txtSpace" value="RJ"/&gt;Space Top&lt;br /&gt;
              &lt;input type="radio" name="txtSpace" value="SM" /&gt;Space Bottom&lt;br /&gt;
              &lt;/div&gt;
            &lt;/div&gt;

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

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

            &lt;/div&gt;


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

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

            &lt;div class="wrapper"&gt;
            &lt;div class="textarea_box"&gt; &lt;strong&gt;Comments:&lt;/strong&gt;
                &lt;textarea name="txtComments" cols="1" rows="1"&gt;&lt;/textarea&gt;
            &lt;/div&gt;
            &lt;/div&gt;

            &lt;input type="submit" name="submit" value="Submit"&gt; 
           &lt;/div&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/article&gt;
  &lt;/div&gt;
&lt;/section&gt;
 &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
 &lt;/html&gt;
</code></pre>

<p>Errors:</p>

<p>Check this php fiddle here.</p>

<p>line 25. This is never shown even if I leave x textfield blank
$message = "You forgot to enter X Value";</p>

<p>same is with line 29. This is never shown even if I leave y textfield blank
$message = "You forgot to enter Y Value";</p>

<p>However if I enter the values in x and y textfield i.e. in txtXLocation and in txtYLocation they are being saved in db meaning it is just not checking the validation. </p>

<p>Thanks in advance</p>
