开发者

jQuery Passing Variables to MySQL from PHP

开发者 https://www.devze.com 2023-01-12 07:31 出处:网络
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:

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开发者_JAVA技巧 $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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号