0

I am trying to get a html page with jquery ajax to connect to an online database and return true or false if a query returns true or false.

Here is the code for the index.html file (This is running on my local pc in wamp)

The Query/Ajax Code:

<script type="text/javascript">

function submitForm() {

  if(!$("#username").val()){
     alert("username field cannot be empty.");
     return false;
  }

  if(!$("#password").val()){
     alert("password field cannot be empty.");
     return false;
  }      

  $.ajax({type:'POST', 
      url: 'http://mywebsite.com/test/index.php', 
      data:$('#ContactForm').serialize(), success: function(response) {
      $('#ContactForm').find('.form_result').html(response);
      }});

      return false;
}

</script>

And the Form...

<form id="ContactForm" onsubmit="return submitForm();">
    Username: <input id="username" type="text" name="username" value="" /><br /> 
    Password: <input id="password" type="text" name="password" value="" /><br />
    <br />
    <input type="submit" name="submit" value="Submit" /><br />
    <div class="form_result"></div>
</form>

Now, here is the code on the server side...This file I''ve uploaded to the server where the database is resident:

<?php

  if(isset($_POST['username'])) {

      $username = $_POST['username'];
      $password = md5($_POST['password']); 

      $con=mysqli_connect("localhost","dbuser","pass","myDatabase"); //Server Online


    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $result = mysqli_query($con, "SELECT * FROM tblusers WHERE username='".$username."'");


     if ($result->num_rows) {

      echo "right login !!";

         } else {

         echo 'wrong login';

       }

     mysqli_close($con);

    }


?>

The problem is that nothing is returned on the html page (callback) ...

In other words.. this line:

$('#ContactForm').find('.form_result').html(response); 

is not being called.

I've checked the console log and it returns 200 OK and the posts is actually submitted BUT nothing is returned to the page.

Any ideas what could be going wrong?

7
  • Have you checked the response data with console.log(response)... Commented Mar 1, 2014 at 22:11
  • You can check the network console (ctrl+shift+j) in Firefox or Chrome to make sure you're actually getting a response message, but you might be wanting .html(response.responseText); Commented Mar 1, 2014 at 22:18
  • Yes I've checked that and nothing is traced. But it firefox console the post is being sent but nothing returned. Commented Mar 1, 2014 at 22:18
  • It's checking firefox console log ... headers tab is ok, post tab is retuning the username and password and the HTML tag is totally blank. Commented Mar 1, 2014 at 22:24
  • Are you sure you are using MySQL version 4.1.13 and above? Commented Mar 1, 2014 at 22:26

4 Answers 4

2

Try changing the following lines of code:

if($result->num_rows > 0){
    echo "right login !!";
} else{
    echo 'wrong login';    
}

And then edit your AJAX to look like this (adding dataType: 'html')

$.ajax({type:'POST', 
  url: 'http://mywebsite.com/test/index.php', 
  data:$('#ContactForm').serialize(), 
  dataType: 'html', //or dataType: 'text'
  success: function(response) {
       $('#ContactForm').find('.form_result').html(response);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try to change the if statement with:

if($result->num_rows > 0){

  echo "right login !!";

     } else {

     echo 'wrong login';

   }

1 Comment

Sorry, no change here.
0

Try using json_encode() on your 'right login' and 'wrong login' strings

1 Comment

The code works perfectly if I use the local database and all the files are in the same place locally.
0

On local pc with wamp for exemple, debugger may add debugging info in your php response. Then, statut is ok (200) but you response is not json parsable. Try to add a

 error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }

in your ajax and you ll see that response contain debugging infos...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.