du8589840 2015-06-29 21:11
浏览 77
已采纳

如何将php函数的输出添加到列表中

So i am trying to write code to do the following very basic thing:

*Take a number from the html input-form

*Take a counter, going from 0 to the input-number in a loop

*get the number as a list-item in the php-output, except if it is divisible by 3 or 5, which get ping and pong respectively as an output-value.

At the moment, this sadly does not work and all i get displayed is the tite & an error-message. Any and all help is very much appreciated!


Here the code from the html input-page:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <title>PHP Ping Pong Purified Perfection!</title>
</head>
<body>
    <div class="container">
        <h1>Geimme a number between 0 and 100 or you get a paddlin´!</h1>
        <form action="phpPingPong.php" method="post">
            <div class="form-group">
                <label for="inputNumber">The Number:</label>
                <input id="inputNumber" name="inputNumber" class="form-control" type="number">
            </div>
            <button type="submit" class="btn">Gimme the darn result already!</button>
        </form>
    </div>
</body>
</html>

Here´s the code from the php-page:

<?php 
    $inputNumber = $_GET["inputNumber"];
    $counter = 0;
    $outputMessage = "";

    function colorFate($inputNumber, $outputMessage)
    {
        if ($inputNumber < 100){

            for ( $counter == 1; $counter <= $inputNumber; $counter ++) {

                if ($counter % 5 === 0 && $counter % 3 === 0){
                    $outputMessage = "Ping-Pong";
                }

                else if ($counter % 3 === 0){
                    $outputMessage = "Ping";
                }

                else if ($counter % 5 === 0){
                    $outputMessage = "Pong";
                }

                else if ($counter % 5 != 0){
                    $outputMessage = $counter;
                }

                $outputMessage = "<li>"+ $outputMessage +"</li>";

                "#outputList".append ($outputMessage) ;


            }   
        }
        else{
            alert("Please follow the guidelines on what to enter!");
        }
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title> PHPingPong </title>
</head>
<body>
    <h1>Read and Weep!</h1>
    <ul id="outputList">
    </ul>
</body>
</html>

Thanks again!

  • 写回答

1条回答 默认 最新

  • dougaxing8673 2015-06-29 21:16
    关注

    The First problem:- form method is POST and you use $_GET to retrieve the value in second page. change code like below:-

    $inputNumber = $_GET["inputNumber"]; to $inputNumber = $_POST["inputNumber"];

    Second one is :- $outputMessage = "<li>"+ $outputMessage +"</li>"; must be $outputMessage = "<li>".$outputMessage."</li>";

    Third is :- "#outputList".append ($outputMessage) ; it seems that you are mixing javascript code to php code. Don't do that.

    So your second page code must be like below:-

          <?php 
        $inputNumber = $_POST["inputNumber"];
    
    
        function colorFate($inputNumber)
        {
    
            $new_array = array();
            $outputMessage = "";
            if ($inputNumber < 100){
    
                for ( $counter = 1; $counter <= $inputNumber; $counter++) {
    
                    if ($counter % 5 === 0 && $counter % 3 === 0){
                        $new_array['Ping-Pong'][$counter] = $counter;
                    }else if ($counter % 3 === 0){
                        $new_array['Ping'][$counter] = $counter;
                    }else if ($counter % 5 === 0){
                        $new_array['Pong'][$counter] = $counter;
                    }else if ($counter % 5 != 0){
                        $new_array['nothing'][$counter] = $counter;
                    }
                }
                return $new_array;  
            }
            else{
                alert("Please follow the guidelines on what to enter!");
            }
        }
    
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title> PHPingPong </title>
    </head>
    <body>
        <h1>Read and Weep!</h1>
        <?php $result = colorFate($inputNumber);?>
        <?php foreach($result as $key=>$value){
            echo "<ul><span>$key numbers are:-".implode(',',$value)."</ul>";
        }?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类