views:

32

answers:

2

The below code works as far as I can tell except for the var tid and var open section. They are being submitted from an image href:

$("#chngeHref").click(function() {

    var tid  = $('tid').attr('value'); // VARIABLES DONT WORK
    var open = $('open').attr('value'); // VARIABLES DONT WORK

    $.ajax({
        type: "POST",
        url: "update.php",
        data: "tid="+ tid +"& open="+ open,
        success: function(){ 
            $('#chnge').fadeTo('slow',0.4);
        }
     });
 });    

HTML code this is coming from:

<a href="#" id="chngeHref" /><img src="<?php echo "image.php?url=" . $row[2]; ?>?tid=<?php echo $row[0]; ?>&open=<?php echo $row[1]; ?>" id="chnge" /></a>

Which works perfectly fine (output: image.php?url=image.jpg?tid=1&open=1). The issue is I don't think I have the var tid/open set up right to actually read the variables and pass them onto my mysql page (where I need to values to update the db). I have tried:

var tid     = $('tid').attr('value');
var tid     = $('.tid').attr('value');
var tid     = $('#tid').attr('value');

I just don't know enough to make it work. Any suggestions are much appreciated.

Phillip.

A: 

You need to parse the src attribute of chnge and grab the tid:

var chnge = document.getElementById('chnge');
var tid = chnge.src.match(/&tid=([^&])/)[1];

Same thing for open:

var chnge = document.getElementById('chnge');
var tid = chnge.src.match(/&open=([^&])/)[1];
Delan Azabani
Thank-you... But hasnt that 2nd one now just overridden the first because they are under the same var's? var chnge is the same in both lines, and var tid is different but the 2nd will override the first?
Phillip L
+1  A: 

I think the best thing you can do is pass the variables to a hidden field, so you can easily access the information.

Something like that:

HTML

<input type="hidden" id="hfTid" value="<?php echo $row[0]; ?>" />
<input type="hidden" id="hfOpen" value="<?php echo $row[1]; ?>" />

jQuery

$("#chngeHref").click(function() {

    var tid = $('input#hfTid').val();
    var open = $('iput#hfOpen').val();

    $.ajax({
        type: "POST",
        url: "update.php",
        data: "tid="+ tid +"& open="+ open,
        success: function(){ 
            $('#chnge').fadeTo('slow',0.4);
        }
    });
});  

I would do it this way. It's cleaner.

I'm glad if helped.

Philipe
Phillip L