0

感谢您抽出时间来阅读....

我的任务是验证 php 表单并在新页面“process-comp.php”中对其进行处理,我可以在 JS 中验证它是否正常,我可以在 PHP 中验证它是否正常。问题:如果验证不正确,则会提示错误。如果它验证正确,它不会去任何地方,也不会处理表单

一旦表单完成并验证,我需要知道如何将表单及其值重定向到“process-comp.php”。

如果有错误,我不希望用户在空白字段中再次输入信息,只是为了更正错误的字段。

以及如何防止 XSS(跨站点脚本)攻击,

请在下面找到代码:

    <?PHP
   require_once ("formvalidator.php");

   $show_form=true;
   if(isset($_POST['Submit']))
   {// We need to validate only after the form is submitted
   //   
   // The first argument is the name of the input field in the form. 
   // The second argument is the validation descriptor that tells the type of the validation required. 
   // The third argument 
   // is the error message to be displayed if the validation fails. 
   //

   //Setup Server side Validations
       //Please note that the element name is case sensitive 
       $validator = new FormValidator();
       $validator->addValidation("FIRSTNAME_FIELD","req","Please enter first name");
       $validator->addValidation("LASTNAME_FIELD","req","Please neter your surname");    
       $validator->addValidation("EMAIL_FIELD","email","Enter a valid email value");
       $validator->addValidation("EMAIL_FIELD","req","Please enter an Email");
       $validator->addValidation("STORE_NAME_FIELD","req","Please select a store");


       //Then validate the form
       if($validator->ValidateForm())
       {
    // 
   // How do i make it to process the form it is correctly to "process-page.php"
    //
       }
       else
       {
           echo "<B>Validation Errors:</B>";

           $error_hash = $validator->GetErrors();
           foreach($error_hash as $inpname => $inp_err)
           {
             echo "<p>$inpname : $inp_err</p>\n";
           }
       }
   }

   if(true == $show_form)
   {
   ?> 





   <form class="" name="emvForm" id="emvForm"  action="" method="POST" style="" >

        <table width="380" border="0" cellspacing="10" cellpadding="" style="">
     <tr>
       <td class="form-comp"><label for="FIRSTNAME_FIELD" style="">First name*</label></td>
       <td><input type="text"  class="required nameaa " id="FIRSTNAME_FIELD" name="FIRSTNAME_FIELD" value="" size="25" maxlength="64" style=""></td>
     </tr>
     <tr>
       <td class="form-comp"><label for="LASTNAME_FIELD">Surname* </label></td>
       <td><input type="text" class="required nameaa" id="LASTNAME_FIELD" name="LASTNAME_FIELD" value="" size="25" maxlength="64" style=""></td>
     </tr>
     <tr>
       <td class="form-comp"><label for="EMAIL_FIELD">Email*</label></td>
       <td><input type="text" id="EMAIL_FIELD" class="required email" name="EMAIL_FIELD" value="" size="25" maxlength="64" style=""></td>
     </tr>
     <tr>
       <td class="form-comp"><label for="STORE_NAME_FIELD">Select shop*</label></td>
       <td><select id="STORE_NAME_FIELD" name="STORE_NAME_FIELD" class="required" style="">
         <option selected disabled="disabled" value="">Select shop</option>
         <option value="Aberdeen |T: 0123456789 | 345 Lorem High St EFG 456">Aberdeen </option>
         <option value="Acton | T: 0123456789 | 123 Ipsum High St ABD 123">Acton </option>
         <option value="Aldgate">Aldgate </option>
         <option value="Ashford">Ashford </option>
         <option value="Aylesbury">Aylesbury </option>
         <option value="Baker Street">Baker Street </option>

       </select></td>
     </tr>
     <tr>
       <td> <label for="STORE_NAME_FIELD"> </label>
       </td>
       <td>
        <input type='submit' name='Submit' value='Submit form' style=" cursor:pointer;">
       </td>
     </tr>
   </table>
         <input type="hidden" id="SOURCE_FIELD" name="SOURCE_FIELD" value="St-Petes">
         <input type="hidden" id="PROMO_FIELD" name="PROMO_FIELD" value="<?php echo($Promo); ?>">
         <input type="hidden" id="PROMO2_FIELD" name="PROMO2_FIELD" value="<?php echo($Promo2); ?>">





   </form>

   <?PHP
   }//true == $show_form
   ?>



   </table>
4

2 回答 2

2

根据您的流程页面的外观,这应该是最简单的方法。

if($validator->ValidateForm())
 {
    include("process-page.php");   
 }

但我建议将文件的相关部分“复制”到当前文件中,因为这样,任何人(有知识的人)都可以直接 POST 到处理站点并绕过验证。

于 2013-01-15T11:22:47.700 回答
1

我会建议你用 ajax 来做。

将表单数据发送到您的 proccess-page.php,如果有效则返回 true,如果无效则返回错误消息。

这将为您提供如何防止 XSS 的信息:如何使用 HTML/PHP 防止 XSS?

于 2013-01-15T11:22:13.253 回答