Reputation: 65
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.
Upvotes: 1
Views: 841
Reputation: 572
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.
Upvotes: 1
Reputation: 81384
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];
Upvotes: 0