doujiao6507 2016-07-20 16:40
浏览 42
已采纳

将Jquery变量传递给php url

I have a jquery function that looks like this:

$('.dislike_box a').click(function(event){
    event.preventDefault();
    var title = $(this).data('title');
    alert(title);
    $.ajax({
    url: 'vote.php?vote=dislike&title=title', 
    success: function(result){
        $('.dislike_box p.vote_text').text('Dont Like it.');
    }});
});

My issue is when I alert the title variable it is correct but when I pass it to the php script to be put into the database the php script uses the $_GET[] method but puts the literal string "title" into the database instead of the variable value.

Answer: Thanks to @Ivan

 url: 'vote.php?vote=dislike&title='+title, 
  • 写回答

2条回答 默认 最新

  • dptpn06684 2016-07-20 16:43
    关注

    The variable you are sending via AJAX isn't a variable but the string 'title'. Instead use + to add the variable in the URL.

    $('.dislike_box a').click(function(event){
        event.preventDefault();
        var title = $(this).data('title');
        alert(title);
        $.ajax({
        url: 'vote.php?vote=dislike&title='+title, 
        success: function(result){
            $('.dislike_box p.vote_text').text('Dont Like it.');
        }});
    });
    

    To retrieve the js variable in PHP file :

    <?php
    
    if(isset($_REQUEST['title'])) {
        $title = $_REQUEST['title'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制