Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:40:32+00:00 2026-06-12T04:40:32+00:00

I needed to log into a database using jSON and Ajax to refresh it

  • 0

I needed to log into a database using jSON and Ajax to refresh it onto itself. Here is the fixed code to do so:

< auth.php >`

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"
    type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript">
        $(document).ready(function() {

$("#login").click(function() {

    var action = $("#form1").attr('action');
    var form_data = {
        email: $("#email2").val(),
        password: $("#password").val(),
    };
$.getJSON("login.php",form_data,function(data){
    switch(data.retval){
      case 0: $("#form").html("You have logged in successfully,"+ data.data.displayName+"!");
      break;
      case 1: $("#status").html("Invalid Username/Password, please try again.");
      break;
      default: $("#status").html("Database error, please try again.");
      break;
   }
});
});
});
    </script>
</head>

<body>
    <div id="fullBG">
        <div id="formBG">
            <div id="form">
                <div id="status">
                    <br />
                    <br />
                </div>
                <form id="form1" name="form1" action="login.php" method="post">
                    <input type="email" placeholder="Email" name="email" id="email2" size="30">
                    <br>
                    <input type="password" placeholder="Password" name="password" id="password"
                    size="30">
                    <br>
                    <input type="button" value="Login" class="sub" id="login">
                </form>
                <br />
                <br />
                <form method="link" action="register.php">
                    <input type="submit" value="Register" class="register">
                </form>
            </div>
        </div>
    </div>
</body>

</html>`

< login.php >`

<?php
ob_start();
session_start();
mysql_connect('', '', '') or 
die('Could not connect: ' . mysql_error());
mysql_select_db('phppro2') or
die ('Can\'t use database: ' . mysql_error());

// retval: 0 - login ok, 1 - login failed, 2 - internal error
$json = array("retval" => 2, "data" => NULL, "debug" => "");

$email = mysql_real_escape_string($_REQUEST['email']);
$password = mysql_real_escape_string($_REQUEST['password']);

$sql="SELECT * FROM users WHERE email='$email' and password='$password'";

$json['debug'] .= "SQL query was: ".$sql."\n";
$result=mysql_query($sql);
if (!$result) {
$json['debug'] .= "SQL query failed\n";
$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
die(json_encode($json));
}
$count=mysql_num_rows($result);

if($count==1){
$json['retval'] = 0;
$json['data'] = mysql_fetch_assoc($result);
} else {
$json['retval'] = 1;
}
$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
echo json_encode($json);`

// Old problem:

I need to retrieve the information from my database and load it using Ajax. I had everything working, but my auth.php file was overwritten and I lost it. I tried to retrace my steps but I am getting lost. The page no longer refreshes onto itself and it does not retrieve the data. (I removed my database information, but it is connected)

My login.php I have not changed, so I assume it’s accurate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:40:33+00:00Added an answer on June 12, 2026 at 4:40 am

    1) you shouldn’t keep same ID of two buttons (Register & Login Button)

    2) change your javascript function as below

     $.getJSON("login.php",form_data,function(data){
            switch(data.retval){
              case 0: $("#form").html("You have logged in successfully,"+ data.data.displayName+"!");
              break;
              case 1: $("#status").html("Invalid Username/Password, please try again.");
              break;
              default: $("#status").html("Database error, please try again.");
              break;
           }
        }
    

    3) Also your status div should have to be blank

    <div id="status"></div>
    

    4) syntax error on http://samaradionne.com/phppro2/auth.php

    $(document).ready(function() {
    
        $("#login").click(function() {
    
            var action = $("#form1").attr('action');
            var form_data = {
                email: $("#email").val(),
                password: $("#password").val(),
            };
     $.getJSON("login.php",form_data,function(data){
            switch(data.retval){
              case 0: $("#form").html("You have logged in successfully,"+ data.data.displayName+"!");
              break;
              case 1: $("#status").html("Invalid Username/Password, please try again.");
              break;
              default: $("#status").html("Database error, please try again.");
              break;
           }
        });
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When users log into our site we retrieve an object from our database that
Needed Finfo but deleted msi package, so uninstalled php 5.3.0, downloaded 5.3.2 and installed.
I needed to store arbitrary data in a relational database of multiple data-types so
I need a flexible way to log keyvalue pairs of arguments using System.Diagnostics.Trace. Basically,
I need to convert the output of an .aspx page into a JSON object
I'm fairly to MediaWiki and needed a way to automatically log users in after
As in many web applications, when my users log into my site, I set
I want to insert or update, then insert a new log into another table.
I'm considering creating an input/log file for my code for two main purposes. 1)
I have a internal website that users log into. This data is saved as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.