多次显示字符串

时间:2012-10-18 20:11:09

标签: php

我有一个填充数字的选择框,我希望当我选择一个数字例如5来打印一个字符串5次。

这是我到目前为止尝试的代码:

<!DOCTYPE HTML>
<html>
<head>
    <style type="text/css">
    .centrer
    {
        position: absolute;
        left: 50%;
        top: 50%;
        font-size:24px;
    }
    </style>

</head>
<body>
    <select name="nombres">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>

   <div class="centrer">
    <?php
        $s = "Hello World! <br/>";
        for($i=0; $i<3; $i++)
            echo $s;
    ?>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

请试试这个:

我们可以使用javascript轻松完成此操作。 但是你需要在php中打印你的字符串。

使用javascript时我们不需要加载页面。 但是在PHP中我们需要在每次选择选择框时加载页面。 因为PHP是服务器端。 Javascript是客户端。

欢呼......

<!DOCTYPE HTML>
<html>
<head>
<script>
function checkIt()
{
    var getNum = document.getElementById("numb").value;

    //this_file.php ... im specifying for just. you specify this full code in any of file and specify the whole url path
    location.href="this_file.php?countIt="+getNum;
}
</script>
    <style type="text/css">
    .centrer
    {
        position: absolute;
        left: 50%;
        top: 50%;
        font-size:24px;
    }
    </style>

</head>
<body>
    <select name="nombres" id="numb" onchange="checkIt();">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>

<?php
if($_REQUEST["countIt"])
{
    $displayTimes = $_REQUEST["countIt"]; 
}
?>
   <div class="centrer">
    <?php
        $s = "Hello World! <br/>";
        for($i=0; $i<$displayTimes; $i++)
            echo $s;
    ?>
    </div>
</body>
</html>

答案 1 :(得分:0)

您必须从表单发回服务器的值。从那里,您可以根据需要渲染页面。

相关问题