2015-05-17 100 views
0

我有這個代碼,當提交時,它會從數組中生成隨機數。提交後禁用按鈕PHP

<?php 

    if(isset($_POST['roll'])) { 
     $randarray = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 
     $randselect = array_rand($randarray); 
     $nr = $randarray[$randselect]; 
     echo '<p class="btn btn-info"> Branch: '. $nr. '</p>'; 
    } 

?> 

<form action="#" method="post"> 
    <button type="submit" class="btn btn-default" name="roll">Roll Branch </button> 
</form> 

我想要做的是在表單提交後,按鈕將被禁用。有什麼想法嗎?

回答

2

只需添加disabled如果按下該按鈕,如

<button type="submit" class="btn btn-default" name="roll" <?php echo isset($_POST["roll"]) ? "disabled" : "";?>>Roll Branch </button> 
+0

清潔和完美。謝謝! – FewFlyBy

+0

@FewFlyBy不客氣。 – Rizier123

1

添加onclick="this.disabled='true';"的按鈕標籤

+0

它不會回聲出隨機數。 – FewFlyBy

+0

爲什麼?我建議用一點解釋來擴展你的答案。 – peterh

2

你可以這樣做:

<button type="submit" class="btn btn-default" name="roll" <?php echo isset($_POST['roll']) ? 'disabled="true"' : ''; ?> >Roll Branch </button>