title: Ajax and PHP with database connection
body: <p>i have problems with the code below, I'm trying to bring a message of error if the email already exists, but I'm not having success .. Look at the code:</p>
  
  <p>Ajax an jQuery:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;  
  
      // Centering the text content
      jQuery(window).resize(function () {
          boxHeight();
      }).load(function() {
          boxHeight();
          // Show the content and focus the email input
          $("#content").fadeIn();
          $("#email").focus();
  
  
      });
  
      jQuery(document).ready(function($){
          $('#subscribe').submit(function(e){
              e.preventDefault();
  
              email = $('input#email');           
              email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;           
              if(!email_regex.test(email.val())) {
                  $('#response', form).fadeIn(500, function() {
                      $('#response', form).html('&lt;p class="message warning" align="center"&gt;Invalid email&lt;/p&gt;');
                  });
                  return;
              } else {
                  $('#response', form).html('&lt;p class="message"&gt;Please Wait...&lt;/p&gt;');
              }
  
              var form = $(this);
              var post_url = form.attr('action');
              var post_data = form.serialize();               
              $.ajax({
                  type: 'POST',
                  url: post_url,
                  data: post_data,
                  success: function(responseText) {  if(responseText == 1) {
                      $('#response', form).html('&lt;p class="message"&gt;Error...&lt;/p&gt;');
                  } else { if(responseText == "") {                       
                      $(form).fadeOut(500, function(){
                          form.html(msg).fadeIn();
                      });
  
                  }
                  }
              }
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>PHP Database connect:</p>
  
  <pre><code>    &lt;?php    
  $host="xxxx"; // Host name
  $username="xxxx"; // Mysql username
  $password="xxxx"; // Mysql password
  $db_name="xxxx"; // Database name
  $tbl_name="xxxx"; // Table name
  
  // Connect to server and select database.
  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // Get values from form
  $email = $_POST['email'];
  
  $query = mysql_query("SELECT email FROM banco_emails WHERE 'email' = '$email'");
      if(mysql_num_rows($query) == 1) { // if return 1, email exist.
      echo '1';
      } else {
  // Insert data into mysql
  $sql="INSERT INTO $tbl_name(email) VALUES ('". $email . "')";
  $result=mysql_query($sql);
  echo '&lt;p class="message"&gt;Thanks for registering. Our bar is getting crowded!&lt;/p&gt;';
  </code></pre>
  
  <p>The problem is that the ajax code does not show the error message, only the message "Please wait ..." and nothing happens, i don't know why...</p>
  
  <p>Sorry for my bad english.</p>
  
  <p>Thanks in advanced!</p>
  
  <hr>
  
  <p>Problem solved, the problem was in the php code, I did it and it worked!</p>
  
  <pre><code>    $query = mysql_query("SELECT email FROM banco_emails WHERE email = '$email' LIMIT 1");
  
      $email_check = mysql_num_rows($query);
       if ($email_check &gt; 0) {
      echo '1';
      } else if ($email_check == 0) {
  // Insert data into mysql
  $sql="INSERT INTO $tbl_name(email) VALUES ('". $email . "')";
  $result=mysql_query($sql);
  echo '&lt;p class="message"&gt;Thanks for registering. Our bar is getting crowded!&lt;/p&gt;';
  </code></pre>