我有一个表单,当用户 Select 一个地方时,我需要在文本框中获得数据.如果用户 Select 一个地点,则以下名称和金额将出现在下面的文本框中.我已经做了以下工作,但它不起作用.有谁能帮我这个忙吗?
I have tried the following:个
HTML个
<div>
<label for="" class="">Place</label>
<select name="place" id="place" onchange="myplace();">
<option value="">--Option from db--</option>
</select>
</div>
<div>
<label for="" class="">Name</label>
<input type="text" name="myname" id="myname" value="<?php //if there is data in databas then it is shown here...otherwise the value will change according to the above selection?>" >
</div>
<div>
<label for="" class="">Amount</label>
<input type="text" name="myamnt" id="myamnt" value="<?php //if there is data in databas then it is shown here...otherwise the value will change according to the above selection?>" >
</div>
MySQL:
if(!empty($_POST["myinitplace"]))
{
$sql = "SELECT * FROM all_data , my_place WHERE all_data.place=my_place.place and all_data.place = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $_POST["myinitplace"]);
$stmt->execute();
$result = $stmt->get_result();
while($rows = $result->fetch_assoc())
{
$data['name']=$rows['name'] ;
$data['myacc']=$rows['amount'] ;
}
echo json_encode($data);
}
Ajax个
function myplace()
{
var a = document.getElementById("place").value;
//alert(a);
$.ajax({
type : "POST",
data : {
myinitplace : a
},
dataType : "JSON",
success : function(data){
document.getElementById("myname").value = data.name;
document.getElementById("myamnt").value = data.myacc;
}
})
}