如果发生错误,保留字段值

时间:2011-12-29 09:09:41

标签: php forms validation

我已经看到了一些针对我的问题的解决方案,但我只是不知道如何应用它们。我要发布部分验证文件,因为它很大。

if ($action == "submit" && ($member_submit == 0 || ($member_submit==1
   && $_SESSION['loggedin']==1))){

                   $frompage = $_SERVER['HTTP_REFERER'];

                   if($_POST['thumburl']=="http://")
                   $_POST['thumburl']="";

                   // Check to see if the user is trying to bypass your requirements, and if so, redirect them!
                   if ($_SESSION['nosubmit']==1){
                     $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> There was something wrong with your
   submission. Please try again later</div>";
                       header('Location: '.$frompage.'');
                       exit;        

                   }
                   // End Cheat Check

                   // Check to see if IP address is allowed to submit. If not, redirect!
                   if (ban_check("submit") == "banned"){
                     $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Cannot Add Submission At This Time</div>";
                       header('Location: '.$frompage.'');
                       exit;        

                   }
                   // End Ban Check

                   $submissiontime = time();
                   if (($submissiontime - $delay) <= $_SESSION['submission']){
                    $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Flood Control Initiated</div>";
                       header('Location: '.$frompage.'');
                       exit; 
                   }
       $ipaddress = $_SERVER['REMOTE_ADDR']; 
               $contenttitle = clean_string($_POST['contenttitle']);
               $contentdescription = clean_string($_POST['contentdescription']);
               $contenturl = clean_string($_POST['contenturl']);
               $contenturl2 = strtolower($contenturl);
               $category = clean_string($_POST['category']);

                 // Make sure they selected a category
                if ($category == 0){
                $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Please select a category</div>";
                   header('Location: '.$frompage.'');
                   exit; 
               }

       // Check to see if have backlink and nofollow atribute
       $parse = parse_url($contenturl);
       $base_url = $parse["host"]; // domain.com

       $linkback1 = reciprocal_linkback($contenturl, "http://www.dumpvid.com", 1);

       if ("$linkback1"=="0") {


       $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Backlink was not found, or nofollow detected.</div> ";
       header('Location: '.$frompage.'');
        exit; 

       }

       // Check to see if have backlink in the main also
       $parse = parse_url($contenturl);
       $base_url = $parse["host"]; // domain.com

       $linkback2 = reciprocal_linkback($base_url, "http://www.dumpvid.com", 1);

       if ("$linkback2"=="0") {


       $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Backlink found only on content url.</div> ";
       header('Location: '.$frompage.'');
        exit; 

       }

               // Check if TITLE and URL are filled in
               if (empty($contenttitle) || $contenttitle == "Title?"){
                $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Please Fill In Title</div>";
                   header('Location: '.$frompage.'');
                   exit; 
               }

               elseif (empty($contenturl) || $contenttitle == "http://"){
                $_SESSION['submitstatus'] = "<div class=error><b>Error:</b>Invalid URL</div>";
                   header('Location: '.$frompage.'');
                   exit; 
               }
               elseif (empty($contentdescription) || $contentdescription == "Nice description gets more traffic..."){
                $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Invalid or Missing Descriptio</div>";
                   header('Location: '.$frompage.'');
                   exit; 
               }


               // Check if VALID URL
               if (is_url("$contenturl")) {     
               } else { 
                   $_SESSION['submitstatus'] = "<div class=error><b>Error:</b> Doesn't seem to be a valid URL</div>";
                   header('Location: '.$frompage.'');
                   exit; 
               }

验证文件的结构基本上就是这个,如果你需要我发布整个验证文件和表格,请告诉我。

我只想保留用户填写的字段值,以防验证失败。

3 个答案:

答案 0 :(得分:1)

您应该在出错时再次显示表单但不使用header('Location:...')因为您将丢失重定向后使用表单发送的params。您可以使用函数在html中检索params:

function getParam($name, $defaultVal = null){
    return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $defaultVal;
}


<input type="text" value="<?php echo getParam('firstname', ''); ?>" name="firstname">

修改

如果您有2个文件: form.php - 您的表单定义 save_form.php - 表单验证,

然后在save_form.php你可以使用这样的东西:

//...
if($error){
    include 'form.php';
    die();
}

并在form.php中

<input type="text" value="<?php echo getParam('firstname', ''); ?>" name="firstname">

答案 1 :(得分:0)

据我所知,我认为你必须在html中这样做:

<input type="text" value="<?php echo (isset($_REQUEST['firstname'])) ? $_REQUEST['firstname'] : ''; ?>" name="firstname">

这将返回你想要的东西..将这样的东西应用到所有领域..​​

编辑:当你点击提交按钮然后你将获得'firstname'值,否则它将显示为空白以上将工作..并且请永远不要忘记验证上面的<form>标记表示页面顶部..

答案 2 :(得分:0)

<?php
session_start();

//save post values into session
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
    $_SESSION['_postHistory'] = $_POST;
}


//this will restore your previously posted values into $_POST global
if(isset($_SESSION['_postHistory']) && is_array($_SESSION['_postHistory'])){
   foreach($_SESSION['_postHistory'] as $key => $val){
      if(!isset($_POST[$key])) $_POST[$key] = $val;
   }
}

function getPostValue($key, $default = null){
   return (isset($_POST[$key])) ? $_POST[$key] : $default;
}
?>

<input type="text" value="<?php echo getPostValue('fieldname')?>" name="fieldname"/>

我不建议使用$ _REQUEST作为帖子,因为你可能在$ _GET中定义了相同的param,这可能会相互冲突。