0

I want to create a login script on HTML page. I read that I need to use an intermediate script so that I can let my HTML use the database. So, I`ve written a PHP page that checks the username/password regarding the database.

I also read that I should use AJAX to connect to the database.

I would like to know how to write the AJAX code to return the value of the login either true or false.

3
  • It's not necessary that you should only use AJAX to connect to database. Commented Nov 30, 2010 at 17:12
  • you can try jquery if you want to . api.jquery.com/jQuery.ajax Commented Nov 30, 2010 at 17:13
  • HTML cannot connect to MySQL - it requires a programming lange such as PHP or a web-facing API that JavaScript can access. Also, AJAX is a method of using JavaScript to send and receive data in XML (or other) formats from a server (often a backend Ruby/PHP script). Commented Nov 30, 2010 at 17:15

4 Answers 4

3

You should really at-least try Google before posting here.

Google Search: ajax login code

This is a very simple implementation but can be a starting point. http://woork.blogspot.com/2007/10/login-using-ajax-and-php.html

You may need to read up on ajax a little if this doesn't make sence: http://code.google.com/edu/ajax/tutorials/ajax-tutorial.html

Sign up to request clarification or add additional context in comments.

1 Comment

Although I agree, there are probably plenty of examples out there, I think a lot of questions on SO could be answered by Google, but that's the beauty of having a community like SO. This should be a place to have others help you solve a problem.
1

AJAX is really nothing more than JavaScript which connects to a server-side resource (such as a PHP-backed page), receives a result, and likely does some UI manipulation of the HTML as a response to that result. A good place to get started is the jQuery ajax method. Using the jQuery JavaScript library will make the process much simpler. But, ultimately, it'll connect to your PHP code on the server to perform the actual database interaction.

Beyond that, it sounds like you're lacking a good bit of design oversight in this project. The statement "I also read that I should use AJAX to connect to the database." is particularly troubling. Where did you read that? Why did you read that? There seems to be little value in that suggestion beyond someone somewhere thinking that "AJAX is cool and people should use it for stuff."

Is there a specific design concern for using AJAX vs. just posting a form to some PHP code?

2 Comments

No, I hae googled and found all about AJAX through HTML. Also, I want to login using a popup box without navigating away from the page.
@actually, you can't do AJAX through HTML - however you can do it inside an HTML page with JavaScript.
0

Although it's not necessary to use AJAX for this, you can use something like below.

Considering you have an element that has a username and password like below:

<div id="login">
  <label for="txtUsername">Username:</label>
  <input type="text" id="txtUsername" />
  <label for="txtPassword">Password:</label>
  <input type="password" id="txtPassword" />
  <button id="btnLogin">Log In</buton>
</div>
<div id="logout" style="display: none;">
   <a href="logout.php">Log Out</a>
</div>

Then, having jQuery already referenced call your PHP page ("login.php" in the example):

<script type="text/javascript">
$(function() {
  $("#btnLogin").click(function() {
    $.ajax({
      url: "login.php",
      data: {username: $("#txtUsername").val(), password: $("#txtPassword").val()},
      success: function(data){
        $("#login").toggle();
        $("#logout").toggle();
      }
    });
  });
});
</script>

Comments

0

If I understand your question correctly, there are two ways you could handle this. The first (and most common) way is to just set the PHP script as the action of your HTML form:

<form method="post" action="folder/phpfile.php">
  <input type="text" name="username" />
  <input type="password" name="password" />
</form>

After doing this, make sure your PHP script takes in the post variables as $_POST['input_name'], where input_name is the name you used in the inputs on your HTML form (as 'username' and 'password' are used above).

Using AJAX for authentication is not always the best way to handle things in my opinion, but it is doable. I would recommend you try using JQuery behind your JavaScript if you are going to go the AJAX route. They have a really handy framework for handling AJAX queries and events.

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.