doucan4873 2015-04-30 11:42
浏览 74

使用POST将JavaScript变量传递给PHP

I read somewhere that you don't need a form or hidden value to pass data when using ajax. If I could even get an example I would be grateful.

index.js:

function collectData(r) { 
    //gets the rows index
    var i = r.parentNode.parentNode.rowIndex;
    //selects the row picked
    var sliceRow = document.getElementById('Sona').rows[i];
    //have access to indiviual cells in the row
    var sliceCell = sliceRow.cells;
    var song = (sliceCell[0].innerHTML);
    var artist = (sliceCell[1].innerHTML);
    $.post("myLibrary.php", { postsong: song, postartist: artist });
}

PHP file :

if (isset($_POST)) {
    $song = $_POST['postsong'];
    echo $song;
}
  • 写回答

1条回答 默认 最新

  • duanfei8399 2015-04-30 11:51
    关注
        $.ajax({
            url  : example.com', 
            contentType : 'application/x-www-form-urlencoded',
            type : 'post',
            data : {
             i         : r.parentNode.parentNode.rowIndex,
             sliceRow  : document.getElementById('Sona').rows[i],
             sliceCell : sliceRow.cells,
             song      : (sliceCell[0].innerHTML),
             artist    : (sliceCell[1].innerHTML),
            },
            success:function(data) {
                // blabla
            }, 
        });
    
    
        <?php
    
        if(isset($_POST)) {
            print_r($_POST);
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误