dongzong1866 2016-01-23 03:48
浏览 109

将参数添加到POST数据并提交

Is it possible to add parameters to your php $_POST data through javascript before submitting the form without making individual hidden input tags?

I have a page that uses ajax (non-jQuery) which parses the innerText of <td> elements and sends them to a php page for processing.

I'm now going to make a .csv file from that data (via the php page) and I can no longer do that via ajax. Therefore, I have to submit the whole form and leave the page. When doing this, I don't know how to add parameters to the $_POST data like I did with ajax. When the form is submitted, it only processes the input tags and I can't add my custom data like I did with the ajax call.

Thanks!

EDIT: Ajax code as requested by Matthew

function generateReport(saveReport)
{   
    xmlHttp=GetXmlHttpObject();
    var params = [];

    if(saveReport !== undefined)
    {
        params.push("reportName=" + document.myForm.reportName.value);
    }

    if (xmlHttp==null)
    {
      alert ("Browser does not support HTTP Request");
      return;
    } 

    var cols = document.myForm.colsUsedSel;

    if(cols.options.length == 0)
    {
        alert("Please choose columns to view in the report.");
        return;
    }

    //Loop through display columns
    for(var i = 0; i < cols.options.length; i++)
    {
        params.push("displayCols[]=" + cols.options[i].value);
    }

    //Loop through <tr>'s of "where clause" table
    var trList = document.getElementById("clauseTable").childNodes;

    for(var x = 0; x < trList.length; x++)
    {
        var tdList = trList[x].childNodes;

        for(var y = 0; y < tdList.length; y++)
        {
            var tdType = tdList[y].getAttribute("class");

            //Check if there is a class for this <td>
            if(!!tdType)
                params.push(tdType + "=" + encodeURIComponent(tdList[y].innerText));            
        }
    }


    //Loop through <tr>'s of "order by" table
    var trList = document.getElementById("orderByTable").childNodes;

    for(var x = 0; x < trList.length; x++)
    {
        var tdList = trList[x].childNodes;

        for(var y = 0; y < tdList.length; y++)
        {
            var tdType = tdList[y].getAttribute("class");

            //Check if there is a class for this <td>
            if(!!tdType)
                params.push(tdType + "=" + encodeURIComponent(tdList[y].innerText));            
        }
    }

    if(document.myForm.generateCSV.checked)
        params.push("generateCSV=1");
    else
        params.push("generateCSV=0");

    params.push("sid="+Math.random());
    xmlHttp.onreadystatechange= stateChangedGenerate;
    xmlHttp.open("POST","reportBuilderDB.php",true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(params.join("&"));
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法