我正在尝试获取在选择选项中选择的某个项目的某些数据,并自动将其显示在文本字段中,如下图所示:
但它不起作用。是否有补救措施或这种类型的编码根本不可行?希望有人可以帮助我。
这是我制作的代码:
$credit_accounts = $db->query("SELECT * FROM credits");
echo'<select id="customer_order_id" name = "customer_order_id">
<option value = "">SELECT ACCOUNT</option>';
foreach($credit_accounts as $key){
$id = $key['purchase_order_id'];
$name = $key['customer_firstName']."\t\t".$key['customer_lastName'];
echo'<option value = "'.$id.'"><a href="?info='.$id.'">'.$name.'</a></option>';
}
echo'
</select>';
注意:该链接将执行一个查询,该查询将检索所选项目的某些数据。如果在没有<option></option>
标记的循环外声明链接,则它会相应地起作用。但是如果它在<option></option>
标签的循环中,它就不像链接那样工作了。请帮帮我。
答案 0 :(得分:-2)
对不起我的英语,我会写一些很长的答案,希望它也能帮助别人,所以我给予了解决方案。
请注意:此解决方案已编写并使用 SIMPLE MYSQL,AJAX和PHP函数如果您还想要数据安全,请参阅[PHP MySQLI Prevent SQL Injection
目标:从DATABASE获取/显示与特定(1)用户/产品相关的数据,然后在HTML页面中获取/显示它(另外:没有页面刷新)IN A FORM。
代码:
<?php
//php-database connection script
$db_config = mysqli_connect("localhost", "username", "pass", "database_name");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
?>
<?php
//this pHP SCRIP FIRES when you select a USERNAME (if u want not to SELECT but type then change ONCHANGE to ONKEYUP function & change username input type to text)
if(isset($_POST['uname'])){
$u = $_POST['uname'];
$sql = "SELECT email , gender FROM credits WHERE username='$u' LIMIT 1";
$query= mysqli_query($db_config,$sql);
while($row2 = mysqli_fetch_array($query)){
//Here we form a STRING SEPRATED BY QUAMMAS and ECHO IT TO PAGE
//later on we will fill this data in input fields
echo $row2['email'].",".$row2['gender'];
exit();
}exit();}
?>
<!-- HERE is your form where user will select or type his username-->
<form >
Select username :<br/>
<select id="user_name" onchange="load_data()">
<option value=""> select an ouser</option>
<option value="v1111"> v111</option>
</select><br/>
Email<br/>
<input type ="text" id="email" value=""><br/>
Gender :
<select id="gender">
<option value='m'>male</option>
<option value='f'>female</option>
</select>
</form>
<span id='status' ></span>
<script>
//AJAX OBJECT CREATION script it works as get or post mechanism
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();
x.open(meth, url, true);
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState === 4 && x.status === 200){
return true;
}
}
//here is function which fires when username is selected
function load_data(){
var value = document.getElementById('user_name').value;
//declare ajax obj, and name(url) of same page you are coding.
var ajax = ajaxObj("POST", "url.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
var str = ajax.responseText;
//split STRING and extract individual data
var res = str.split(',');
//here we fetch his username into input field BINGO!do same for gender.
_('email').value = res[0];
//res[1] will have gender related data.
}
}
//Declaration to send URL encoded variable (username in this case)
ajax.send("uname="+value);
}
</script>
如果您需要有关数据安全的更多资源和信息,请参阅评论部分中的BRAD评论