简体   繁体   English

PHP-停止在“提交”按钮上提交空白表格

[英]PHP - Stop empty form submission on submit button

I have an html page(edituserdetails.html), where I have a form containing 3 textboxes: FirstName, LastName,Email and a submit button. 我有一个html页面(edituserdetails.html),其中有一个包含3个文本框的表单:名字,姓氏,电子邮件和提交按钮。 On clicking submit button, I'm validating these 3 textboxes(inside a function " SaveEdit "). 单击提交按钮后,我正在验证这3个文本框(在“ SaveEdit ”函数中)。 Below is my code: 下面是我的代码:

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');
        }`

Now, the problem is that the same function loads the textboxes with contents from the database(This code is also inside SaveEdit ): 现在,问题是该函数从数据库中加载带有内容的文本框(此代码也位于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']);

Due to this, when I click 'Submit' button even with empty textboxes, page refreshes and fills textboxes from database. 因此,即使在文本框为空的情况下,单击“提交”按钮,页面也会刷新并填充数据库中的文本框。 As a result, my validations aren't working properly. 结果,我的验证无法正常工作。 Please help. 请帮忙。 Below is my html(I have used Smarty variable $FirstName,$LastName and $Email for dislaying the respective data. $FirstNameEmpty, $LastNameEmpty etc. are the smarty variables containing error messages for validation. 下面是我的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>

If I understand correctly you have problem with fills. 如果我理解正确,您可能会遇到填充问题。 Try to do your select from DB only if form i success submited. 仅当成功提交表单后,才尝试从数据库中进行选择。 By the way, do update before select. 顺便说一句,请在选择之前进行更新。

success submited = all if clauses are passed. 成功提交=所有if子句均已通过。

Example: 例:

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']);
}

Check my below method. 检查我下面的方法。 Initialize a flag with false, Inside each failed validation make it true. 用false初始化一个标志,在每个失败的验证中将其设置为true。

Before your select query add a condition to only if there is no validation error. 在选择查询之前,仅​​在没有验证错误时才添加条件。

<?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']);
             }
        }
}

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

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