2016-09-16 29 views
-3

中提交數據從表格中隨機值會發生變化。我需要計算沒有刷新頁面的隨機值在php

$x=rand('1000','9999'); 
echo $x; 

<form method="post"> 
<input type="number" name="rnd"> 
<input type="submit" value="Submit"> 
</form> 

$a = $_POST['rnd']; 
if($a === $x){ 
echo "Match"; 
} 
+0

???這看起來像作業/作業......但至少提供了一個更好的問題。 – mroman

回答

0

我認爲你正在尋找像下面這樣的東西。

<?php 
    $x=rand('1000','9999'); 
    echo $x; 
?> 
<script type="text/javascript"> 
    var rand_value = "<?php echo $x?>"; 
</script> 
<form method="post" onsubmit="return validateForm()" > 
<input type="number" name="rnd" id="rnd"> 
<input type="submit" value="Submit"> 
</form> 
<script type="text/javascript"> 
    function validateForm(){ 
     x = document.getElementById("rnd").value; 
     if(rand_value == x){ 
      alert("Match"); 
      return true; 
     }else{ 
      alert("Not-Match"); 
      return false; 
     } 
    } 
</script> 

如果你想在客戶端進行驗證,你應該使用JavaScript

別的嘗試這個

<?php 
    $x=rand('1000','9999'); 
    echo $x; 
?> 

<form method="post"> 
<input type="hidden" name="myRnd" value="<?php echo $x; ?>"> 
<input type="number" name="rnd"> 
<input type="submit" value="Submit"> 
</form> 
<?php 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     $x = $_POST['myRnd']; 
     $a = $_POST['rnd']; 
     if($a === $x){ 
      echo "Match"; 
     } 
    } 
?>