Click here to Skip to main content
15,503,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am making a form in html with two text fields, one where the password is entered, the second where the user is written and options of a select, where the company is written, of which there can be several options, that is exactly why it is a "Select" with several options, the function it does is that, when typing a password in the corresponding field and pressing the enter key, with Ajax, it checks in the database if that password exists, and if so, it is filled automatically the user text field, and the "option" should also be filled, but right here is the problem I have, that the user text is perfectly filled with the data that corresponds to it from the database when pressing enter in the text password; but the company option is not filled, nothing is shown, but it remains empty. What I want is that when all the fields are already filled, with the submit button, enter another page and log in with the id of the user who is entering, but that is later, the current problem is what I mentioned earlier, how to fill also the option until you press enter and check if the password exists in the database, that the options do not appear when loading the page, but that, like all the other fields, it is empty and filled, just as it is filled automatically the user field, when pressing enter in password. I will change the entry method, since as it is currently it is not very secure, since when typing random passwords, it could be hacked, then I will ask to enter the username and password manually, but that the company select be filled when pressing enter, that yes i need to do it. Thank you very much in advance.


What I have tried:

I have tried this:

I share my html code:

<pre lang="HTML"><pre><!DOCTYPEhtml>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="home.css">
   <link rel="stylesheet" href="style2.css">
   <title>Management 2022</title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
</head>
<body>

<div class="divarabove">

      <h5>SDIG</h5>
      <h6>Management</h6>

      <div class="icon2 icon-key"></div>

      <h1>Website Management</h1>
   </div>

   <div class="corner"></div>

   <table class="dataentries">
       <tr>
<form method="POST">
   <td><h2 class="password">Password:</h2></td> <td class="td"><input class="boxpassword" id="password" type="text" name="password " autocomplete="off" value=""></td>
   </tr>

   <tr>
   <td><h2 class="user">User:</h2></td> <td><input type="text" class="userbox" id="user" name="user" disabled style=" background:transparent;border:none;" value=""></td>
   </tr>

           <td><h2 class="company">Company:</h2></td>
           <td><select name="company" id="company" value="" style="width:98%; height:calc(1.2em + 1.2vw);" >

<option value="0"></option>

          
               </select></td>
       </tr>

       
   </table>

<div id="Hidden"></div>
<script>
$("input[name='password']").keypress(function(e) {
   if(e.which == 13) {
       $.post('login.php', {
           password : $("input[name='password']").val()
       }, function(data){
           $('#Hidden').html(data);
       });
   }
});
</script>

<div class="divabelow">

<table class="buttonsdown">
<tr>
<td><label style="color:green; background: white; font-size: calc(1em + 1vw); border-radius: 45%; width: 20%;"><input type="submit" id= "register" disabled class="btenter" value="Login" name="register"></td>
<td><button class="btcancel"><label style="color:white; font-size: calc(1em + 1vw);">🗙</label> Cancel</button></td>
</tr>
</table>
</form>


</div>
<?php
   include("login.php");
   ?>
</body>
</html>


And my php code:

PHP
<pre>  <?php

$conex = mysqli_connect("SERVER", "USER", "PASSWORD", "DB");
if ($connect->connect_error) {
    die('<p>Failed to connect to MySQL: '. $conex->connect_error .'</p>');
  } else {
    echo '<p>Connection to MySQL server successfully established.</p>';
  }
?>



<script>
    $("input[name='user']").val('');
</script>
<?php
$password = (is_numeric($_POST['password']))?$_POST['password']:NULL;
if(!is_null($password)){
    $query = 'SELECT * FROM clients WHERE password= '.$password.';';
    $row = $conex->query($query);
    $record = $row->fetch_assoc();
    $totalrows = $conex->affected_rows;
    if($totalrows == 0){
        echo "<script>alert('There are no values ​​matching the search.');</script>";
    } else {
        ?>
        <script>
            $("input[name='username']").val("<?php echo $record['username'];?>");

            <?php
            $getcompany1 = 'SELECT * FROM clients order by id';
            $getcompany2 = mysql_query($getcompany1);

          while($row = mysqli_fetch_array($getcompany2))
          {
            $id = $row['id'];
            $company = $row['company'];

            ?>
           <option value="<?php echo $id ?>"><?php echo $company;?></option>
            <?php
          }
          ?>
          
        </script>
<?php
    }
} else {
    echo "<script>alert('You are entering a non-numeric value');</script>";
    exit();
}
?> 
Posted
Comments
Member 15627495 11-Sep-22 8:34am    
be careful about php and ajax ,
when you 'echo' by php, the buffer for the page to send is filled.
but during an ajax query 'echo' is the sender of the answer for the ajax request.
you're lost in a double use of echo here ... one to build your page, the other to send the ajax request answer.

How you can achieve :
keep echo for the ajax answer, and use global $var1,$var2.....,var_something to store datas all along your script.

in a second time, to show you relevant syntax :
<?php
            $getcompany1 = 'SELECT * FROM clients order by id';
            $getcompany2 = mysql_query($getcompany1);

          while($row = mysqli_fetch_array($getcompany2))
          {
            $id = $row['id'];
            $company = $row['company'];

            ?>
           <option value="<?php echo $id ?>"><?php echo $company;?></option>
            <?php
          }
          ?>

could be writen :
<?php
    $getcompany = mysql_query('SELECT * FROM clients order by id');

    while($row = mysqli_fetch_array($getcompany))
          {

       echo    "<option value=.$row['id'].">".$row['company']."</option>";
           
          }
    ?>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900