简体   繁体   English

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

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

I want to display in my page the fixtures generated from the PHP function but only when I press the "Generate Fixtures" button. 我想在我的页面中显示从PHP函数生成的固定装置,但是仅当我按下“ Generate Fixtures”按钮时才显示。 I know that AJAX should be used but I can't pass the data from the function to the div in my HTML page. 我知道应该使用AJAX,但是我无法将数据从函数传递到HTML页面中的div。

PHP code 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> 

Update your event handler as: 将事件处理程序更新为:

$('.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.

相关问题 在HTML上按下按钮后,如何重新创建重新加载问题 - How can I recreate the reload issue when a button is pressed on HTML 按下按钮时如何模拟按下的不同按钮? - How can I simulate a different button pressed when a button is pressed? 按下按钮时,如何将数组作为表格显示到 HTML 页面? - How do I display an array to a HTML page as a table when a button is pressed? 当主容器上有data-dismiss =&#39;modal&#39;并且按下按钮以展开内部面板时,如何保持模式不关闭 - how can i keep a modal from closing when there is a data-dismiss='modal' on the main container and when a button is pressed to expand panels inside 在 HTML 代码中按下按钮时,如何触发 JavaScript function - How do I trigger a JavaScript function when a button is pressed in HTML code 如何使用 javascript 函数的返回来显示 HTML 按钮? - How can I display an HTML button using the return from a javascript function? 如何在异步 function 中从返回的 object 中提取数据? - How can I extract data from this returned object in an async function? 如何存储从异步 function 返回的数据 - How can I store the data returned from an async function 按下按钮时启动PHP功能 - Launch PHP function when button is pressed 按下按钮时,如何使用带有新数据的 django 视图更新 firebase 记录? - How can i update firebase record using django views with new data when a button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM