【发布时间】:2020-02-15 18:13:31
【问题描述】:
我有一个 php 网页,我可以从下拉选择字段中进行选择,它会按应有的方式填写该字段,但我在填写一些输入字段时遇到了问题。我花了几天时间尝试在 google 上和其他地方找到的各种不同的解决方案,但很少使用 mysqli 填充的下拉选择字段来填充其他输入字段。我可以让它在填充表格的地方工作,但我需要允许用户修改填充的数据,所以我使用了一个输入字段。
CDC3.php 文件:
<?php
include_once 'includes/db_connect.php';
?>
<?php
$b = intval($_GET['b']);
$sql="SELECT * FROM Batches WHERE BatchNum = '".$b."'";
$result = $mysqli->query($sql);
while ($row = mysqli_fetch_array($result)) {
}
?>
主php文件:
<?php include_once 'includes/db_connect.php'; ?>
<!DOCTYPE HTML>
<html>
<head>
<title>CDC Wash Run Entry</title>
<script>
function showBatch(str) {
if (str == "") {
document.getElementById("txtMessage").innerHTML = "";
return;
}
else {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtMessage").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","CDC3.php?b="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form id="WashForm" name="washform" method="POST">
<h1>CDC Wash Run Entry</h2>
<label for="DateTimeCode">DateTimeCode</label>
<input name="DateTimeCode" value="<?php echo date('Y-m-d H:i:s'); ?>" required><br>
<label for="BatchNum">BatchNum</label>
<?php
$sql = "SELECT BatchNum, BatchName, SourceProduct, SourceIngredient FROM Batches ORDER by BatchNum DESC";
$result = $mysqli->query($sql);
echo "<select name='BatchNum' id='BatchNum' onchange='showBatch(this.value)'>";
echo "<option value=''> Select BatchNum </option>";
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['BatchNum'] . "'>" . $row['BatchNum'] .' - '. $row['BatchName'] . "</option>";
}
echo "</select><br><br>";
echo "<label for='BatchName'>Batch Name</label>";
echo " <input name='BatchName' value='".$row['BatchName']."'><br>";
echo "<label for='SourceProduct'>Source Product</label>";
echo " <input name='SourceProduct' value='".$row['SourceProduct']."'><br>";
echo "<label for='SourceIngredient'>Source Ingredient</label>";
echo "<input name='SourceIngredient' value='".$row['SourceIngredient']."'><br>";
?>
</form>
</body>
</html>
工作的、修改过的代码,对不起代码墙,但它可以工作
数据库/includes/psl-configPDO.php 文件
<?php
$servername = "localhost";
$username = "DBadmin";
$password = "xxxxxxxx";
$dbname = "CDCtest";
?>
database/includes/db_connect_PDO.php 文件(从命令行工作)
<?php
require_once('psl-configPDO.php');
try{
$dbh = new PDO('mysql:host='.$servername.';dbname='.$dbname,$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
die(json_encode(array('outcome' => true)));
}
catch(PDOException $ex){
die(json_encode(array('outcome' => false,
'message' => 'Unable to connect to '.$servername.':'.$dbname.' with '.$username)));
}
$dbh = null;
?>
database/includes/jquery.min.js 是https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js的wget
数据库/CDC_Ajax_PDO.php 文件
?>
<?php
require_once('includes/psl-configPDO.php');
$db = new PDO('mysql:host='.$servername.';dbname='.$dbname,$username,$password);
$sth = $db->prepare('SELECT BatchName, SourceProduct, SourceIngredient FROM Batches WHERE BatchNum =:BatchNum');
$sth->bindParam(':BatchNum', $_POST['BatchNum'], PDO::PARAM_INT);
$sth->execute();
$data = new stdClass;
$status = 'failed';
if ($row = $sth->fetchObject() ) {
$data = $row;
$status = 'success';
}
header('Content-Type: application/json');
echo json_encode([
'status' => $status,
'data' => $data
]);
?>
数据库/CDC_WashFormPDO.php 文件
<?php
require_once('includes/psl-configPDO.php');
$pdo = new PDO('mysql:host='.$servername.';dbname='.$dbname,$username,$password);
$db = $pdo->prepare("SELECT BatchNum, BatchName FROM Batches ORDER by BatchNum DESC");
$db->execute();
?>
<!DOCTYPE HTML>
<html class="supernova"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta property="og:title" content="CDC Wash Run Entry" >
<meta property="og:description" content="Please click submit to complete this form.">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<meta name="HandheldFriendly" content="true" />
<link type="text/css" rel="stylesheet" href="styles/main.css" />
<head>
<title>CDC Wash Run Entry</title>
<script src = includes/jquery.min.js></script>
</head>
<body>
<form class="jotform-form" accept-charset="utf-8" id="WashForm" name="washform" method="POST">
<div class="form-all">
<ul class="form-section page-section">
<li id="cid_1" class="form-input-wide" data-type="control_head">
<div class="form-header-group ">
<div class="header-text httal htvam">
<h1>CDC Wash Run Entry</h2>
<label class="form-label form-label-top form-label-auto">Logged in as: <?php echo htmlentities($_SESSION['username']); ?> </label>
</div>
</div>
</li>
<li class="form-line" data-type="control_textbox">
<label class="form-label form-label-top form-label-auto" for="DateTimeCode">DateTimeCode</label>
<div class="form-input-wide">
<input type="text" class="form-control" size="20" name="DateTimeCode" value="<?php echo date('Y-m-d H:i:s'); ?>" required>
</div>
</li>
<li class="form-line" data-type="control_dropdown">
<label class="form-label form-label-top form-label-auto" for="choose-batch">Choose a Batch</label>
<div class="form-input-wide">
<select class='form-dropdown' id='choose-batch'>
<?php while($row = $db->fetchObject()): ?>
// show both name and number in drop-down
<option value="<?= $row->BatchNum ?>" ><?= $row->BatchNum." - ".$row->BatchName ?></option>
<?php endwhile; ?>
</select>
</div>
</li>
<li class="form-line" data-type="control_textbox">
<label class="form-label form-label-top form-label-auto" for="BatchName">Batch Name</label>
<div class="form-input-wide">
<input type='text' class='form-control' size='20' id="BatchName" name="BatchName" value="<?php echo $row['BatchName'];?>" />
</div>
</li>
<li class="form-line" data-type="control_textbox">
<label class="form-label form-label-top form-label-auto" for="SourceProduct">Source Product</label>
<div class="form-input-wide">
<input type="text" class="form-control" size="20" id="SourceProduct" name="SourceProduct" value="" required>
</div>
</li>
<li class="form-line" data-type="control_textbox">
<label class="form-label form-label-top form-label-auto" for="SourceIngredient">Source Ingredient</label>
<div class="form-input-wide">
<input type="text" class="form-control" size="20" id="SourceIngredient" name="SourceIngredient" value="" required>
</div>
</li>
</ul>
<ul class="form-section page-section" >
<li class="form-line" data-type="control_text" >
<div class="buttonrow">
<div class="col-1 form-buttons-wrapper">
<button type="submit" class="form-submit-button" id="submit_form" name="submit" value="Submit">Submit</button>
</div>
<div class="col-1 form-buttons-wrapper">
<a href="CDChome.php" >
<button type="button" class="form-submit-button" >
Home
</button>
</a>
</div>
<div class="col-1 form-buttons-wrapper">
<a href="includes/logout.php">
<button type="button" class="form-submit-button" >
Logout
</button>
</a>
</div>
</div>
</li>
</ul>
</div>
</form>
<script>
// monitor for changes in drop-down
$(document).ready(function() { // technically not necessary if script is at the bottom, but I do it out of habit
$('#choose-batch').on('change', function() {
retrieveItem( $(this).val() );
})
});
// send batchNum via ajax
function retrieveItem(BatchNumber) {
$.post(
"CDC_Ajax_PDO.php", // where to send data
{BatchNum: BatchNumber}, // parameters to send {varname: value}
function(result) { // what to do with results
if(result.status=='success') {
populateForm(result.data);
} else {
alert ('oops, failed');
}
}
);
}
// put results into page
function populateForm(data) {
$('#BatchName').val(data.BatchName);
$('#SourceProduct').val(data.SourceProduct);
$('#SourceIngredient').val(data.SourceIngredient);
}
</script>
</body>
</html>
【问题讨论】:
-
如果您有后续问题,请将其作为新问题发布,而不是将其编辑到不会被看到的问题中。
-
我没有后续问题,我正在发布我的解决方案进展状态,我们已经接近。
-
它现在可以工作了,非常感谢@Tim-Morton
-
请不要将工作代码添加到问题中。