简体   繁体   English

Echo无法在PHP中用于!is_numeric

[英]Echo not working in PHP for !is_numeric

I have a simple textbox in HTML which takes only integers, they have to be EXACTLY 6 characters. 我在HTML中有一个简单的文本框,其中仅包含整数,它们必须完全是6个字符。 If these conditions arent met, there should be appropriate messages given to the user and the value shouldnt be $_POST'ed. 如果不满足这些条件,则应该向用户提供适当的消息,并且该值不应为$ _POST'ed。

$idLength = strlen($_POST['customerid']);

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(empty($_POST['customerid']))
    {
        $idErr = " *Enter ID";
    }
else if(($idLength > 0) && ($idLength < 6))
       {
       {$idnErr = " ID must be exactly 6 Digits";}
       if((!is_numeric($_POST['customerid'])))
       {$idnumErr = " ID must only be numerical values";}
       }

else if ((is_numeric($_POST['customerid']))&&($idLength = 6)) 
    {
        $id = $_POST['customerid'];
    }
echo "$id";

So with that code, i check if the input box is empty, if it is, it displays '*Enter ID' next to the box. 因此,使用该代码,我检查输入框是否为空,如果为空,则在该框旁边显示“ * Enter ID”。 I check if the value is greater than 0 and less than 6 (maxlength = 6 in html), if it is, it says "ID must be exactly 6 Digits". 我检查该值是否大于0且小于6(html中的maxlength = 6),如果是,它说“ ID必须正好是6位数字”。 I also check if the characters are numbers, if they arent, it displays 'ID must only be numerical values'. 我还要检查字符是否为数字,如果没有,它会显示“ ID必须仅是数字值”。

If the user enters a vaild ID, only then is the id stored in the variable. 如果用户输入有效ID,则只有ID才存储在变量中。 For example:- 123456. All this seems to work fine. 例如:-123456。这一切似乎正常。

My problem occurs when i enter exactly 6 characters that arent meant to be taken such as, 12345a or ABCDE3. 当我输入正好要输入的6个字符(例如12345a或ABCDE3)时,就会出现我的问题。 The code doesnt store them into $id but it doesnt show an error message like its meant to, i was wondering what i am doing wrong. 代码没有将它们存储到$ id中,但是没有显示错误信息,这是我的意思,我想知道自己在做错什么。

I also want to save the input by the user in the textbox if they enter it incorrectly. 如果用户输入错误,我也希望将其保存在文本框中。 For example. 例如。 if the user enters '1234g' in customer id and click submit, i want it to be there in the text box when the page refreshes with the errors. 如果用户在客户ID中输入“ 1234g”,然后单击“提交”,那么我希望页面刷新时出现错误时,它会出现在文本框中。

Over here: 在这里:

($idLength = 6)

You are assigning the value 6 to the variable. 您正在6 分配给变量。 You should be doing: 您应该这样做:

($idLength == 6)

== is a comparison operator and checks whether's it's equal to 6. ==比较运算符 ,它检查是否等于 6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM