title: ajax PHP form validation display error massage when no succes
body: <p>I know this question was probabley asked before but from what i have searched I couldnt find the answer I was looking for. </p>
  
  <p>This is the issue:</p>
  
  <p>I have a form that is sent via ajax $post to a php file called <code>upload_data.php</code>.
  This file calls a php function called <code>insert_data</code> the php function contains a simple check to see if the <code>$idNo</code> variable already exists. </p>
  
  <p>Now till now everything works great.
  My question is how can I show the user the error that i receive from the php function.</p>
  
  <p>this are the files: </p>
  
  <p><strong>ajax call:</strong></p>
  
  <pre><code>            $.ajax({
                  url: 'upload_data.php',
                  data: fd,
                  cache: false,
                  contentType: false,
                  processData: false,
                  type: 'POST',
                  success: function(data) {
                      console.log(data);
                        $('#error').append(data);
  
                  }
              });
  </code></pre>
  
  <p><strong>upload_data.php</strong></p>
  
  <pre><code>$upload_dir = "uploads/vitur-sodiut/";
  $img = $_POST['hidden_data'];
  $idNo = $_POST['idno'];
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  
  
  
  $img = str_replace('data:image/png;base64,', '', $img);
  $img = str_replace(' ', '+', $img);
  $data = base64_decode($img);
  $file = $upload_dir . "" . $idNo . ".png";
  $success = file_put_contents($file, $data);
  print $success ? $file : 'Unable to save the file.';
  
  
  $suc = new Form;
  $suc-&gt;insert_data($lname, $fname, $file, $idNo);
  </code></pre>
  
  <p><strong>insert_data function</strong></p>
  
  <pre><code>public function insert_data($lname, $fname, $file, $idNo){
  
      $stmt = $this-&gt;dbh-&gt;prepare('SELECT * FROM vitur_sodiut WHERE idno=?');
      $stmt-&gt;bindParam(1,  $idNo);
      $stmt-&gt;execute();
      $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
      var_dump($row);
      if( ! $row)
      {
  
  
  
          $query = $this-&gt;dbh-&gt;prepare("INSERT INTO vitur_sodiut (lname, fname, file, idno) VALUES(?, ?, ?, ?)"); 
          $query-&gt;bindparam(1, $lname);
          $query-&gt;bindparam(2, $fname);
          $query-&gt;bindparam(3, $file);
          $query-&gt;bindparam(4, $idNo);
  
  
  
          try {
          $query-&gt;execute();
              if (!$query) { //בודק אם הנתונים הוכנסו למנ
                  echo "Failure conncting db!";
              }else{
                  $success["body"] = "You request just sending to the server ...";
                  header("HTTP/1.1 200 OK", true, 200); 
              }
          }
          catch (PDOException $e) {
              die($e-&gt;getMessage());
          }
      }else{
          echo'value already exists in the table';
      }
  }
  </code></pre>
  
  <p>This is a snapshot of my console. what we see there is the data var from the ajax call. the result from the php function is out of the array.</p>
  
  <p><a href="https://i.stack.imgur.com/J9lhh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J9lhh.png" alt="enter image description here"></a> </p>