I am trying to update multiple items, in the case span tags, through JQuery. I did manage to update a single item, using html dataType, but I have been unable to find the correct way to reference an array of items.
The problem seems to be in the setData function below. I was attempting to reference it as an ordinary array, but that does not seem to work.
This is the JQuery:
<script>
$(document).ready(function () {
$(".hook1").change(function () {
pass_id = (this.value);
var seltop = $(this).attr('id');
var lastchar = seltop.slice(-1);
$.ajax({
type: "POST",
url: "comp_data.php",
dataType: "json",
data: {
passval: pass_id,
pass2: lastchar
},
success: function setData(data) {
$("#price" + lastchar).html(data[0].price);
$("#matricule" + lastchar).html(data[1].matricule);
$("#tag" + lastchar).html(data[2].tag);
$("#ins_yr1" + lastchar).html(data[3].ins_yr1);
$("#Totalacq" + lastchar).html(data[4].Totalacq);
}
});
});
});
</script>
This is the relevant portion of the comp_data.php file:
<?php
$version_id = $_REQUEST['passval'];
$i = $_REQUEST['pass2'];
mysql_query("CREATE OR REPLACE VIEW vcomp AS (SELECT......");
$return = array(
"price" = > "",
"matricule" = > "",
"tag" = > "",
"ins_yr1" = > "",
"Totalacq" = > ""
);
$name = array_keys($return);
$ct = count($name);
$n = 0;
while ($n < $ct) {
$key = $name[$n];
$sql2 = mysql_query("SELECT *
FROM vcomp
ORDER BY segment_id,price
");
while ($row = mysql_fetch_assoc($sql2)) {
$return[$key] = $row[$key];
}
$n++;
}
$returnJSON = json_encode($return);
echo $returnJSON;
This last echo of $returnJSON gives
{"price":"1014000","matricule":"10000","tag":"6000","ins_yr1":"25350","Totalacq":"1055350"}
And that's what I want to output with the setData function.
I appreciate any ideas.