gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:39:17 25 4
gpt4 key购买 nike

我有很多要通过 PHP 插入 mysql 的作家和出版商列表,但每次我都必须在 "Add Writer" 和插入每个作者/出版商详细信息后,手动“添加出版商”

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

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>
链接到 index.php

.js 文件

 $(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

<?php
//Database credentials
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'library';

//Connect and select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>

最佳答案

发生这种情况的原因是在您的选择中:

<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"'; ?> >

关于php - 在Mysql中插入PHP值后如何显示最后选择的下拉值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50083987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com