我有一个使用 jQuery 和 AJAX 填充输入字段的表单。当我从动态填充的下拉列表中选择选项时,我无法img
更改 URL 。src
JavaScript:
$(document).ready(function(){
$("#id").change(function(){
$.ajax({
url : 'get_driver_data2.php',
type : 'POST',
dataType: 'json',
data : $('#ContactTrucks').serialize(),
success: function( data ) {
for(var id in data) {
$(id).val( data[id] );
}
}
});
});
});
PHP:
$id_selected = $_POST['id']; // Selected Id
$query = "SELECT * from admin_dispatch_records where id = '$id_selected' AND driver LIKE '%$username%'";
$result = mysqli_query($dbcon, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$eta = $row['eta'];
$time = $row['dispatch_time'];
$date = $row['dispatch_date'];
$name = $row['name'];
$phone = $row['phone'];
$vehicleyear = $row['vehicleyear'];
$color = $row['color'];
$make = $row['make'];
$model = $row['model'];
$vin = $row['vin'];
$plate = $row['plate'];
$mileage = $row['mileage'];
$pickup = $row['pickup'];
$dropoff = $row['dropoff'];
$price = $row['price'];
$invoice = $row['invoice'];
$cash = $row['cash'];
$credit = $row['credit'];
$check = $row['check'];
$po = $row['po'];
$billed = $row['billed'];
$need_to_bill = $row['need_to_bill'];
$getphoto = $row['image_path'];
}
$arr = array( 'input#eta' => $eta, 'input#dispatch_time' => $time, 'input#dispatch_date' => $date, 'input#name' => $name, 'input#phone' => $phone, 'input#vehicleyear' => $vehicleyear, 'input#color' => $color, 'input#make' => $make, 'input#model' => $model, 'input#vin' => $vin, 'input#plate' => $plate, 'input#mileage' => $mileage, 'textarea#pickup' => $pickup, 'textarea#dropoff' => $dropoff, 'input#price' => $price, 'input#invoice' => $invoice, 'input#cash' => $cash, 'input#credit' => $credit, 'input#check' => $check, 'input#po' => $po, 'input#billed' => $billed, 'input#need_to_bill' => $need_to_bill, 'image#image_path' => $getphoto);
echo json_encode($arr);
一些HTML:
<td>
<img id="image_path" src="????" />
</td>
</tr>
</table>
<p><strong>
<input type="submit" value="Complete Dispatch">
</strong></p>
src
当我更改选择框时,如何使用来自 AJAX 调用的数据库值填充?填充所有其他数据并正确返回字符串。我已经通过放置一个输入框并包含input#image_path => $getphoto
. img
标签的语法与input
or不同textarea
吗?我尝试过包含 PHP 内联并将其分配src
给$getphoto
没有运气。我希望使用 AJAX 传递的数据创建一个隐藏的输入字段,然后获取该数据并将其设为 var,但也无法弄清楚。