我希望我的第一个字段(entrynum)在更新时执行MySQLi搜索,如果发现记录自动填充其余字段。
我已更新问题和原帖。我有一个表单字段,onchange正确触发,但无论我尝试什么都不返回任何数据。
reg.php
<?php include("process.php"); ?>
<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/entrynum.js"></script>
</head>
<body>
<?php
if (isset($_POST['reg-submit'])) {
echo "<p id='notice' style='padding: .5em; border: 2px solid red;'>Entry $entrynum Saved!<br>$timenow on $datenow<br><a href='upload.php' style='font-size:xx-large;'>Upload Pictures</a></p>";
} else {
echo "<p id='notice' style='display: none; padding: .5em; border: 2px solid red;'></p>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend><h1>Registration</h1></legend>
<label for="entrynum">Entry Number</label>
<input type="number" pattern="\d*" name="entrynum" id="entrynum" value="" required="true" placeholder="" autofocus onchange="entry_check()" />
<label for="fname">First Name</label>
<input type="text" name="fname" id="fname" value="" required="true" placeholder="" list="" style="text-transform:capitalize" onkeyup="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" />
<label for="lname">Last Name</label>
<input type="text" name="lname" id="lname" value="" required="true" placeholder="" list="" style="text-transform:capitalize" onkeyup="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" />
<input type="submit" name="reg-submit" id="reg-submit" value="Submit" />
</fieldset>
</form>
</body>
</html>
process.php
<?php
include("connect.php");
error_reporting(E_ALL);
ini_set('display_errors', 'On');
date_default_timezone_set('US/Central');
session_start();
$datenow = date("y-m-d");
$timenow = date("h:i:sa");
// registration
if (!empty($_POST['reg-submit'])) {
$entrynum = $_POST['entrynum'];
$_SESSION['entrynum'] = $entrynum;
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sql = "INSERT INTO HeatWaveData (entrynum, FName, LName)
VALUES ('$_POST[entrynum]','$_POST[fname]','$_POST[lname]')";
if (!$db->query($sql)) { die("Error: {$db->errno} : {$db->error}"); }
}
?>
entrynum.js
function entry_check() {
var entrynum = $("#entrynum").val();
// Send the AJAX call
$.post(
'entrysearch.php', // TO search.php page
{entrynum: entrynum}, // the DATA sent with the request
function(data) { // a CALLBACK function
if (data == 'none') { // no rows were found or an error occurred
document.getElementById("notice").innerHTML = "New Entry!";
document.getElementById("notice").style.display = "block";
return;
} else {
document.getElementById("notice").innerHTML = "Already Exists!";
document.getElementById("notice").style.display = "block";
}
data = JSON.parse(data); // parse the array returned from the server
// set the data in the matching fields (could be done manually if needed)
// for (var i = 0; i < data.length; i++) {
// $("#data" + i).val(data[i]);
// }
}
);
}
entrysearch.php
<?php
// check post data is received
if (!isset($_POST['entrynum'])) {
echo 'none';
die();
}
// create a prepared statement against sql-injection
$sql = $db->prepare("SELECT * FROM HeatWaveData WHERE entrynum=%d", $_POST['entrynum']);
// execute the query (depending on your db class type get the results otherwise)
$results = $db->query($sql);
// After this line results should be a matrix (change/add lined as needed)
// we need the first row returned (probably only 1 was returned...)
$result = $results[0];
// check if a row was returned
if (!$result) {
echo 'none';
die();
}
echo json_encode($result);
?>
答案 0 :(得分:0)
您需要做的是输入字段的onChange函数,该函数将向脚本发送ajax调用,该脚本将搜索您需要的数据。
然后使用JavaScript,您可以更新相关字段。
我正在这里写一个代码示例(没有选中),但是玩它并且它应该工作:
注意:我在我的代码中使用jQuery
foo.html
<script>
function input_change() {
// Get the value from the input
var entrynum = $("#entrynum").val();
// Send the AJAX call
$.post(
'search.php', // TO search.php page
{entrynum: entrynum}, // the DATA sent with the request
function(data) { // a CALLBACK function
if (data == 'none') { // no rows were found or an error occurred
return;
}
data = JSON.parse(data); // parse the array returned from the server
// set the data in the matching fields (could be done manually if needed)
for (var i = 0; i < data.length; i++) {
$("#data" + i).val(data[i]);
}
}
);
}
</script>
<input type="number" pattern="\d*" name="entrynum" id="entrynum" value="" required="true" placeholder="" autofocus onchange="input_change()" />
<input type="text" name="data0" id="data0">
<input type="text" name="data1" id="data1">
<input type="text" name="data2" id="data2">
<input type="text" name="data3" id="data3">
的search.php
<?php
//------------(maybe different depending on your db class type)----------------
// begin sesstion and connect to db and stuff
$db = //get database connection
// check post data is received
if (!isset($_POST['entrynum'])) {
echo 'none';
die();
}
// create a prepared statement against sql-injection
$sql = $db->prepare("SELECT * FROM table WHERE entrynum=%d", $_POST['entrynum']);
// execute the query (depending on your db class type get the results otherwise)
$results = $db->query($sql);
// After this line results should be a matrix (change/add lined as needed)
// we need the first row returned (probably only 1 was returned...)
$result = $results[0];
// check if a row was returned
if (!$result) {
echo 'none';
die();
}
echo json_encode($result);
?>