2

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.

1 Answer 1

1

I don't know PHP, but from what I just read, you have an "associative array." And this says an associative array is converted by json_encode() to an object. So you should use:

$("#price" + lastchar).html(data.price);
$("#matricule" + lastchar).html(data.matricule);
$("#tag" + lastchar).html(data.tag);
$("#ins_yr1" + lastchar).html(data.ins_yr1);
$("#Totalacq" + lastchar).html(data.Totalacq);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your post. I just tried it but still no output. It seems that everything else is working ( posting to php,php file ), so I am still at a loss.
UPDATE: IT'S WORKING NOW. I believe the issue was that I left some headings on the php file comp_data.php that were somehow getting things messed up when the data was being passed back. So, thanks again John S for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.