weixin_33675507 2016-06-13 19:43 采纳率: 0%
浏览 18

AJAX将jQuery发布到PHP $ _POST

I have this modal jQuery AJAX:

        $('#switch_modal').on('show.bs.modal', function (e) {
        var rowid = $(e.relatedTarget).attr('data-id');
            $.ajax({
                type : 'post', // commented for this demo
                url : 'pars.php', // commented for this demo 
                data :  'id='+ rowid,
                success : function(data) {
                $('.fetched-data').show().html(rowid); // show rowid for this demo
                }
            });
        });

My mysql query:

$query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC";
while ($row = $result->fetch_assoc()) {

My modal data-id:

<a href="#viewgame" data-toggle="modal" data-id="<?php echo $row['id'];?>"">

How can i do to use the var rowid like a PHP post? Something like that:

$id = $_POST['rowid'];
echo $id;
  • 写回答

1条回答 默认 最新

  • Lotus@ 2016-06-13 19:52
    关注

    If i understand well this test your doing...
    Your JavaScript rowid is the value. The $_POST identifier is id.

    Try this in your PHP:

    $id = $_POST['id'];
    echo $id;
    

    You'll get the javascript rowid sent as a POST value (named as $_POST['id']) via ajax...
    And returning in data on ajax success.

    $('.fetched-data').show().html(data);
    

    So you'll have to use data in your jQuery html()... Wich is the echoed text.



    -----
    EDIT AFTER ACCEPTATION of this answer
    (Based on your last comment)

    So i have this query:

    $query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC";
    if ($result = $conn->query($query)) {
        while ($row = $result->fetch_assoc()) {
            $gameid = $row['id'];
        }
    }
    

    So i want to use $gameid variable into this query:

    $sql = "SELECT * FROM games WHERE id='".$gameid."'";
    


    I understand, that you want to get THE LAST matching full line where winner value is empty from games table.
    No need for an ajax call...
    No need for a second query.

    Just do it:

    $query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC";
    $result = $conn->query($query);
    $row = $result->fetch_assoc();
    
    for ($i=0;$i<sizeOf($row);$i++){    // This is the «size» (number of values) of one row, the last fetched.
        echo $row[$i] . "<br>";
    }
    

    You'll get all your line values echoed...
    This will be the LAST matching line fetched.


    If you have many matching lines and want all results, do it like this:

    $query="SELECT * FROM games WHERE winner='' ORDER BY amount DESC";
    $result = $conn->query($query));
    
    while ($row = $result->fetch_assoc()) {     // While fetching, echo all values of one matching line.
        echo "row id: " . $row['id'] . "<br>";
        echo "values: <br>";
        for ($i=0;$i<sizeOf($row);$i++){
            echo $row[$i] . "<br>";
        }
    }
    

    Notice that this script, that I suggest, will enlight you about the while fetch loop possible results. You'll have to work a little to have it displayed correctly on your page.
    ;)

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型