如何使用PHP检查HTML表单是否为空

3
我想知道是否有一种方法可以检查HTML文本框是否为空,如果为空,则执行特定的PHP代码(我会提供)。
HTML
<input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="<?php echo $name; ?>">

<input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="<?php echo $email; ?>">

<button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>

PHP

if(!isset(['name']) || !isset(['email']) == 0){
        $msg_to_user = '<h1>Fill out the forms</h1>';
}

我想知道以下几点:
  1. 我的PHP代码是否正确,我认为它是有问题的。
  2. 是否可以检查输入是否为空,如果为空,则执行PHP代码(具体来说是'$msg_to_user')?
  3. 是否可以调整我的代码,当用户单击提交按钮并且字段都为空时,让$msg_to_user代码执行?
谢谢您提前回答!

您的值将在$_POST或$_GET超全局变量中,具体取决于您的表单方法,即method="post",因此if(!isset(['name']) || !isset(['email']) == 0)应该看起来像if(!isset($_POST['name']) || !isset($_POST['email']))。您也可以使用empty()代替isset(),它既检查isset(),又检查值是否为空。 - Sean
1
@Sean 减去 == 0 - Sverri M. Olsen
2个回答

2

根据您的HTML表单方法,请在数组$_POST$_GET上使用isset检查。

应该是这样的:

<form method="POST">
    <input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="">
    <input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="">
    <button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>
</form>

PHP:

if (!isset($_POST['name']) || !isset($_POST['email']))
{
    $msg_to_user = '<h1>Fill out the forms</h1>';
} else {
    // process your results
}

我猜,我已经回答了问题A和B。那么问题C呢 - 这取决于你的架构。例如,如果你想要将表单从页面提交到自身(我指index.php文件有一个带有action='index.php'或空action的表单),那么你可以这样做(只是一个示例方法,绝不是最好的):

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if (!isset($_POST['name']) || !isset($_POST['email']))
    {
        $msg_to_user = '<h1>Fill out the forms</h1>';
    } else {
        // do something in database
        if ($doSomethingInDatabaseSuccess)
            $msg_to_user = '<h1>Succesffully saved!</h1>'; // really? h1? :)
        else
            $msg_to_user = '<h1>Something went wrong!</h1>';
} else {
    $msg_to_user = ''; 
}

<form method="POST">
    <input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="">
    <input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="">
    <button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>
</form>
<p class="result"><?php echo($msg_to_user); ?></p>

我有一个问题:action="(某个URL)"会影响这段代码的结果吗?我已经在我的服务器上设置了重定向用户到不同页面并根据字段数据的完整性显示$msg_to_user。例如,如果他们只是没有填写名称字段,它将告诉他们在下一页上这样做,但是代码在您提出的最后建议上不执行。除了完全空的字段数据之外,其他所有可能的结果都有效。 - user3761911
@user3761911 如果你想在PHP中处理这个表单,你的表单应该有一个指向这个PHP文件的操作。 - Yeldar Kurmangaliyev

2

A. 不,你的语法有误。

B. 是的。

C. 是的,首先你需要给你的按钮一个名字属性,这样它才能被 PHP 捕捉到。你还需要使用 $_POST 变量来赋值 $email 和 $name 变量。

HTML

<input type="text" name="name" class="form-control" id="exampleInputName2" placeholder="your name" maxlength="36" value="<?php echo $name; ?>">
<input type="email" style="margin-top: 10px;" name="email"  class="form-control" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="<?php echo $email; ?>">
<input type="submit" name="mySubmitBtn" class="btn btn-default btn-md" value="subscribe">

PHP

//check to see if the submit button is pressed
if(isset($_POST['mySubmitBtn'])){
//assign the variables from the content of the input form
$email = $_POST['email'];
$name = $_POST['name'];
  //check if name is empty
  if(!isset($name) || trim($name) ==''){
  $errorMsg = "You must enter a name";
  return;
  }
  //check if email is empty
  if(!isset($email) || trim($email) ==''){
  $errorMsg = "You must enter an email";
  return;
  }
  //do something else

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接