duan02468 2015-12-09 23:45
浏览 37

JavaScript - 将发布请求发送到PHP元素

        var table = $('#accessRequest').DataTable({
            "dom": 'Blfrtip',
            "ajax": "Editor-PHP-1.5.1/php/editor-serverside.php",
            "columns": [
                {   //special column for detail view
                    "className": 'details-control',
                    "orderable": false,
                    "data": null,
                    "defaultContent": ''
                },
                {   data: "DT_RowId",
                    render: function ( data ) {
                        return data.replace( 'row_', '' );
                    }
                },
                {   data: null,
                    render: function ( data, type, row ) {
                        // Combine the first and last names into a single table field
                        return data.first+' '+data.last;
                    }
                },
                { "data": "phone" },
                { "data": "responsibleParty" },
                { "data": "email" },
                { "data": "building" },
                { "data": "typeOfWork" },
                { "data": "startTime" },
                { "data": "endTime" },
                { "data": "description" },
                { "data": "dockNeeded" },
                { "data": "numPeople" },
                { "data": "numTrucks" },
                { "data": "requestPlaced" },
                { "data": "updatedAt" },
                { "data": "approved" },
                { "data": "approvedBy" },
                { "data": "approvedAt" },
                { "data": null }
            ],
            "aoColumnDefs": [
                {
                    "aTargets": [-1],
                    "mData": null,
                    "mRender": function (data, type, full) {
                    return '<button id="ApprovalButton" onclick="$.post(\'extra.php\', \'approve_request\')" action="extra.php" method="post"> Process </button>';
                 //Send post request
                    }
                }
            ],
            "order": [[1, 'asc']],
            select: true,
            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit",   editor: editor },
                { extend: "remove", editor: editor }
            ]
        });

I have a string of code (above) in which I am creating a table to contain information. On the line that says "//send post request" I'm trying to make it so the button directly above it sends a post request to a separate file called "extra.php", and despite what I've done I haven't been able to that.

I've tried using a submit button in an input form, but that didn't work. I've tried a lot of different things, but I can't seem to get it to work. Any help would be appreciated.

There is already a file (extra.php) that is capable of receiving a post request and acting on it once it has received it. I'm attempting to create a button in HTML on a page, that when clicked posts 'approve_request' to the file "extra.php"

Sorry, I'm new to both coding and this website, there's probably something simple that I'm not doing.

    <!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php
header('Access-Control-Allow-Origin: *'); 
$id = intval($_POST['id']);

$con = mysql_connect("RequestAccess.db.10160035.hostedresource.com","RequestAccess","br@6HeCher");
if (!$con){
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("RequestAccess", $con);

//if action sent is approve set request as apporved
if(isset($_POST['approve_request'])){
    $sql = "UPDATE AccessRequests
            SET approved='1', approvedAt=current_timestamp
            WHERE AccessID='" . $_POST['approve_request']."'";

    $results = mysql_query($sql);
}
//else action sent must be for child rows so populate child row
else {
}

This is the section responsible for handling the post request, if that helps.

  • 写回答

2条回答 默认 最新

  • dsa111111 2015-12-09 23:57
    关注

    I think you are missing this:

    "processing": true,
    "serverSide": true,
    

    DataTabele needs these directives to know that you want perform the request to the php script.

    And maybe also this:

    "ajax": "your php.php"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大