我如何追加到新行,jQuery单击结果

时间:2019-03-28 14:05:36

标签: php arrays ajax

我有一个函数,该函数现在仅在单击按钮时生成一个随机数。当前,它仅覆盖当前生成的数字。

<!DOCTYPE html>
<html>
<head>
    <title>Number Generator</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

</head>
<?php 
// $random_number_array = [];
    function generatedNumbers(){
        $random_number_array = range(0, 100);
        shuffle($random_number_array );
        $random_number_array = array_slice($random_number_array ,0,10);

        foreach ($random_number_array as $key => $value) {
           print_r($value);
        }

    }

    if (isset($_GET['run'])) {
    generatedNumbers();
  }
;
 ?>
<body>
    <a href='index.php?run=true'>Generate</a>
<p></p>
</body>
</html>

我想在新行上将数字作为数组生成,因此每次点击都会生成具有新数字集的新行

1 个答案:

答案 0 :(得分:1)

解决方案1:

如果您只想使用PHP来获得所需的结果,则可以遵循以下示例:

HTML:

<a href='index.php?run=true'>Generate</a>

PHP:

<?php 
session_start(); // need to use session_start to store array in session.

function generatedNumbers(){
    $random_number_array = range(0, 100);
    shuffle($random_number_array);
    $random_number_array = array_slice($random_number_array,0,10);
    foreach ($random_number_array as $key => $value) {
       $myArray[] = $value; // store values in array
    }

    if(isset($_SESSION['data']) && count($_SESSION['data']) > 0){
        $myArray = array_merge($_SESSION['data'],$myArray); // merge new and old array if exist in session.
    }

    $_SESSION['data'] = $myArray;    // reset the value in session.
    echo "<pre>";
    print_r($myArray);
}

if (isset($_GET['run'])) {
   generatedNumbers();
}
?>

一些说明:

PHP中,您可以使用session获得所需的输出,可以将所有值存储到数组中,然后可以将$_SESSION用于下一个结果,最后可以合并旧的array_merge()

将新数据

解决方案2:

您还可以通过使用jQuery / Ajax获得所需的结果:

前端

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<a href='javascript:void(0);' id="generate">Generate</a>

<div id="result"></div>

<script type="text/javascript">
    $(document).ready(function(){
        $("#generate").click(function(){
            $.ajax({
                url: "index2.php",
                type: "GET",
                data: "run=1",
                dataType: "html",  // Has to be false to be able to return response
                success: function(response) {
                    $("#result").html(response);
                }
            });  // JQUERY Native Ajax End
        });
    });
</script>

现在,您需要使用ajax来调用index2.php,您的index2.php具有以下代码:

index2.php:

<?php 
session_start(); // need to use session_start to store array in session.
function generatedNumbers(){
    $random_number_array = range(0, 100);
    shuffle($random_number_array);
    $random_number_array = array_slice($random_number_array,0,10);
    foreach ($random_number_array as $key => $value) {
       $myArray[] = $value; // store values in array
    }

    if(isset($_SESSION['data']) && count($_SESSION['data']) > 0){
        $myArray = array_merge($_SESSION['data'],$myArray); // merge new and old array if exist in session.
    }

    $_SESSION['data'] = $myArray;    // reset the value in session.
    echo "<pre>";
    print_r($_SESSION['data']);
}

if (isset($_GET['run'])) {
   generatedNumbers();
}
?>

旁注: index.php和index2.php必须位于同一目录中。