PHP - 在提交按钮上停止提交空表单

PHP - Stop empty form submission on submit button

我有一个 html 页面 (edituserdetails.html),其中有一个包含 3 个文本框的表单:FirstName、LastName、Email 和一个提交按钮。单击提交按钮后,我将验证这 3 个文本框(在函数“SaveEdit”内)。 下面是我的代码:

if ($_POST['SaveUserDetails']) {
        $Id = $args[2];
        if(empty($_POST['FirstName']))
        {
            $this->smarty->assign('FirstNameEmpty',"Please enter your firstname!");
            controller::display($this, 'tpl.edituserdetails.html');
        }
        if(empty($_POST['LastName']))
        {
            $this->smarty->assign('LastNameEmpty',"Please enter your lastname!");
            controller::display($this, 'tpl.edituserdetails.html');
        }
        if(empty($_POST['Email']))
        {
    $this->smarty->assign('EmailEmpty',"Email field should not be empty!");
   controller::display($this, 'tpl.edituserdetails.html');
        }`

现在,问题是同一个函数用数据库中的内容加载文本框(此代码也在 SaveEdit 中):

$x = $args[2];
    $x = mysql_real_escape_string($x);
    $result = db::sql("Select contact_fname,contact_lname,contact_email from billing_buyer where id = '$x'");
    $row = mysql_fetch_assoc($result);
    $this->smarty->assign('FirstName', $row['contact_fname']);
    $this->smarty->assign('LastName', $row['contact_lname']);
    $this->smarty->assign('Email', $row['contact_email']);

因此,当我单击 'Submit' 按钮时,即使文本框为空,页面也会刷新并从数据库中填充文本框。结果,我的验证工作不正常。请帮忙。 下面是我的 html(我使用了 Smarty 变量 $FirstName、$LastName 和 $Email 来显示相应的数据。$FirstNameEmpty、$LastNameEmpty 等是包含用于验证的错误消息的 smarty 变量。

<html>  
<div> 
    <form action="" method="post" id="subscriptions" onsubmit="">
<input type="hidden" value="form" name="action">

<div class="form">

<h2><span>Edit User Details</span></h2>
    <div>

<table id="im_list" class="primary_table" border="0" cellpadding="10px" cellspacing=0>

        <tr>
            <td>First Name</td>
            <td class="bar_style"> <input type="text" name="FirstName" value="{$FirstName}"></td>
            <td><font color="red">{$FirstNameEmpty}</font></td>
        </tr>
        <tr>
            <td>Last Name</td>
            <td class="bar_style"> <input type="text" name="LastName" value="{$LastName}"></td>
           <td><font color="red">{$LastNameEmpty}</font></td>
        </tr>
        <tr>
            <td>Contact email</td>
            <td class="bar_style"><input type="text" name="Email" value="{$Email}"></td>
            <td><font color="red">{$EmailEmpty}</font></td>
            <td><font color="red">{$WrongEmailFormat}</font></td>
        </tr>
        <tr>
            <td>
         <input id="SaveUserDetails" name="SaveUserDetails" type="submit" value="Save Preferences">
            </td>
        </tr>
</table>
    </div>
</div>
</form>
</div>

检查我下面的方法。用 false 初始化一个标志,在每个失败的验证中将其设为 true。

在您的 select 查询之前添加一个条件,仅当没有验证错误时。

<?php

  function yourFunctionName(){

    if ($_POST['SaveUserDetails']) {
            $Id = $args[2];
            $error = false;
            if(empty($_POST['FirstName']))
            {
                $this->smarty->assign('FirstNameEmpty',"Please enter your firstname!");
                controller::display($this, 'tpl.edituserdetails.html');
                $error = true;
            }
            if(empty($_POST['LastName']))
            {
                $this->smarty->assign('LastNameEmpty',"Please enter your lastname!");
                controller::display($this, 'tpl.edituserdetails.html');
                $error = true;
            }
            if(empty($_POST['Email']))
            {
        $this->smarty->assign('EmailEmpty',"Email field should not be empty!");
       controller::display($this, 'tpl.edituserdetails.html');
              $error = true;
           }
          if(!$error){
             $x = $args[2];
             $x = mysql_real_escape_string($x);
             $result = db::sql("Select contact_fname,contact_lname,contact_email from billing_buyer where id = '$x'");
             $row = mysql_fetch_assoc($result);
             $this->smarty->assign('FirstName', $row['contact_fname']);
             $this->smarty->assign('LastName', $row['contact_lname']);
             $this->smarty->assign('Email', $row['contact_email']);
             }
        }
}

如果我理解正确的话,你的填充有问题。仅当我成功提交表单时,才尝试从数据库中执行 select。 顺便说一下,在 select.

之前进行更新

提交成功=所有if条款通过。

示例:

if(!empty($_POST['FirstName']) && !empty($_POST['LastName']) && !empty($_POST['Email']) ){

// update 


// And select
$x = $args[2];
    $x = mysql_real_escape_string($x);
    $result = db::sql("Select contact_fname,contact_lname,contact_email from billing_buyer where id = '$x'");
    $row = mysql_fetch_assoc($result);
    $this->smarty->assign('FirstName', $row['contact_fname']);
    $this->smarty->assign('LastName', $row['contact_lname']);
    $this->smarty->assign('Email', $row['contact_email']);
}