0
私の最初のフィールド(entrynum)は更新時にMySQLi検索を実行し、残りのフィールドにはレコードが自動入力されます。sqlは私が試しても何のデータも返しません
質問とオリジナルの投稿を更新しました。私は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);
?>
私が正しく理解していれば、あなたは必要があります(HTTP [イベントonfocusout]://www.w3schools .com/jsref/event_onfocusout.asp)を使用して、[ajax呼び出し](http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp)をあなたにスクリプト(coul'd PHPにする)をそのフィールドの値をデータベースに照会し、有効なデータを返すかどうかをチェックします(他のフィールドのデータに設定されています)(http://www.w3schools.com/jsref/prop_text_value.asp)。それぞれのリンクに例があり、それが役立つことを願っています。 –