如何在Mysql中插入PHP值后显示最后选择的下拉列值?

时间:2018-04-29 06:30:49

标签: php jquery mysql drop-down-menu

我有大量的编写器和发布者列表,这些列表将通过PHP插入到mysql中,但每次我必须在 "Add Writer" {{1插入每个编写者/发布者详细信息后手动

我希望在Mysql中插入值时,所选值不会自动更改(直到用户更改),即,在插入值之后,所选选项保持不变(由用户选择),直到用户更改为止。 / p>

"Add Publisher"

index.php
链接到 <?php include "dbConfig.php"; /*Adding Writer*/ if(isset($_POST['addwrt'])) { $writerid=$_POST['writerid']; $wname=$_POST['wname']; $wcity=$_POST['wcity']; $resultw=mysqli_query($db,"insert into wdetails(writerid,wname,wdesg,salutation,wcity)values('$writerid','$wname','$wdesg','$salut','$wcity')"); } /*Adding Publisher*/ else if(isset($_POST['addpubl'])) { $publisherid=$_POST['publisherid']; $pname=$_POST['pname']; $pcity=$_POST['pcity']; $resultp=mysqli_query($db,"insert into pdetails(publisherid,pname,pdesg,pcity)values('$publisherid','$pname','$pdesg','$pcity')"); } ?> <body> <select id="stf"> <option value="cf" selected="selected" />Select the Field <option value="addwriter" />Add Writer <option value="addpublisher" />Add Publisher </select> <form method="post" name="libraryAdd"> <div id="addwriter" style="display:none"> <table> <tr> <td>Writer ID<span style="color:red"><b>*</b></span></td> <td>W&nbsp;<input type='text' name="writerid"></td> </tr> <tr> <td>Writer Name<span style="color:red"><b>*</b></span></td> <td><input type="text" name="wname" /></td> </tr> <tr> <td>Writer City<span style="color:red"><b>*</b></span></td> <td><input type="text" name="wcity" /></td> <td><input name="addwrt" type="submit" value="ADD Data"></td> </tr> </table> </div> <div id="addpublisher" style="display:none"> <table> <tr> <td>Publisher ID<span style="color:red"><b>*</b></span></td> <td>P&nbsp;<input type='text' name="publisherid"></td> </tr> <tr> <td>Publisher Name<span style="color:red"><b>*</b></span></td> <td><input type="text" name="pname" /></td> </tr> <tr> <td>Publisher City<span style="color:red"><b>*</b></span></td> <td><input type="text" name="pcity" /></td> <td><input name="addpubl" type="submit" value="ADD Data"></td> </tr> </table> </div> </form> </body>

.js 文件

index.php

$(document).ready(function() { $('#stf').change(function(){ var value=$(this).val(); if(value === "addwriter"){ $("#addwriter").show('slow'); $("#addpublisher").hide('slow'); } else if(value === "addpublisher"){ $("#addpublisher").show('slow'); $("#addwriter").hide('slow'); } else { $("#addwriter").hide('slow'); $("#addpublisher").hide('slow'); } }); });

dbConfig.php

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是,在你的选择中你有:

<option value="cf" selected="selected" />Select the Field

表示选择始终以选择“选择字段”开始。

如果您希望在提交表单后仍保持选中上次选择的选项,则应将选择代码更改为:

<option value="cf" <?php if (!isset($_POST['addwrt']) && !isset($_POST['addpubl'])) echo 'selected="selected"'; ?> >Select the Field
<option value="addwriter" <?php if (isset($_POST['addwrt'])) echo 'selected="selected"'; ?> >Add Writer
<option value="addpublisher" <?php if (isset($_POST['addpubl'])) echo 'selected="selected"'; ?> >Add Publisher

您还需要进行以下更改以确保显示相应的输入框。变化

<div id="addwriter" style="display:none">
<div id="addpublisher" style="display:none">

为:

<div id="addwriter" <?php if (!isset($_POST['addwrt'])) echo 'style="display:none"'; ?> >
<div id="addpublisher" <?php if (!isset($_POST['addpubl'])) echo 'style="display:none"'; ?> >