0

I am trying to perform a php function on click of link

and then get the value from input type text and call perform a select query

so far,

<form name="form" id="form" action="login.php" method="get">
          <h1>Login</h1>
          <div>
            <input name="username" id="username" type="text" class="form-control" placeholder="Username" required="" />
          </div>
          <div>
            <input id="password" type="password" class="form-control" placeholder="Password" required="" />
          </div>
          <div>
            <input id="team" type="text" class="form-control" placeholder="Team" required="" />
          </div>
           <div>
            <a class="btn btn-default submit" type="submit" id="submit" name="submit" onClick='return Logincheck()'>Log in</a>
            <br>
            <br>
            <p class="change_link">Lost your password ??    
            <!-- <a class="reset_pass" href="?sendcode=true">Lost your password?</a> -->
            <a href="#"> Change Password </a>
            </p>
          </div>

so onclick of submit anchor tag it calls LoginCheck javascript i.e

<script type='text/javascript'>
function Logincheck()
{
var user=document.getElementById("username").value;
var pass=document.getElementById("password").value;
var team=document.getElementById("team").value;
}

it successfully performs this task.

but now i have a php file that contains some database connectivity with function that performs select query

not i want to call that file from above javascript function

the php file is dbConnection.php

    <?php
function validateLogin($user,$pass,$team)
{
    $serverName = "server";

    $connectionOptions = array(
    "Database" => "dbName",
    "Uid" => "sa",
    "PWD" => "123456"
    );
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);

    $tsql= "SELECT Password,Name,Team FROM Login where Username=?;";
    $params = array($user);
    $getResults= sqlsrv_query($conn, $tsql,$params);
    //echo ("Reading data from table");
    if ($getResults == FALSE)
        die(FormatErrors(sqlsrv_errors()));
    while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
        $password=$row['Password'];
        $empName=$row['Name'];
        $temaname=$row['Team'];

        if($password==$pass && $team==$temaname)
        {
            echo '<script>
            alert("Login Success !!!!")
            var newURL = location.href.split("?")[0];
            window.history.pushState(null, null, window.location.pathname);
            </script>';
        }
        else
        {
            echo '<script>
            alert("Login Faild !!!!")
            var newURL = location.href.split("?")[0];
            window.history.pushState(null, null, window.location.pathname);
            </script>';
        }

    }
    sqlsrv_free_stmt($getResults);
}
?>

but i dont know how to call the validateLogin(user,pass,team) of above php file from the javascript of my first file

i was trying below code but no luck,

<script type='text/javascript'>
function Logincheck()
{
var user=document.getElementById("username").value;
var pass=document.getElementById("password").value;
var team=document.getElementById("team").value;
alert("hi");
var result=
"<?php
include 'dbConnection.php';
echo validateLogin("2012","hcca123@","BFL");
?>";

alert(result);
}

but i think i am doing something wrong, but not getting the cause. plz help

varsha
  • 113
  • 1
  • 1
  • 9
  • You are looking for Ajax call. JS does not see any of PHP code and can't execute it. It only can call another page to perform action, read it's response as simple text and then act depending on it – Justinas Mar 30 '20 at 14:30
  • Actually i am not familiar with ajax. I tried but no luck – varsha Mar 30 '20 at 14:31
  • hi, browser and server is not tightly coupled, the entire PHP will run one time and generate HTML and give to browser, thats all, for any operation at server again you need to communicate the server – Dickens A S Mar 30 '20 at 14:32
  • Will you plz suggest code so ill understand better – varsha Mar 30 '20 at 14:33
  • delete all other code in question and put only the AJAX code and a piece of the receiving part, I mean whatever you tried in AJAX – Dickens A S Mar 30 '20 at 14:34
  • please come to the chat I will help you – Dickens A S Mar 30 '20 at 14:37
  • hey Dickens i have started chat – varsha Mar 30 '20 at 14:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210595/discussion-between-dickens-a-s-and-varsha). – Dickens A S Mar 30 '20 at 14:43

0 Answers0