title: How can I show an error message if a value already exists in the database?
body: <p>I have a page where users can create topics via ajax. Before a topic is created I check to see if it already exists in the database. If the topic exists in the database then I want to show an error message via a div box before proceeding to submit the new topic. Everything works fine. But I can't figure out how to make the error message appear only when the topic already exists in the database.</p>
  
  <p>Here is the page with my form and ajax code:</p>
  
  <pre><code>&lt;script&gt;
      function cancelTopicCreation() {
          $(".create-topic-cancel-button, .close_blue_modal").click(function () {
              $('#id-create-topic-picture-preview').attr('src','&lt;?php echo BASE_URL; ?&gt;content/pictures/layout/default_group_icon.png');
          });
      }
  
      function close_blue_modal() {
          $('#blue_modal_mask').fadeOut(144);
          $('.blue_modal_window').fadeOut(144);
          $('.blue_modal_window_two').fadeOut(144);
          $('.blue_modal_window_three').fadeOut(144);
          $('.blue_modal_window_five').fadeOut(144);
      }
      function createTopic() {
          /*----------------submit title of new topic----------------*/
          $(document).ready(function() {
              var topicTitle = $(".create-topic-input-box").val();/* get the value of the topic title input and put it in a variable */
              var dataString = 'topicTitle='+ topicTitle;/* returns successful data submission message when the entered information is stored in database. */
              if(topicTitle == '') {/* if there is no title entered then */
                  alert("Please Fill All Fields");
              } else {
                  $.ajax({
                      type: "POST",
                      url: site_url+'resources/create-topic.php',
                      data: dataString,
                      cache: false,
                      success: function(result) {/* if the ajax submit is successful then */
                          $('#createTopicForm').each(function() {
                              this.reset();
                          });
                          close_blue_modal();
                          $('#id-create-topic-picture-preview').attr('src','&lt;?php echo BASE_URL; ?&gt;content/pictures/layout/default_group_icon.png');
                          $("#results").html(''); //append data into #results element
                          load_contents(track_page); //initial content load
                      }
                  });
              }
              return false;
          });
          /*----------------upload topic picture----------------*/
          $(document).ready(function() {
               var input = document.getElementById("id-create-topic-picture-input");
                file = input.files[0];
                if (file != undefined){
                  formData= new FormData();
                  if(!!file.type.match(/image.*/)){
                    formData.append("nameTopicPicture", file);
                    $.ajax({
                      url: site_url+'resources/upload-topic-icon.php',
                      type: "POST",
                      data: formData,
                      processData: false,
                      contentType: false,
                      success: function(data) {/* you can do something if the picture upload is successful here */}
                    });
                  } else {
                    alert('Not a valid image!');
                  }
                } else{/* if there is no image selected then */}
          });
      }    
  &lt;/script&gt;
  &lt;div id="blue_modal_mask" class="close_blue_modal"&gt;&lt;/div&gt;&lt;!--#blue_modal_mask .close_blue_modal--&gt;
  &lt;div id="blue_modal_four" class="blue_modal_window_two"&gt;
      &lt;div onclick="return cancelTopicCreation();" class="close_blue_modal icon-x-mark"&gt;&lt;/div&gt;&lt;!--.close_white_modal--&gt;
      &lt;div class="create-topic-label"&gt;Create topic&lt;/div&gt;&lt;!--.create-topic-label--&gt;
      &lt;div class="create-topic-error-message"&gt;That topic already exists. Please choose a different topic name.&lt;/div&gt;&lt;!--.create-topic-label--&gt;
      &lt;div class="create-topic-description"&gt;&lt;div class="icon-book create-topic-description-icon"&gt;&lt;/div&gt;Topics are used to organize content on the website.&lt;/div&gt;&lt;!--.create-topic-label--&gt;
      &lt;form id="createTopicForm" action="&lt;?php echo BASE_URL?&gt;resources/upload_topic_icon.php" method="post" enctype="multipart/form-data"&gt;
          &lt;div class="create-topic-input-wrap"&gt;
              &lt;label for='id-create-topic-input-box'&gt;&lt;div class='create-topic-name-label'&gt;Topic name&lt;/div&gt;&lt;/label&gt;
              &lt;input class="create-topic-input-box" id="id-create-topic-input-box" type="text" name="topicTitle" placeholder="Enter a topic name" autocomplete="off"&gt;&lt;!--input.index_sign_up_input--&gt;
              &lt;div class="create-topic-autocomplete-result-box"&gt;
                  &lt;div class="create-topic-autocomplete-result-list nice-scroll"&gt;
                      &lt;div class="create-topic-autocomplete-result-item"&gt;
                          &lt;img class="create-topic-autocomplete-result-item-picture" src="content/pictures/layout/default_group_icon.png" alt=""&gt;
                          &lt;div class="create-topic-autocomplete-result-item-content"&gt;
                              &lt;div class="create-topic-autocomplete-result-item-title"&gt;The title of the result goes here&lt;/div&gt;
                              &lt;div class="create-topic-autocomplete-result-item-details"&gt;The details of the result goes here&lt;/div&gt;
                          &lt;/div&gt;&lt;!--create-topic-autocomplete-result-item-content--&gt;
                      &lt;/div&gt;&lt;!--create-topic-autocomplete-result-item--&gt;
                      &lt;div class="create-topic-autocomplete-result-item"&gt;
                          &lt;img class="create-topic-autocomplete-result-item-picture" src="content/pictures/layout/default_group_icon.png" alt=""&gt;
                          &lt;div class="create-topic-autocomplete-result-item-content"&gt;
                              &lt;div class="create-topic-autocomplete-result-item-title"&gt;The name of the result goes here&lt;/div&gt;
                              &lt;div class="create-topic-autocomplete-result-item-details"&gt;The details of the result goes here&lt;/div&gt;
                          &lt;/div&gt;&lt;!--create-topic-autocomplete-result-item-content--&gt;
                      &lt;/div&gt;&lt;!--create-topic-autocomplete-result-item--&gt;
                      &lt;div class="create-topic-autocomplete-result-item"&gt;
                          &lt;img class="create-topic-autocomplete-result-item-picture" src="content/pictures/layout/default_group_icon.png" alt=""&gt;
                          &lt;div class="create-topic-autocomplete-result-item-content"&gt;
                              &lt;div class="create-topic-autocomplete-result-item-title"&gt;The name of the result goes here&lt;/div&gt;
                              &lt;div class="create-topic-autocomplete-result-item-details"&gt;The details of the result goes here&lt;/div&gt;
                          &lt;/div&gt;&lt;!--create-topic-autocomplete-result-item-content--&gt;
                      &lt;/div&gt;&lt;!--create-topic-autocomplete-result-item--&gt;
                      &lt;div class="create-topic-autocomplete-result-item-empty"&gt;Nothing found&lt;/div&gt;
                  &lt;/div&gt;&lt;!--create-topic-autocomplete-result-list--&gt;
              &lt;/div&gt;&lt;!--create-topic-autocomplete-result-box--&gt;
          &lt;/div&gt;
          &lt;div class="create-topic-picture-preview-wrap"&gt;
              &lt;img id="id-create-topic-picture-preview" src="&lt;?php echo BASE_URL; ?&gt;content/pictures/layout/default_group_icon.png" class="create-topic-picture-preview"&gt;
          &lt;/div&gt;
          &lt;div id="create-topic-footer"&gt;
              &lt;label for="id-create-topic-picture-input" &gt;
                  &lt;div class="icon-camera create-topic-camera-icon"&gt;&lt;/div&gt;
              &lt;/label&gt;
              &lt;input name="nameTopicPicture" accept="image/*" type="file" class="create-topic-picture-input" id="id-create-topic-picture-input"&gt;
              &lt;a onclick="return createTopic();" class="create-topic-button"&gt;Create&lt;/a&gt;
              &lt;a onclick="return cancelTopicCreation();" class="create-topic-cancel-button cancel_blue_modal"&gt;Cancel&lt;/a&gt;
          &lt;/div&gt;&lt;!--#new_post_options--&gt;
      &lt;/form&gt;
  &lt;/div&gt;&lt;!--.blue_modal_one .blue_modal_window--&gt;
  </code></pre>
  
  <p>Here is my php page that is referenced by the ajax:</p>
  
  <pre><code>session_start();/* start new or resume existing session */
  require_once('Wall.php');
  $Wall = new Wall;/* create a new object for the class */
  $languageFile = $Wall-&gt;getTheLanguageFileForFilesInTheResourcesFolder();/* get the language file and put it in a variable */ 
  include_once ($languageFile);
  $idOfLoggedInUser = isset($_SESSION['mem_id']) ? intval($_SESSION['mem_id']) : "";/* get the id of the logged in user and put it in a variable */
  $topicTitle = $_POST['topicTitle'];/* get the entered topic title and put it in a variable */
  $query = mysqli_query($databaseConnection, "SELECT columnTopicTitle FROM tableTopics WHERE columnTopicTitle='".$topicTitle."'");
  if (mysqli_num_rows($query) != 0) {
      echo "Username already exists";
  } else {
      $idOfLastTopicCreated = $Wall-&gt;createTopic($idOfLoggedInUser, $topicTitle);/* get the language file and put it in a variable */ 
      $_SESSION['idOfLastTopicCreated'] = $idOfLastTopicCreated;
  }
  
      $advert = array(
          'ajax' =&gt; 'Hello world!',
          'advert' =&gt; $row['adverts'],
       );
      echo json_encode($advert);
  ?&gt;
  </code></pre>
  
  <p>How can I tell javascript that a topic by that name already exists. If the topic already exists in the database then I want the ajax to stop and show the error popup div. Please help. I have tried to use json to pass a string that indicates that the topic already exists in the database. But was not successful. I have tried passing the string from php to query but the problem with that is that the string doesn't change on multiple attempts. And so it shows the error every time.</p>