change img src on select box selection
Posted
by
user1871596
on Stack Overflow
See other posts from Stack Overflow
or by user1871596
Published on 2012-12-03T05:02:27Z
Indexed on
2012/12/03
5:03 UTC
Read the original article
Hit count: 227
i have a form that auto populates input fields using jquery and ajax i can not get the img url to change in the img src when i select the option from dropdown
my dropdown is dynamicaly populated here is is my function
<script type="text/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] );
}
}
});
});
});
</script>
here is get_driver_data2.php
<?php include ('dbc.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 );
?>
a bit of the html
<td>
<img id="image_path" src="????" />
</td>
</tr>
</table>
<p><strong>
<input type="submit" value="Complete Dispatch">
</strong></p>
how do i if at all possible populate the src with the database vaule ajax recieved when i change the select box all other data is populated and the string is returned correctlly i have tested that by placing an input box and calling input#image_path => $getphoto. is there syntax for an img tag like the input....textarea....etc. I have tried including the get....php inline and assigning the src to $getphoto no luck there I was looking at trying to make a hidden input field with the ajax passed data and then taking that data and making it a var but can not figure that out either.
thanks
© Stack Overflow or respective owner