通过php验证html文本字段

通过php验证html文本字段,php,Php,我有html表单,我想验证所有字段,直到用户插入正确的详细信息。我的当前代码未验证表单 我的代码: 前 公司* 地址* 网站(URL) 关键联系人* 电子邮件 我只想要带有电子邮件验证的字符串值,但不幸的是它不起作用 php验证: if(isset($_POST['register'])) { if($company == "" && $address =="" && $url =="" && $contact ==""

我有html表单,我想验证所有字段,直到用户插入正确的详细信息。我的当前代码未验证表单

我的代码:


公司*
地址*
网站(URL)
关键联系人*
电子邮件
我只想要带有电子邮件验证的字符串值,但不幸的是它不起作用

php验证

if(isset($_POST['register']))
    {

       if($company == "" && $address =="" && $url =="" && $contact =="" && $email =="" && $requirements=="")
         {

            $alert = '<script> bootbox.alert("Please fill the form Correctly.")</script>';
         }
         else
         {
              if($company !=="" || $address !=="")
              {

                $alert = '<script> bootbox.alert("Please fill the form Correctly.")</script>';


              }


         }

        $company = $_POST['company']."\n";
        $address = $_POST['address']."\n";
        $url     = $_POST['url']."\n";
        $contact = $_POST['contact']."\n";
        $email= $_POST['email']."\n";

        $msg =  'Company Name: '.$company;
        $msg.=  'Address: '.$address;
        $msg.=  'URL: '.$address;
        $msg.=  'Contact: '.$contact;
        $msg.=  'Email: '.$email;

            }
?>
if(isset($\u POST['register']))
{
如果($company==“”&&$address==“”&&&$url==“”&&&$contact==“”&&&$email==“”&&&$requirements==“”)
{
$alert='bootbox.alert(“请正确填写表单”);
}
其他的
{
如果($company!==“”| |$address!==“”)
{
$alert='bootbox.alert(“请正确填写表单”);
}
}
$company=$\发布['company']。“\n”;
$address=$\u POST['address']。“\n”;
$url=$\u POST['url']。“\n”;
$contact=$\u POST['contact']。“\n”;
$email=$\u POST['email']。“\n”;
$msg=‘公司名称:’。$Company;
$msg.='地址:'.$Address;
$msg.=“URL:”.$address;
$msg.=“联系人:”.$Contact;
$msg.=“电子邮件:”.$Email;
}
?>

根据您编写的代码,我没有看到表单字段“register”,这意味着验证无法工作

我不知道登记册应该做什么,但是

<input type=hidden name=register value=1>


这可能需要添加到表单中

首先,您正在尝试验证$u POST['register'],它不是HTML代码的一部分。你可以给你的表格起个名字 . 我认为最好首先在客户端用Javascript(最好是JQuery)验证代码

检查是否设置了$_POST['email']等字段,然后查看内容是否为空

您必须告诉您的表单将处理表单的脚本

如果php代码位于单独的文件中

<form action="my_validator.php" method="post"> 

否则,

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

要检查值是否为字符串,可以使用
php
is_string(value)
函数,该函数返回
TRUE
False

你可以了解更多 您还可以在此处学习如何使用
php
验证用户输入:

您只需验证表单中的电子邮件,就是这样吗?@nuke_推断您检查地址等的第一个条件都是正确的。在检查变量之前,必须先定义变量。i、 e.
$company=$_POST['company']
,如果它们被定义,你的条件很可能总是错误的,因为你使用了
(你应该使用
,即
|
)。这个按钮在哪里?我做了所有可能的事情
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">