簡體   English   中英

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

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

我有一個html頁面(edituserdetails.html),其中有一個包含3個文本框的表單:名字,姓氏,電子郵件和提交按鈕。 單擊提交按鈕后,我正在驗證這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']);

因此,即使在文本框為空的情況下,單擊“提交”按鈕,頁面也會刷新並填充數據庫中的文本框。 結果,我的驗證無法正常工作。 請幫忙。 下面是我的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子句均已通過。

例:

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

檢查我下面的方法。 用false初始化一個標志,在每個失敗的驗證中將其設置為true。

在選擇查詢之前,僅​​在沒有驗證錯誤時才添加條件。

<?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