简体   繁体   English

AJAX将jQuery发布到PHP $ _POST

[英]AJAX post jQuery to PHP $_POST

I have this modal jQuery AJAX: 我有这个模态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: 我的mysql查询:

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

My modal data-id: 我的模态数据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? 我该如何像php post一样使用var rowid? Something like that: 像这样:

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

If i understand well this test your doing... 如果我理解得很好,请测试您的操作...
Your JavaScript rowid is the value. 您的JavaScript rowid是值。 The $_POST identifier is id . $_POST标识符是id

Try this in your PHP: 在您的PHP中尝试一下:

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

You'll get the javascript rowid sent as a POST value (named as $_POST['id'] ) via ajax... 您将通过ajax获取以POST值发送的javascript rowid (名为$_POST['id'] )...
And returning in data on ajax success. 并返回有关ajax成功的data

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

So you'll have to use data in your jQuery html() ... Wich is the echoed text. 因此,您将不得不在jQuery html()使用data 。Wich是回显的文本。



----- -----
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: 所以我想在此查询中使用$ gameid变量:

 $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... 不需要ajax调用...
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. 请注意 ,我建议使用此脚本来启发您了解while fetch loop可能结果。 You'll have to work a little to have it displayed correctly on your page. 您需要做一些工作才能使其正确显示在页面上。
;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM