1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank-you -- The problem at this stage is that the variables are coming from an image (image.php?url=image.jpg&id=1&open=1), so there is no room for forms. I would agree that your way is clean and simple if that was the way I was able to do this. As I ultimately will have approx. 50+ images on the page, I don't want 50 forms on the page :)
0

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];

1 Comment

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?

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.