2017-12-13 16 views
1

"029,032および033"の "newvar_"値は、エラーチェック中に記憶されず、修正されたフォームが送信されたときにもポストされません。エラーチェック中または投稿中にPHPチェックボックスの値が保存されない

「名前」フィールドは、エラーチェックと投稿の両方で正しく機能します。

すべてのチェックボックスのフォームデフォルトは選択されていません。

ユーザーは、希望するフォームのみを選択することができます。それらの選択は、名前とともにtxtファイルに投稿する必要があります。あなたの援助のため、事前に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 $name\n 

REQUESTED FORMS AND SUPPLIES:\n 
STANDARD FORMS ++++++++++++++++++++ 
Form F-029:\t $Newvar_029 
Form F-032:\t $Newvar_032 
Form F-033:\t $Newvar_033\n 

=============================================================================== EOF =\n\n);"               
      fclose($file);            
     }             

    // Format and send email notice of successful submittal              
     if ($_POST['submit']) {             

      $sn_header = "From: Inquiries Database\n"            
       . "Reply-To: [email protected]\n";            
      $sn_subject = "Ordering Supplies Request";            
      $sn_email_to = "[email protected]";             
      $sn_message = "Please check the database for a recent submission.\n\n"            
      . "This is a post-only message.\n\n"             
      . "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> 

答えて

0

$ Form_F_029、_032、_033の値は決して設定されません。

$fields = $_POST; 

だから、これは意図したとおりに働いていた:

あなたは$フィールドにPOST変数をコピーし

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

しかし$ Form_F_0xxの用途はヌル、未定義または文脈に応じて、偽として扱われます。

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

それはコメントやpreタグには例えば、あなたの出力に変数と配列の値を含めることができ、このような問題をデバッグするための
$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 

ただちに削除したりコメントアウトしてください。

+0

あなたのおすすめではないかもしれません。 –

+0

おそらく私は誤解していますか?私は私の元のコードを置き換える: ;と :? と: <?php $ Form_F_029 = $ _POST ['Form_F_029']; ?> エラーチェック中に保存されなかったか、またはテキストファイルに投稿されませんでした。 –

+0

明確にするために...私は一緒にではなく、一度に1つのおすすめを試しました。 –

関連する問題