weixin_33694620 2016-08-18 15:04 采纳率: 0%
浏览 15

Ajax代码未执行

I am building a basic social network site with a function to create groups. All my code works perfectly, except for this little piece of code that declines or approves pending members. The following PHP code displays a list of the pending members in the specific group:

if (in_array($_SESSION['username'],$moderators)){
    $addMembers = "Pending members";
    for($x=0;$x<$pend_count;$x++){      
        $addMembers .= '<a href="'.$pending[$x].'">'.$pending[$x].'</a>';
        $addMembers .= '<button onclick="approveMember('.$pending[$x].')">Approve</button>';
        $addMembers .= '<button onclick="declineMember('.$pending[$x].')">Decline</button>';
    }
}

The onclick events should trigger the following JS scripts:

function approveMember(u){
    var ajax = ajaxObj("POST", "php_parsers/group_parser.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            var datArray = ajax.responseText;
            if(datArray == "member_approved"){
                alert ("Member approved!");
            }
        }
    }
    ajax.send("action=approve_member&u="+u);
}


function declineMember(u){
    var ajax = ajaxObj("POST", "php_parsers/group_parser.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            var datArray = ajax.responseText;
            if(datArray == "member_declined"){
                alert ("Member Declined");
            }
        }
    }
    ajax.send("action=decline_member&u="+u);
}

The AJAX code executes a PHP script in the background to approve/decline a member and give a response 'member_approved' or 'member_declined', which should then trigger an alert in the JS.

Now, I have about 10 of these onclick event triggers to execute some AJAX code, which are all working fine, except these approveMember() and declineMember(). They just don't want to trigger my JS scripts and I can't find out why. I am sure the problem is not in the PHP script that should be executed in the background.

  • 写回答

2条回答 默认 最新

  • elliott.david 2016-08-18 15:12
    关注

    So if $pending[$x] is a string your code looks like this when rendered

    <button onclick="approveMember(foobar)">
    

    So now when you click on the button, it is looking for a variable named foobar. You need to wrap the string in quotes.

    $addMembers .= '<button onclick="approveMember(\''.$pending[$x].'\')">Approve</button>';
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法