在nodejs mysql中动态下拉

问题描述 投票:0回答:2

我想在用户单击并选择客户端时根据客户端更改区域,然后根据与databaseenter code here中该客户端相关的区域更改区域下拉列表

这是我的客户表:

这是我的Region表,其中client_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>

这是添加区域的页面:Here is the page to add area

javascript mysql node.js ajax html5
2个回答
1
投票

您需要在onChange事件上为field_id = client_id添加ajax请求,以根据所选的client_id获取区域。对于响应,您可以根据需要设计服务器,您可以在服务器端生成HTML,也可以从响应中的数据库获取原始结果,然后在客户端从中生成HTML。当您为选择选项获得非常适合的HTML格式时,请将该html附加到region_id选择。


0
投票

基本上,解决方案可归结为以下内容:

//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格式的列表进行响应

© www.soinside.com 2019 - 2024. All rights reserved.