In this test I have a simple form with two fields. I need upadate the text field when a user select an option in the select field.
This is the html code:
<form id="form1" name="form1" method="post" action="">
<input type="text" name="dst" id="dst" value="" />
<select name="contactos" id="contactos">
<option value="-1">No selected</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</form>
This is the jquery code:
<script>
$(document).ready(function() {
$("#contactos").change(function(event){
var id = $("#contactos").find(':selected').val();
$.getJSON('contacback.php?contactoID='+id, function(data) {
$.each(data, function(i,item) {
if (item.field == "dst") {
$("#dst").val(item.value); }
});
});
});
});
</script>
(added)This is the php code:
<?php require_once('Connections/cnx3.php'); ?>
<?php
$colname_rsConta开发者_如何学运维ctos = "-1";
if (isset($_GET['contactoID'])) {
$colname_rsContactos = $_GET['contactoID'];
}
mysql_select_db($database_cnx3, $cnx3);
$query_rsContactos = sprintf("SELECT contactoID, telefono FROM contactos WHERE contactoID = %s",
GetSQLValueString($colname_rsContactos, "int"));
$rsContactos = mysql_query($query_rsContactos, $cnx3) or die(mysql_error());
while ($fila = mysql_fetch_array($rsContactos)) {
$json = array('field' => 'dst',
'value' => $fila['telefono']);
}
echo json_encode($json);
mysql_free_result($rsContactos);
?>
The query to php is sent OK and the JSON answer it's also OK image of JSON reply
But the value of #dst is not updated. Any helps is welcome !!
(Answered in a question edit and comments. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
Solved!! Thanks to @Attack the
jQuery
script:
$("#contactos").change(function(event){
var id = $("#contactos").find(':selected').val();
$.getJSON('contacback.php?contactoID='+id, function(data) {
if (data.field == "dst") {
$("#dst").val(data.value);
}
});
});
精彩评论