doupu1957 2016-04-23 14:05
浏览 120

使用PHP生成的按钮生成POST请求,该按钮使用参数调用javascript函数

First of all, sorry for the weird title! Currently im facing the following problem:

I have a PHP script which creates a HTML button

echo '<input type="button" id="$entryID" name="change_title" value="change title" onclick="sendRequest(sendRequest($type, $entryID));" />';

This button should call a script, which creates a post request with the parameter

$type //is used to recognize which mysqli table should be adressed
$entryID //is used to recognize which entry in table sould be adressed

I know that I can not create a post request with javascript because its for front-end developement. So I have done some researches and found AJAX. So im never realy used it. But I tried to create a post request like this (in the same file):

<script>
    // t = type & i = ID
    function sendRequest(t, i){

        var request = $.ajax({
        type: "POST",
        url: "admin_page.php",
        data: {"type": t, "id": i},
        success: function(){
            console.log('request via AJAX');
        }
    });

        return request;
    }
</script>

After the script has run I use the following php script to check if it has worked:

if(isset($_POST['type'])){
    if(isset($_POST['id'])){
        printf('Post request sent');
    }
}

I have checked the AJAX and the javascript documentations and have not found anything. It this process even possible? What am I doing wrong?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doulu4976 2016-04-23 14:16
    关注

    I edited this answer after realizing that this was part of an echo statement. This should work:

    echo '<input type="button" id="'.$entryID.'" name="change_title" value="change title" onclick="sendRequest('.$type.','.$entryID.'));" />';

    Also I would advise against calling the JS function with variables generated via PHP. It is not elegant. Put your variables as data-* attributes and use JS to pull these attributes

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路