Renaud is Not Bill Gates
Renaud is Not Bill Gates

Reputation: 2084

Display a string for a number of times

I have a Select box which is filled with numbers, I want when I select a number for example 5 to print a string 5 times.

This is the code I tried so far:

<!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>

Upvotes: 2

Views: 114

Answers (2)

suresh gopal
suresh gopal

Reputation: 3156

Please try this:

We can easily do this using javascript. But you need printing your string in php.

while using javascript we dont need to load a page. But in PHP we need to load a page each time selecting select box. Because PHP is server side. Javascript is client side.

cheers...

<!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>

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190941

You will have to send back the server the value from the form. From there, you can render the page as you see fit.

Upvotes: 0

Related Questions