PHP复选框值在错误检查或发布期间未保存



" 029、032和033"的" newvar_"值在错误检查过程中未记住,也不会在提交校正表格时发布。

>

"名称"字段在错误检查和发布时都正确工作。

未选择所有复选框的表单默认值。

用户应该能够仅选择他们想要的表格。他们的选择应张贴到TXT文件及其名称。

验证库AM可以在以下网址找到:http://www.benjaminkeen.com/software/php_validation

事先感谢您的帮助。

    <?php                                                       
    // Eliminate server error notices except for parse errors                                                       
        error_reporting(E_PARSE);                                                   
    // Receiving variables                                                      
        $errors = array(); // set the errors array to empty, by default                                                 
        $fields = array(); // stores the field values                                                   
        if (isset($_POST['submit']))                                                    
        {                                                   
    // Import the validation library                                                        
        require("../include/validation.php");                                                   
        $rules = array(); // stores the validation rules                                                    
    // Rules specific to this form                                                      
        $rules[] = "required,name,Please enter Your Name.";                                                 
        $rules[] = "length<71,name,Your Name is too long.";                                                 
    // Check the user's entries for errors based upon above rules                                                       
        $errors = validate_fields($_POST, $rules);                                                  
    // If there were errors, re-populate the form fields with user entries                                                      
        if (!empty($errors))                                                    
        {                                                   
        $fields = $_POST;                                                       
      }                                                     
    // If there were no errors                                                      
        else                                                    
        {                                                   
    // Redirect to a "thank you" page URL shown below                                                       
        header("Location: thank-you.php");                                                      
            }                                               
        }                                                   
    // Count the errors. If none process the form data                                                      
        if (count($errors) == 0) {                                                  
    // Location of flat file database - absolute path                                                       
        $datafile_01 = '/home/domain/public_html/Forms.txt';                                                    
    // Strip-out the HTML tags                                                      
        $_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);                                                    
        $_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);                                                   
    //  Write successfully submitted form data to flat file database                                                    
        if ($_POST['submit']) {                                                 
            $file = fopen($datafile_01, "a");                                               
        if (!$file) {                                                   
            die("Can't write to database file");                                                
        }                                                   
    // Set timezone to match delivery location                                                      
        date_default_timezone_set('America/New_York');                                                  
        $date = date('Y m-d  g:i:s a');                                                 
    // Select and code the fields being posted                                                      
        $name = $_POST['name'];$name=($name);                                                   
    //FORMS & SUPPLIES                                                      
        // Standard Forms they requested                                                    
        // Change the checkbox 'on' to something else (defined by $Newvar)                                                  
        if($Form_F_029 == 'on') { $Newvar_029 = &$Form_F_029; $Newvar_029 = "X"; }                                                  
        if($Form_F_032 == 'on') { $Newvar_032 = &$Form_F_032; $Newvar_032 = "X"; }                                                  
        if($Form_F_033 == 'on') { $Newvar_033 = &$Form_F_033; $Newvar_033 = "X"; }                                                  
    // Format the fields for the flat file                                                      
        fwrite($file,                                                   
    "Name:t $namen
REQUESTED FORMS AND SUPPLIES:n
STANDARD FORMS ++++++++++++++++++++
Form F-029:t $Newvar_029
Form F-032:t $Newvar_032
Form F-033:t $Newvar_033n
=============================================================================== EOF =nn);"                                                        
            fclose($file);                                              
        }                                                   
    // Format and send email notice of successful submittal                                                     
        if ($_POST['submit']) {                                                 
            $sn_header = "From: Inquiries Databasen"                                               
              . "Reply-To: no_reply@domain.comn";                                              
            $sn_subject = "Ordering Supplies Request";                                              
            $sn_email_to = "someone@domain.com";                                                
            $sn_message = "Please check the database for a recent submission.nn"                                              
            . "This is a post-only message.nn"                                                
            . "Do not reply.n";                                                
            @mail($sn_email_to, $sn_subject ,$sn_message ,$sn_header ) ;                                                
        }                                                   
    }                                                       
    ?>                                                      
    <!DOCTYPE HTML>                                                     
    <html>                                                      
        <head>                                                  
            <title>Form Test</title>                                                
            <meta charset="utf-8" />                                                    
            <meta name="viewport"                                               
                content="width=device-width, initial-scale=1" />                                            
    <?php //This CSS needed only for forms to style Modal ?>                                                        
            <link rel="stylesheet"                                              
                href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"                                            
                media="screen" />                                                                                                   
        </head>                                                 
        <body>                                                  
    <!-- Wrapper -->                                                        
                <div id="wrapper" class="divided">                                          
                        <section class="wrapper style1 align-left">                                 
                            <div class="inner">                             
                                <h2 class="color-1">Measuring &amp; Ordering Supplies</h2>                          
                                                <form action="<?=$_SERVER['PHP_SELF']?>" name="inquiry" method="post">          
                                                    <div class="field">     
                                                        <label  style="margin:1rem 0 .25rem 0"><i class=" color-1 fa fa-star"></i> Your Name:</label>   
                                                        <input type="text" name="name" id="name" value="<?=$fields['name']?>" />    
                                                    </div>      
                                                    <div class="field" style="padding-top:1rem">        
                                                        <label style="font-size:110%; margin-bottom:0"><b class="color-1">Standard Forms</b> &nbsp; 
                                                            <span style="font-weight:300; font-size:80%">Check box to order | Click name to download PDF</span><br />
                                                        </label>    
                                                    </div>      
                                                        <div class="field third first" style="padding-top:10px">    
                                                            <input type="checkbox" id="Form_F_029" name="Form_F_029" <?php if($Form_F_029 == "on"){echo "CHECKED";}?> />
                                                            <label for="F_029"><a href="../../../forms_resources/order-forms/pdf/Form_F_029.pdf">Form F 029</a>
                                                            </label>
                                                        </div>  
                                                        <div class="field third" style="padding-top:10px">  
                                                            <input type="checkbox" id="Form_F_032" name="Form_F_032" <?php if($Form_F_032 == "on"){echo "CHECKED";}?> />
                                                            <label for="Form_F_032"><a href="../../../forms_resources/order-forms/pdf/Form_F_032.pdf">Form F 032</a>
                                                            </label>
                                                        </div>  
                                                        <div class="field third" style="padding-top:10px">  
                                                            <input type="checkbox" id="Form_F_033" name="Form_F_033" <?php if($Form_F_033 == "on"){echo "CHECKED";}?> />
                                                            <label for="Form_F_033"><a href="../../../forms_resources/order-forms/pdf/Form_F_033.pdf">Form F 033</a>
                                                            </label>
                                                        </div>  
                                                    <ul class="actions" style="padding-top:30px">       
                                                        <li><input type="submit" class="button small" name="submit" id="submit" value="Submit Request" /></li>  
                                                    </ul>       
                                                </form>         
                            </div>                              
                            <div id="error" title="Form Errors:">                               
                                <?php                           
                                // if $errors is not empty display them in the modal                            
                                if (!empty($errors))                            
                                    {                       
                                    echo "<div style="padding:15px 15px 0 15px">";                        
                                            echo "<ul style="margin-bottom:20px">";               
                                    foreach ($errors as $error)                     
                                        echo "<li style="font-size:15px; padding:5px">$error</li>";                   
                                        echo "</ul></div>";                 
                                    }                       
                                ?>                          
                            </div>                              
                        </section>                                  
    <!-- Footer -->                                                     
                </div>                                          
    <!-- Scripts -->                                                        
    <?php include $_SERVER["DOCUMENT_ROOT"] . "/assets/includes/php/javascripts-01.php"; ?>                                                     
    <?php // Supporting scripts for Error Modal ?>                                                      
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>                                                          
            <script>                                                
                 <?php if( isset( $_POST["submit"] ) ){ ?>                                              
                     $('#error').dialog({                                               
                         height: 380,                                               
                         width: 260,                                                
                         modal: true,                                               
                         resizable: false,                                              
                         dialogClass: 'no-close error-dialog'                                               
                     });                                                
                 <?php } ?>                                             
            </script>                                               
        </body>                                                 
    </html> 

$ form_f_029,_032,_033的值永远不会设置。

您将帖子变量复制到$字段中:

$fields = $_POST;

因此,这是按预期工作的:

<input type="text" name="name" id="name" value="<?=$fields['name']?>" /> 

,但使用$ form_f_0xx的用法被视为null,未定义或错误,具体取决于上下文

<input type="checkbox" id="Form_F_029" name="Form_F_029" <?php if($Form_F_029 == "on"){echo "CHECKED";}?> />

您可以通过设置

来解决此问题
$Form_F_029 = $fields['Form_F_029'];

$Form_F_029 = $_POST['Form_F_029'];

等。

对于此类调试问题,它可以有助于在输出中包括变量和数组的值,例如在注释或预先标签中:

echo "<!-- 29= $Form_F_029, 32 = $Form_F_032  -->";  // DEBUGGING
echo "<pre>- 29= $Form_F_029, 32 = $Form_F_032  </pre>";  // DEBUGGING

只记得删除它们或以后将其评论。

最新更新