【问题标题】:Incomplete form still submitting in PHP不完整的表单仍在 PHP 中提交
【发布时间】:2016-03-30 05:38:50
【问题描述】:

当我填写表单中的一两个字段并单击注册按钮时,它仍会提交这些值。在表格中的所有字段完成之前,它不应提交。我究竟做错了什么???

我在顶部添加了一个<pre /> 标签,以便我可以检查输入了哪些值,这是我的代码:

文件开头 - PHP:

<?php

$error_array = array();
$fname = $lname = $email = $dob = $Mchecked = $gender = $Fchecked = $hobbies ="";

if(isset($_POST["sbt_save"]))
{   
echo '<pre>'; print_r($_POST); echo "</pre>";
    if($_POST['fname']=="")
    {           
        $err ="Please Enter your first name"."<br>";
        array_push($error_array,$err);      
    }
    else
    {
        $fname = test_input($_POST['fname']);       
    }

    if($_POST['lname']=="")
    {
        $err ="Please Enter your last name"."<br>";
        array_push($error_array,$err);
        $submit_Ornot_submit = true;
    }
    else
    {
        $lname = test_input($_POST["lname"]);
    }



    if($_POST['email']=="")
    {       
        $err ="Please Enter your email"."<br>";
        array_push($error_array,$err);
    }
    else
    {
        $email_Value = $_POST['email']; 
        $position_Of_at  = strpos("$email_Value","@");      
        $position_Of_dot = strpos("$email_Value",".");

        if($position_Of_at == -1 || $position_Of_dot == -1 || ($position_Of_at + 2) >= $position_Of_dot )
        {
            $err = "Please enter valid Email"."<br>";
            array_push($error_array,$err);          
        }
        else
        {
           $email = test_input($_POST["email"]);
        }
    }

    if($_POST['dob']=="")
    {
        $err ="Please Enter your date of birth"."<br>";
        array_push($error_array,$err);
    }
    else
    {
        $dob = test_input($_POST["dob"]);
    }

    if(!isset($_POST["gender"]))
    {
        $err ="Please select gender"."<br>";
        array_push($error_array,$err);
    }
    else
    {
        $gender = $_POST["gender"];
        if ($gender == "Male")
        {
            $Mchecked = "checked";
        }
        else if ($gender == "Female")
        {
            $Fchecked = "checked";
        }
    }

    if(!isset($_POST['hobbies']))
    {
        $err ="Please Enter your hobbies"."<br>";
        array_push($error_array,$err);
    }
    else
    {
        $hobbies = test_input($_POST['hobbies']);
    }
}

function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}


?>

继续 - HTML & PHP 和 Javascript:(此代码是同一个文件)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ragistration Form</title>
<link rel="stylesheet" type="text/css" href="ragistration_form.css">

<script src="jquery-2.2.1.min.js"></script>
<script>
/*
$(document).ready(function(event)
{    
    $(".sbt_button").click(function(event)
    {        
        var error_arr = [];             
        var email_value = $("#email").val();                    
        var position_of_at  = email_value.indexOf('@');         
        var position_of_dot = email_value.lastIndexOf('.');



        if($("#fname").val() == null || $("#fname").val() == "")
        {
             var err = "First Name";
             error_arr.push(err);
        }

        if($("#lname").val() == null || $("#lname").val() == "")
        {
            var err = "Last Name ";
            error_arr.push(err);
        }
        if(position_of_at == -1 || position_of_dot == -1 || (position_of_at + 2) >= position_of_dot )
        {
            var err = "Email ";
            error_arr.push(err);
        }

        if($("#dob").val() == null || $("#dob").val() == "")
        {
            var err = "Date of Birth ";
            error_arr.push(err);
        }

        if(!$("input[type='radio']").is(":checked"))
        {
            var err = "Gender ";
            error_arr.push(err);
        }

        if(!$("input[type='checkbox']").is(":checked"))
        {
            var err = "Hobbies ";
            error_arr.push(err);
        }


        if(error_arr.length !=0)
        {
            event.preventDefault(); 
            alert(error_arr);   
        }           
    });
});

*/
</script>

</head>
<body>
<form class="form" name="myForm" action="" method="POST">
  <table>
    <tr>
      <p class="heading">Ragistration Form</p>
    </tr>
    <?php
if($error_array !="")
{   
    foreach($error_array as $value)
    {       
        echo "<tr style='color:red;'><td> ". $value. "</td></tr>";                  
    }
}

?>
    <tr>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td class="field_Name">First Name :<b style="color:red">*</b></td>
      <td><input type="text" name="fname" id="fname" class="inputfield_Name"  /></td>
    </tr>
    <tr>
      <td class="field_Name">Last Name :<b style="color:red">*</b></td>
      <td><input type="text" name="lname" id="lname" class="inputfield_Name"  /></td>
    </tr>
    <tr>
      <td class="field_Name">Email :<b style="color:red">*</b></td>
      <td><input type="text" name="email" id="email" class="inputfield_Name"   /></td>
    </tr>
    <tr>
      <td class="field_Name">Date of Birth :<b style="color:red">*</b></td>
      <td><input type="date" name="dob" id="dob"  class="inputfield_Name"   /></td>
    </tr>
    <tr>
      <td class="field_Name">Gender :<b style="color:red">*</b></td>

        <td><input type="radio" name="gender" value="Male"class="inputfield_Name" <?php echo $Mchecked;?>   />Male
        <input type="radio" name="gender" value="Female" <?php echo $Fchecked;?>    />
        Female</td>


    </tr>
    <tr>
      <td class="field_Name">About Yourself :</td>
      <td><textarea name="abt" class="inputfield_Name"$></textarea></td>
    </tr>
    <tr>
      <td class="field_Name">Hobbies :<b style="color:red">*</b></td>
      <td><input name="hobbies" value="Cricket"  type="checkbox" id="hobbies"  class="inputfield_Name"    />
        Cricket
        <input name="hobbies" value="Singing"   type="checkbox"   />
        Singing
        <input name="hobbies" value="Travling"   type="checkbox"  />
        Travling</td>
    <tr>
      <td></td>
      <td><input name="hobbies"  value="Writing"  type="checkbox"  class="inputfield_Name"  />
        Writing
        <input name="hobbies"  value="Teaching"  type="checkbox"   />
        Teaching
        <input name="hobbies"  value="Driving"  type="checkbox"   />
        Driving </td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" value="Ragister" name="sbt_save"  class="sbt_button"/></td>
        </td>
    </tr>
  </table>
</form>
</body>
</html>

【问题讨论】:

  • 我觉得你的代码还可以
  • PHP 永远不会阻止提交表单——它是服务器端的。您的 jQuery 代码看起来可能有这样做的希望,但目前已通过 /* */ 标记将其全部注释掉。
  • 尝试检查`$_POST['email']==null`条件,我也回答了这个问题,所以你可以检查它是否有效,@Shanky
  • 您可以使用 HTML5 验证,只需将 required 属性添加到必填字段;

标签: php jquery validation


【解决方案1】:

您的可用 HTML+Jquery 代码和其余 PHP 代码都可以。 PHP 不会阻止表单提交。您可以通过 HTMLrequired 字段)和使用 Jquery

阻止表单提交

$(document).ready(function(event)
{    
    $(".sbt_button").click(function(event)
    {        
        var error_arr = [];             
        var email_value = $("#email").val();                    
        var position_of_at  = email_value.indexOf('@');         
        var position_of_dot = email_value.lastIndexOf('.');



        if($("#fname").val() == null || $("#fname").val() == "")
        {
             var err = "First Name";
             error_arr.push(err);
        }

        if($("#lname").val() == null || $("#lname").val() == "")
        {
            var err = "Last Name ";
            error_arr.push(err);
        }
        if(position_of_at == -1 || position_of_dot == -1 || (position_of_at + 2) >= position_of_dot )
        {
            var err = "Email ";
            error_arr.push(err);
        }

        if($("#dob").val() == null || $("#dob").val() == "")
        {
            var err = "Date of Birth ";
            error_arr.push(err);
        }

        if(!$("input[type='radio']").is(":checked"))
        {
            var err = "Gender ";
            error_arr.push(err);
        }

        if(!$("input[type='checkbox']").is(":checked"))
        {
            var err = "Hobbies ";
            error_arr.push(err);
        }


        if(error_arr.length !=0)
        {
            event.preventDefault(); 
            alert(error_arr);   
        }           
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<form class="form" name="myForm" action="" method="POST">
  <table>
    <tr>
      <p class="heading">Ragistration Form</p>
    </tr>
    
    <tr>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td class="field_Name">First Name :<b style="color:red">*</b></td>
      <td><input type="text" name="fname" id="fname" class="inputfield_Name"  /></td>
    </tr>
    <tr>
      <td class="field_Name">Last Name :<b style="color:red">*</b></td>
      <td><input type="text" name="lname" id="lname" class="inputfield_Name"  /></td>
    </tr>
    <tr>
      <td class="field_Name">Email :<b style="color:red">*</b></td>
      <td><input type="text" name="email" id="email" class="inputfield_Name"   /></td>
    </tr>
    <tr>
      <td class="field_Name">Date of Birth :<b style="color:red">*</b></td>
      <td><input type="date" name="dob" id="dob"  class="inputfield_Name"   /></td>
    </tr>
    <tr>
      <td class="field_Name">Gender :<b style="color:red">*</b></td>

        <td><input type="radio" name="gender" value="Male"class="inputfield_Name"  />Male
        <input type="radio" name="gender" value="Female"    />
        Female</td>


    </tr>
    <tr>
      <td class="field_Name">About Yourself :</td>
      <td><textarea name="abt" class="inputfield_Name"$></textarea></td>
    </tr>
    <tr>
      <td class="field_Name">Hobbies :<b style="color:red">*</b></td>
      <td><input name="hobbies" value="Cricket"  type="checkbox" id="hobbies"  class="inputfield_Name"    />
        Cricket
        <input name="hobbies" value="Singing"   type="checkbox"   />
        Singing
        <input name="hobbies" value="Travling"   type="checkbox"  />
        Travling</td>
    <tr>
      <td></td>
      <td><input name="hobbies"  value="Writing"  type="checkbox"  class="inputfield_Name"  />
        Writing
        <input name="hobbies"  value="Teaching"  type="checkbox"   />
        Teaching
        <input name="hobbies"  value="Driving"  type="checkbox"   />
        Driving </td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" value="Ragister" name="sbt_save"  class="sbt_button"/></td>
        </td>
    </tr>
  </table>
</form>

【讨论】:

    【解决方案2】:

    您可以使用 PHP 直接管理它,而不是 jquery。

    请尝试检查 NULL 条件。这是代码。

    if($_POST['fname']=="" || $_POST['fname']==null)
        {           
            $err ="Please Enter your first name"."<br>";
            array_push($error_array,$err);      
        }
        else
        {
            $fname = test_input($_POST['fname']);       
        }
    

    或者,如果您希望使用 HTML 来完成,只需在输入类型中添加 required 属性即可。

    就像:

    <input type="text" name="fname" id="fname" required>
    

    【讨论】:

    • @shanky,如果它适合你,请检查答案。
    【解决方案3】:

    1.在检查提交按钮是否被点击之前检查是否有任何文本框为空

    if( isset($_POST['fname']) && 
        isset($_POST['lname']) &&
        isset($_POST['email']) &&
        isset($_POST['dob']) &&
        isset($_POST["gender"]) &&
        isset($_POST['hobbies'])
      )  {  
             //your php code here 
         } else {  echo "Please complete the form"; }                   
    

    2.使用Javascript表单验证

    <form class="form" name="myForm" action="" method="POST"  onsubmit="return validateForm();">
    
    <script>function validateForm() {  
        //your form validation code here 
    } </script>
    

    【讨论】:

      【解决方案4】:

      您需要检查表单值。所以做这样的事情。

      if(isset($_POST["sbt_save"]))
      {   
      

      输入这段代码

      if($_POST['dob']=="" || $_POST['email']=="" || $_POST['lname']=="" || $_POST['gender'] == ""]){
      
      $err ="Please Enter all form fields"."<br>";
              array_push($error_array,$err); }else{
      

      您的其余代码...

      还可以查看jquery validator plugin。某些浏览器(例如移动 safari)无法识别所需的标签,并会提交表单。

      【讨论】:

        【解决方案5】:

        要停止提交表单,您必须使用浏览器端验证,即使用 jquery 验证或简单的 javascript。

        或者另一种方法是使用 HTML 5 验证,即输入字段中的必需属性。

        这将停止您的表单提交,直到您的所有字段都填写并验证。

        使用服务器端验证后,检查是否有任何字段为空,然后返回并突出显示该特定字段。

        【讨论】:

        • 如何在 php 中提交表单后保留 textarea 的值,这是我的代码 - 关于你自己:
        • 行不通,您必须使用您的方法来获取值,即 post 或 get。
        • 在这里您将通过打印 $_POST['abt'] 来保留价值
        • 也有类似
        猜你喜欢
        • 2011-05-12
        • 1970-01-01
        • 1970-01-01
        • 2012-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多