2016-03-29 38 views
0

在php文件中,我有這兩個文本位置來讓用戶鍵入2個數字,並且當第一個輸入大於第二個輸入時,我想提醒某些事情。應該使用gmp_cmp來做到這一點?真的很感激它。在php中,如何使第一個輸入值總是小於第二個輸入值?

<input type="text" name="min" placeholder="min" size="6" maxlength="2" required> 
    to<input type="text" name="max" placeholder="max" size="6" maxlength="2" required> 
+1

你有沒有編寫任何代碼來做到這一點了嗎? – Oisin

+0

不。我嘗試使用gmp_cmp,但它顯示錯誤。 – SaitoD

回答

0

我相信這是與jQuery檢查的最好方式,但是如果你需要用PHP:

<?php //Estrutural 
$min = $_POST[min]; 
$max = $_POST[max]; 
if($min > $max){ 
    echo "Min value must be smaller than max value"; 
}else{ 
    echo "do something"; 
} 
?> 

但最好的方法是jquery,這樣用戶不會知道表單提交之前有什麼錯誤。

0

你應該在php中捕捉輸入最小值和最大值以及IF條件的值來確定它。當您使用的數字,你在<input>示例使用一個類型=「數字」:

<form method="POST" action="file.php"> 
    <input type="number" name="min" placeholder="min" required> 
    <input type="number" name="max" placeholder="max" required> 
    <input type="submit" value="Check the size"> 
</form> 

<?php 

    //FILE.PHP 
    $min = $_POST['min']; 
    $max = $_POST['max']; 
    //IF MIN = 10 AND MAX = 5, ALERT TO USER 
    if ($min > $max ) { 
     echo 'You cant do that'; 
    } 

    else { 
     echo 'Correct!'; 
    } 

?> 
0

如果最大值小於最小值,點擊檢查按鈕比較兩個值和警報的jquery方法。它也清除了最大值並重新設置了它。

$('#checkValue').on('click',function(){ 
 
\t var min=parseInt($('[name=min]').val()); 
 
\t var max=parseInt($('[name=max]').val()); 
 

 
\t if(max < min) 
 
\t \t { \t alert('error: the maximum value cannot be less than the minimum value'); 
 
\t \t \t $('[name=max]').val('').focus() 
 
\t \t } 
 
\t })
<html> 
 
<head> 
 
</head> 
 
<body> 
 
\t <form> 
 
\t \t <input type="text" name="min" placeholder="min" size="6" maxlength="2" required> 
 
\t \t to <input type="text" name="max" placeholder="max" size="6" maxlength="2" required> 
 
\t \t <button type ="button" id="checkValue">Check Value</button> 
 
\t </form> 
 
\t <p id="result"></p> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
</body> 
 
</html>

相關問題