0

我有一个 html 文件,它从我的数据库中加载一个列表,并允许前端用户删除一个特定的条目。

HTML 代码如下所示:

<script type="text/javascript">// <![CDATA[
function sendForm() {
var dataSend=$("#clientid").val();
$("#responseDiv").html("<h2>Removing from Database...</h2>");
$.post("RemoveClient.php",{
ClientId: dataSend
},function(data) {
$("#responseDiv").html(data);
$("#clientlist").empty();
$("#clientlist").html("{client_list nocache}");
});
return false;
}
// ]]></script>
</p>
<div id="MainForm"><form class="form" onsubmit="return sendForm()">
<h2 class="formstyle">Client Wizard</h2>
<p>Please fill all the required fields.</p>
<div id="clientlist">{client_list nocache}</div>
<p><input style="font-size: 1.5em; font-color: #000000;" onclick="sendForm()" type="submit" value="Delete" /></p>
</form>
<div id="responseDiv"> </div>
</div>

为 {client_list} 调用的 UDT 如下所示。

$dbhost='127.0.0.1';
$dbuser='user';
$dbpass='pass';
$dbname='dbname';
$conn=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if(!$conn)
{
die('Could not connect:'.mysqli_connect_error());
}
$sql="SELECT clientid,clientname FROM client order by clientid"; 
echo "<label> Select Client: </label>"; 
echo "<select id=clientid name=clientid>"; 
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result))
{ 
echo "<option value=".$row['clientid'].">".$row['clientname']."</option>"; 
} 
echo "</select>";

现在,一旦我单击删除,我希望下拉列表刷新为没有已删除客户端的新列表。我尝试通过清空 div 然后重新加载 UDT 来做到这一点。不幸的是,这似乎并没有按照我想要的方式工作,因为列表不会刷新,除非我刷新页面。无论如何我可以完成这项工作吗?

4

1 回答 1

0

快速/最简单的选择是为其分配一个 id 并通过 javascript 删除条目。

其次,让 RemoveClient.php 将其作为 AJAX 响应的一部分返回。

var response = data.split('|'); $("#responseDiv").html(response[0]); $("#clientlist").html(response[1]);

第三,我强烈反对这种方式,但这是您提出的问题,将 UDT 单独放在新页面上,然后使用 ?showtemplate=false 参数加载页面。

$("#clientlist").load("//www.mydomain.com/myudt.html?showtemplate=false");

于 2015-12-08T18:43:08.393 回答