troubleshooting Question

php and ajax on same page to extract information from the database?

Avatar of sammySeltzer
sammySeltzerFlag for United States of America asked on
PHPScripting LanguagesAJAX
28 Comments1 Solution625 ViewsLast Modified:
Greetings mates,

Normally, the issue I am posting for is not a difficult task to tackle.

However, this issue has a twist and I am hoping someone can lead me to a solution.

I have a login page that makes an AJAX call to an API to authentic user credentials.

If the supplied credentials are valid, username, a token, as well as validation timestamp is returned to the user is redirected to another page.

This works great.

Here is the problem. The API as stated only returns username of the authenticated user, his/her assigned token and timestamp for what time authentication occurred.

It does not return anything else and WE do *not* have the ability to change what the API returns.

So, the users of the app are asking us that whenever a user is authenticated, that we find a way to retrieve his or her DeptID.

Does anyone know how I can integrate PHP code and the AJAX to retrieve the deptId?

Code below is my attempt to integrate php and jax but I am getting an error that
"An invalid parameter was passed to SQL_Srv query"


<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);

session_start();

// Connect to SQL Server database
include("../../connections/Connect.php");

$upass = $_GET['upass'];
// Construct query

$tsql =
"
SELECT
    ISNULL([ORGANIZATION],'')

FROM
 [EMPLOYEE]
WHERE Lower([LOGINNAME]) = lower('$uname')
";

$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
     echo "Error in executing query.</br>";
     die( print_r( sqlsrv_errors(), true));
}
$results = array();
// Retrieve and display the results of the query
//$lastFeatType = "";
while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) {
    array_push($results,$row);
}

 echo json_encode($results);

// Free statement and connection resources
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
  <h1>
    FTS Service Requests
</h1>
  <div class="action-container" style="display:none;"></div>


   <div id="tabs">
  <ul>
<div id="dialog" title="Basic dialog">
  <p style="font-size:14pt;">User Log In</p>
</div>
  </ul>
<p>
</p>
<br clear="all" />
<div>
     <br />   <br />
      <br />
       <?php
        //Start session
         session_start();
         header("Cache-Control: no-store, must-revalidate, max-age=0");
         header("Pragma: no-cache");
         header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
         if(!empty($_GET['status'])){
                echo '<div align=center><font color=firebrick>You have been logged out!</font><br><br>Log in again or close browser.</div>';
         }
       ?>
       <br />
        <br />
      <br />
      <form id="FormToValidate">
        <table>
         <tr>

           <td nowrap>
                   <div class="input text">
              <label><strong>UserName:</strong></label>
              <input  maxlength="40" class="required" name="user" id="user" size="20" placeholder="Enter username!" type="text" title="Please enter a username." tabindex="2" value="" style="width:400px;color:#000;font-size:10pt;height:20px;" />
            </div>
          </td>
            </tr>
            <tr>
              <td nowrap>
               <div class="input text">
                <label><strong>Password:</strong></label>
                <input  maxlength="40" class="required" name="pass" id="pass" size="20" placeholder="Enter password!" type="password" tabindex="3" title="Please enter a password." value="" style="width:400px;color:#000;font-size:10pt;height:20px;" />
               </div>
              </td>

         </tr>

         <tr>
            <td></td>
              <td>
              <div class="buttonSubmit">
              <input type="button" id="btnValidate" style="width:80px; margin-left:-152px;background-color:#fff;" value="Log In" />
             </div><br clear="all"/>
        </td>
      </tr>
    </table>

  </form>
</div>

<script type="text/javascript">

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

// Creating variables to hold data from textboxes

var uname = $("#user").val();
var upass = $("#pass").val();

$.post("proxyCreate.php",
  { data: JSON.stringify({ LoginName: uname,Password: upass }) })
    .done(function(data) {
        var result = JSON.parse(data);
        switch(result.Status) {
            case 0:
                //login successful
                tokenVal = result.Value.Token;
                location.href = "http://Accounts/accounts.php?token="+tokenVal+ "&user=" + uname;
                break;

            case 2:
                //invalid login
                alert(result.Message);
                break;
        }
    })
    .fail(function() {
        alert("The AJAX request failed!");
    });
  });
</body>
</html>

Any assistance is greatly appreciated
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 28 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 28 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros