繁体   English   中英

当按下HTML按钮时,如何显示从PHP函数返回的数据

[英]How can I display data returned from a PHP function when a HTML button is pressed

我想在我的页面中显示从PHP函数生成的固定装置,但是仅当我按下“ Generate Fixtures”按钮时才显示。 我知道应该使用AJAX,但是我无法将数据从函数传递到HTML页面中的div。

PHP代码

 $teams = array("FC FCSB", "AFC Astra Giurgiu", "FC Dinamo 1948", "FC Viitorul 2009", "CSM Politehnica Iasi", "Universitatea Craiova", "FC Botosani", "CFR 1907 Cluj");
    $fixPair = new Fixture($teams);
    $schedule = $fixPair->getSchedule();
    $i = 1;
    foreach ($schedule as $rounds) {
        echo "<h5> Etapa " . $i . " </h5>";
        foreach ($rounds as $game) {
            echo "{$game[0]} vs {$game[1]}<br>";
        }
        echo "<br>";
        $i++;
    }
    echo "<hr>";

 $(document).ready(function(){ $('.button').click(function(){ var clickBtnValue = $(this).val(); var ajaxurl = 'ajax.php', data = {'action': clickBtnValue}; $.post(ajaxurl, data, function (response) { // Response div goes here. document.getElementById("demo").innerHTML = "Fixtures should be displayed here"; //alert("action performed successfully"); }); }); }); 
 <script src="https://code.jquery.com/jquery-3.4.1.min.js"> </script> <input type="submit" class="button" name="insert" value="GENERATE FIXTURES" /> <div id="demo"></div> 

将事件处理程序更新为:

$('.button').click(function(e){
    // Prevent form submit 
    e.preventDefault();

    var clickBtnValue = $(this).val();
    var ajaxurl = 'ajax.php',
    data =  {'action': clickBtnValue};
    $.post(ajaxurl, data, function (response) {
        // response variable stores all data received from ajax.php script

        // As you use jquery - you can select 
        // element by id with $("#id") selector
        $("#demo").html(response);
    });
});

暂无
暂无

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

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