nodejs mysql中的动态下拉列表

u7up0aaq  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(355)

我想根据客户机更改区域当用户单击并选择一个客户机,然后区域下拉列表根据数据库中与该客户机相关的区域更改 enter code here 这是我的客户表:

以下是我的region表,其中client\u id是外键:

这是我的密码

<div class="container">
		<h3>Add Area</h3>

		<form action="/add_areas" method="POST">

		  <!--input type="text" placeholder="DistributorID" name="distributor_id"-->
		  <input type="text" placeholder="Area Name" name="area_name" required="">
		  <!-- <input type="text" placeholder="Contact ID" name="contact_id" required=""> -->

		  <!-- <label>Client ID</label> -->
		  <select name="client_id" required="">
		  	<option disabled selected value> -- select a Client  -- </option>
		  	<%for(i=0; i<clients.length; i++){%>
		  		<option value="<%= clients[i].id%>"><%= clients[i].client_name %></option>
		  	<%}%>
		  </select>

		  <select name="region_id" required="">
		  	<option disabled selected value> -- select a Region  -- </option>
		  	<%for(i=0; i<regions.length; i++){%>
		  		<option value="<%= regions[i].id%>"><%= regions[i].region_name %></option>
		  	<%}%>
		  </select>
		  <br>

		  <button type="submit">Submit</button>
		</form>

		<br>
		<%if(areas.length>0) { %>
			<h3>All Existing Areas</h3>
			<!--For view-->
			<div class="table-rsponsive"> 
			    <table class="table table-striped table-bordered table-sm " cellspacing="0" width="100%">
			    	<tr>
			    		<th>Area Name</th>
			    		<th>Client </th>
			    		<th>Region </th>
			    		<th colspan="2">Action</th>
			    	</tr>
				  <% for(var i=0; i<areas.length; i++) {%>
				    <tr>
				      <td><%= areas[i].area_name %></td>
				      <td><%= areas[i].client_name %></td>
				      <td><%= areas[i].region_name %></td>

				      <td><a href="/edit_areas/<%= areas[i].id %>"><button class="btn-primary">Edit</button></a></td>
				      <td><a href="/delete_areas/<%= areas[i].id %>" onclick="javascript: return confirm('Are you SURE? If you delete this area All the town belongs to <%= areas[i].area_name %> area will automatically deleted!!');"><button class="btn-danger">Delete</button></a></td>
				    </tr>
				  <% } %>
				</table>
			</div>
			<% } 
			else{ %>
			<h3>No Area Found!!</h3>
		<% } %>

		<!--goto dashboard button-->
		<button type="submit" class="btn btn-primary btn-block" onclick="window.location.href='/dashboard.html'">Return to dashboard</button>
	</div>

这是要添加的页面area:here is 要添加区域的页面

jvlzgdj9

jvlzgdj91#

您需要在onchange事件上为字段\u id=client\u id添加一个ajax请求,以便根据所选的client\u id获取区域。对于响应,您可以根据需要设计服务器,您可以在服务器端生成html,也可以在响应中从数据库获取原始结果,然后在客户端从中生成html。当您为select选项获得了非常合适的html格式时,就将该html附加到region\u id select。

cwtwac6a

cwtwac6a2#

基本上,解决方案归结为以下几点:

//These are your target elements
const client = document.querySelector('#client');
const region = document.querySelector('#region');
const table = document.querySelector('#table');
//POST username upon change event and modify table 'div' and region'select' innerHTML's accordingly
client.addEventListener('change', function () {
    let xhr = new XMLHttpRequest();
    let user = this.value;
    xhr.open('POST', '/getRegionsByUser', true);
    xhr.send(user);
    xhr.onload = function(){
        if (xhr.readyState == 4 && xhr.status == 200) {
            let regions = JSON.parse(this.responseText);
            /* expected JSON-formatted string {list:[region1, region2, ...] */
            regions.list.forEach(region => {
                region.innerHTML += '<option value="'+region+'">'+region+'</option>';
                table.innerHTML += '...';
            });
        }
    };
});

服务器端脚本很大程度上取决于您的环境,从概念上讲,它应该通过用户名执行sql选择区域,并使用json格式的列表进行响应

相关问题