这是我的代码的前端。我需要在选择 CUID 时自动加载数据库内容。例如,如果我选择客户 ID 1,那么它应该自动将数据加载到相关字段。 谁能帮我解决需要做的事情?
下面是当前代码;
客户资料搜索页面
</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>
关于php - 将 MySQL 数据加载到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35252194/