2012-10-18 127 views
2

我有一個選擇框,裏面充滿了數字,我想當我選擇一個數字時,例如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> 
+0

哪裏或者應該怎樣打印? – Alfabravo

+0

我希望它在同一個頁面的選擇框是 –

回答

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> 
+0

Okey謝謝,它的工作,但有一個ptoblrm,當我選擇「1」值它做任何事情,而且當我選擇其他值選擇的值在選擇框仍然值「1」,我有另一個問題是沒有任何其他方式做到這一點,而不使用JS只使用PHP? –

+0

使用Javascript將請求計數值用作選擇框中的選定值。而這個功能可能只是使用PHP。但是我們應該在選擇框中的每個新選擇中重新加載頁面。 –