将MySQL数据加载到PHP
这是我代码的前端。我需要在选择CUID时自动加载数据库内容。例如,如果我选择CustomerID1,那么它应该自动将数据加载到相关字段。 有人能帮我做些什么吗将MySQL数据加载到PHP,php,mysql,Php,Mysql,这是我代码的前端。我需要在选择CUID时自动加载数据库内容。例如,如果我选择CustomerID1,那么它应该自动将数据加载到相关字段。 有人能帮我做些什么吗 </head> <body> <h1>Customer Profile Search Page</h1> <blockquote> <div> <form> <
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
以下是当前代码
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
客户资料搜索页面
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
客户资料搜索页面
客户ID
一些伪代码可以帮助您入门
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
<?php
/*
place at top of the same script as the form as the ajax uses the current location.href as url
alternatively use a separate script and change the url in the ajax function
*/
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['cus_id'] ) ){
ob_clean();
/* prepare variables */
$cust_id=$_POST['cus_id'];
/* etc */
/* prepare sql */
$sql='select * from table where cust_id=$cust_id';
/* execute sql */
/* return recordset to ajax callback */
print_r( $recordset );
exit();
}
?>
<script>
( function(){
var bindEvents=function(){/* assign onchange event handler */
document.getElementById('cus_id').onchange=evtHandler;
};
var evtHandler=function(event){/* the event handler fires off an ajax request ( via POST )*/
var el=event.target || event.srcElement;
ajax.call( this, 'post', document.location.href, { cust_id:el.options[ el.options.selectedIndex ].value }, evtCallback );
};
var evtCallback=function( response ){/* callback function to populate form fields */
alert('use the response data ['+response+'] to populate the form fields');
};
var ajax=function(m,u,p,c){/* basic ajax function */
/*
m=method, u=url, p=params(object), c=callback
*/
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if( xhr.readyState==4 && xhr.status==200 )c.call(this,xhr.response);
};
var params=[];
for( var n in p )params.push(n+'='+p[n]);
switch( m.toLowerCase() ){
case 'post': p=params; break;
case 'get': u+='?'+params; p=null; break;
}
xhr.open( m.toUpperCase(), u, true );
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send( p );
};
document.addEventListener( 'DOMContentLoaded', bindEvents, false );
}());
</script>
(功能(){
var bindEvents=function(){/*分配onchange事件处理程序*/
document.getElementById('cus_id')。onchange=evtHandler;
};
var evtHandler=function(event){/*事件处理程序(通过POST)发出ajax请求*/
var el=event.target | | event.src元素;
call(this'post',document.location.href,{cust_id:el.options[el.options.selectedIndex].value},evtCallback);
};
var evtCallback=用于填充表单字段的函数(响应){/*回调函数*/
警报(“使用响应数据['+response+']填充表单字段”);
};
var ajax=函数(m,u,p,c){/*基本ajax函数*/
/*
m=方法,u=url,p=参数(对象),c=回调
*/
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(xhr.readyState==4&&xhr.status==200)c.call(这是xhr.response);
};
var参数=[];
对于(p中的变量n)参数push(n+'='+p[n]);
开关(m.toLowerCase()){
案例“post”:p=params;break;
大小写“get”:u+=“?”+参数;p=null;break;
}
xhr.open(m.toUpperCase(),u,true);
setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(p);
};
document.addEventListener('DOMContentLoaded',bindEvents,false);
}());
一些伪代码可以帮助您入门
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
<?php
/*
place at top of the same script as the form as the ajax uses the current location.href as url
alternatively use a separate script and change the url in the ajax function
*/
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['cus_id'] ) ){
ob_clean();
/* prepare variables */
$cust_id=$_POST['cus_id'];
/* etc */
/* prepare sql */
$sql='select * from table where cust_id=$cust_id';
/* execute sql */
/* return recordset to ajax callback */
print_r( $recordset );
exit();
}
?>
<script>
( function(){
var bindEvents=function(){/* assign onchange event handler */
document.getElementById('cus_id').onchange=evtHandler;
};
var evtHandler=function(event){/* the event handler fires off an ajax request ( via POST )*/
var el=event.target || event.srcElement;
ajax.call( this, 'post', document.location.href, { cust_id:el.options[ el.options.selectedIndex ].value }, evtCallback );
};
var evtCallback=function( response ){/* callback function to populate form fields */
alert('use the response data ['+response+'] to populate the form fields');
};
var ajax=function(m,u,p,c){/* basic ajax function */
/*
m=method, u=url, p=params(object), c=callback
*/
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if( xhr.readyState==4 && xhr.status==200 )c.call(this,xhr.response);
};
var params=[];
for( var n in p )params.push(n+'='+p[n]);
switch( m.toLowerCase() ){
case 'post': p=params; break;
case 'get': u+='?'+params; p=null; break;
}
xhr.open( m.toUpperCase(), u, true );
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send( p );
};
document.addEventListener( 'DOMContentLoaded', bindEvents, false );
}());
</script>
(功能(){
var bindEvents=function(){/*分配onchange事件处理程序*/
document.getElementById('cus_id')。onchange=evtHandler;
};
var evtHandler=function(event){/*事件处理程序(通过POST)发出ajax请求*/
var el=event.target | | event.src元素;
call(this'post',document.location.href,{cust_id:el.options[el.options.selectedIndex].value},evtCallback);
};
var evtCallback=用于填充表单字段的函数(响应){/*回调函数*/
警报(“使用响应数据['+response+']填充表单字段”);
};
var ajax=函数(m,u,p,c){/*基本ajax函数*/
/*
m=方法,u=url,p=参数(对象),c=回调
*/
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(xhr.readyState==4&&xhr.status==200)c.call(这是xhr.response);
};
var参数=[];
对于(p中的变量n)参数push(n+'='+p[n]);
开关(m.toLowerCase()){
案例“post”:p=params;break;
大小写“get”:u+=“?”+参数;p=null;break;
}
xhr.open(m.toUpperCase(),u,true);
setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(p);
};
document.addEventListener('DOMContentLoaded',bindEvents,false);
}());
您应该在控制器和标记之间进行分离
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
在
关闭标记之前,加载jquery并创建以下脚本:
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
<script src="jquery.js"></script> <!--- Load jquery here --->
<script>
$(document).ready(function() {
$('#cus_id').change(function() {
var cusId = $(this).val();
getData(cusId);
});
function getData(cusId) {
$.get('ajax.php?cus_id=' + cusId)
.success(function(data) {
$('input[name="cusname"]').val(data.cusname);
// ... Fill all your fields with the retrieved data.
})
.error(function(error) {
alert(error);
});
}
});
</script>
$(文档).ready(函数(){
$('#cus_id')。更改(函数(){
var cusId=$(this.val();
getData(cusId);
});
函数getData(cusId){
$.get('ajax.php?cus_id='+cusId)
.成功(功能(数据){
$('input[name=“cusname”]').val(data.cusname);
//…用检索到的数据填充所有字段。
})
.错误(函数(错误){
警报(错误);
});
}
});
在php脚本中,只需在模板中进行如下SQL查询:
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql='select * from custprofile where cus_id = $_GET["cus_id"]';
$result = $conn->query($sql);
$data = array();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
// Return the result
return json_encode($data);
您应该在控制器和标记之间进行分离
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
在
关闭标记之前,加载jquery并创建以下脚本:
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
<script src="jquery.js"></script> <!--- Load jquery here --->
<script>
$(document).ready(function() {
$('#cus_id').change(function() {
var cusId = $(this).val();
getData(cusId);
});
function getData(cusId) {
$.get('ajax.php?cus_id=' + cusId)
.success(function(data) {
$('input[name="cusname"]').val(data.cusname);
// ... Fill all your fields with the retrieved data.
})
.error(function(error) {
alert(error);
});
}
});
</script>
$(文档).ready(函数(){
$('#cus_id')。更改(函数(){
var cusId=$(this.val();
getData(cusId);
});
函数getData(cusId){
$.get('ajax.php?cus_id='+cusId)
.成功(功能(数据){
$('input[name=“cusname”]').val(data.cusname);
//…用检索到的数据填充所有字段。
})
.错误(函数(错误){
警报(错误);
});
}
});
在php脚本中,只需在模板中进行如下SQL查询:
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql='select * from custprofile where cus_id = $_GET["cus_id"]';
$result = $conn->query($sql);
$data = array();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
// Return the result
return json_encode($data);
提供这种用户体验的唯一方法是通过
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
您可以手工编写实现代码或使用第三方库,如
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
请记住,您是在服务器端查询用户的数据,然后需要在用户未注意到的情况下将其传递到后面的DOM。
通过仅使用PHP,您将迫使用户经历两次旅行,第一次是在部署表单时,第二次是在服务器响应表单中选定的用户id请求时
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
通过在后台工作,在不被察觉的情况下从服务器获取数据,AJAX将隐藏甚至加快这一点
</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>
关于如何操作,有成千上万的教程。提供这种用户体验的唯一方法是通过。</head>
<body>
<h1>Customer Profile Search Page</h1>
<blockquote>
<div>
<form>
<label>Customer ID</label>
<select name="cus_id" id="cus_id" class="combo">
<?php
$servername = "localhost";
$username = "cusAdmin";
$password = "123456";
$dbname = "cusid";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connection_error) {
die("connection failed: " . $conn->connection_error);
}
$sql = "select * from custprofile";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
//echo "id: " . $row["cus_id"]. "name:" . $row[cus_name]. "dof:" . $row[dob]. "doj:" . $row[cuEmail]."<br>";
echo "<option value='" . $row["cus_id"] . "'>" . $row["cus_id"] . "</option>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</select>
<br>
<label>Customer Name</label><input type="text" name="cusname"><br>
<label>Date of Birth</label><input type="text" name="dob"><br>
<label>Date of Join</label><input type="text" name="doj"><br>
<label>Phone No</label><input type="text" name="phone"><br>
<label>Email</label><input type="text" name="email"><br>
<label>Gender</label><select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">other</option>
</select><br>
<label>Address</label><textarea name="message" rows="3" cols="30"></textarea><br>
<input type="submit" value="Submit">
</form>
</div>
</blockquote>
</body>