I am making a small form where it asks to enter "Password", when entering it and pressing the enter key, automatically with Ajax it makes the query to the database, and if that password exists, it fills the fields of "User" and "Company", the User field is a text field, and the Company field is a Select, now, the problem I have is that there is also another Select, where the branch of the selected company goes, so far I can fill perfectly the user and the drop-down list of options of the Select of "Company", what I have not been able to achieve is to fill the Select of "Branch" from the DB depending on the Company that the user chooses, that is, I want that when selecting a certain company , automatically the list of Branch options is filled depending on the branches that the company that was selected has, the companies are in a different table in the DB, and the branches are also in a different table, so I want to make a query to the Select of "Branch" verifies the ID of the company and shows the branches indicated for said company. Thank you very much in advance
I attach my Html code:
<!DOCTYPEhtml>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script src="./app.js" charset="utf-8"></script>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="home5.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" style="display:none;"></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); font-family: Arial, Helvetica, sans-serif; ">
<option value="0"></option>
</select></td>
</tr>
<td><h2 class="branch">Branch:</h2></td>
<td><select name="branch" id="branch" value="" style="width:98%;
height:calc(1.2em + 1.2vw); font-family: Arial, Helvetica, sans-serif;">
<option value="0"></option>
</select></td>
</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");
?>
<script language="javascript">
$(document).ready(function(){
$("#company").change(function () {
$("#company option:selected").each(function () {
$IDECOMPANY = $(this).val();
$.post("includes/enter.php", { IDEMPRESA: IDEMPRESA }, function(data){
$("#branch").html(data);
});
});
})
});
</script>
</body>
</html>
and my php code:
<?php
header("Content-Type: text/html;charset=utf-8");
$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('');
$IDEMPRESA = $_POST['IDEMPRESA'];
</script>
<?php
$password = utf8_decode(is_numeric(htmlspecialchars(mysqli_real_escape_string($conex,$_POST['password']))))?$_POST['password']:NULL;
if(!is_null($password)){
$query = 'SELECT * FROM customers 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 $registration['username'];?>");
<?php
$sql = "SELECT COMPANYID, NAME FROM companydata LIMIT 10";
$resultset = mysqli_query($connect, $sql) or die("database error:". mysqli_error($connect));
while( $rows = mysqli_fetch_assoc($resultset) ) {
?>
$('#company').append( $('<option>', { value: "<?php echo $rows["COMPANY"]; ?>", text: "<?php echo $rows["NAME "]; ?>" } ) );
<?php }
?>
<?php
$IDEMPRESA = $_POST['IDEMPRESA'];
$sql = "SELECT BRANCHID, NAME FROM branches WHERE IDEMPRESA = '$IDEMPRESA';";
$resultset = mysqli_query($connect, $sql) or die("database error:". mysqli_error($connect));
while( $rows = mysqli_fetch_assoc($resultset) ) {
?>
$('#branch').append( $('<option>', { value: "<?php echo $rows["BRANCHID"]; ?>", text: "<?php echo $rows["NAME "]; ?>" } ) );
<?php }
?>
</script>
<?php
}
} else {
echo "<script>alert('You are entering a non-numeric value');</script>";
exit();
}
?>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.