Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在表单中检查PHP验证的语法_Php_Forms_Validation - Fatal编程技术网

在表单中检查PHP验证的语法

在表单中检查PHP验证的语法,php,forms,validation,Php,Forms,Validation,我的目标是:我需要验证,如果有人勾选了他们获得经济援助的复选框,我需要验证他们是否勾选了他们正在接受的类型 以下是我到目前为止在php验证部分的内容 if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))): if (isset($_POST['financial_aid'])) { $financial_aid = $_POST['financial_aid']; } if (

我的目标是:我需要验证,如果有人勾选了他们获得经济援助的复选框,我需要验证他们是否勾选了他们正在接受的类型

以下是我到目前为止在php验证部分的内容

if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):

    if (isset($_POST['financial_aid'])) { $financial_aid = $_POST['financial_aid']; }

if ($financial_aid === '') :
        $err_financial_aid = '<div class="error">Sorry, this is a required field</div>';
    endif; // input field empty

我错过了一些东西。我只是不知道是什么。有人能帮我吗?

首先让我说,您的检查输入增强了许多人错过的用户体验

您关于PHP验证的问题似乎有3个方面的回答:

1) 您可以使用以下命令显示表单代码:

财政援助和是的

但是你的if语句显示

 $financial_aid === 'yes'
在if代码中,Camel文本需要相同,以标识相同的变量,并且===相同as Yes与Yes不相同-需要注意并保持协调

2) 缺少每个输入表单字段的id组件。您仅使用“name=“financial_type””。Name用于通过表单提交发送数据项,但也可以使用id=“financial\u type”更容易地使用javascript或css为每个单选按钮/复选框使用不同的id。如果有疑问,只需同时执行name=“item1”和id=“item2”,然后下一个操作相同,但更改为item2,等等。这两个操作都是最好的,直到您在几个复选框(例如,框1表示红色框,2表示蓝色框,3表示绿色框)之间进行选择,而这些复选框不是此任务的一部分

3) 您不显示您是否正在使用表单提交或是否正在使用 “button type=“button”onclick=“dosomething()” 其中dosomething()作为提交触发

在另一端验证数据可以显示“on”一词,而不是通过检查或取消检查。我发现AJAX XMLHttpRequest传输的数据比表单提交的POST传输的数据多。如果您的问题是由“on”确认/验证而不是显示检查或取消检查状态引起的……请尝试下面的dosomething()脚本。 该脚本还显示了标准类型的字段…lname,其中包含和输入id以及要传输的localStorage数组。发送localStorage和表单Submit不能单独工作,使localStorage未发送。需要使用XMLHttpRequest来允许表单字段和localStorage在通过catchby.php中的代码在服务器上处理他的案例

此示例让客户端使用按钮发送

 <button type="button" onclick="dosomething();" class="btn btn-primary btn-lg">Complete </button>
完成
这将触发同一页面中的dosomething(),根据选中或未选中状态使用Yes或No从javascript发送到PHP

<script>
function dosomething() {
    // Start XMLHttpRequest
    var DaData = new XMLHttpRequest();
    // where to pass data to
    var url = "catchby.php";
    // Data items to pass..  lname is a field in the form
    var nm = document.getElementById("lname").value;
    /* financial_type is the id of checkbox input converted to Yes or No avoiding the constant "on" being sent */
    if($('#financial_type').attr('checked')) {
       var cb = "Yes";
      } else {
       var cb = "No";
      }

    /* svdStorage is the name of localStorage that is not part of the form that is other saved input user has entered saved on their client side computer that stays for next web visits... in this case an array that includes any outside the form data to send. */
    var ct = localStorage.getItem('svdStorage');

    // variables to be available in catchby.php via $_REQUEST in code
    var vars = "nname="+nm+"&ftype="+cb+"&scdStuff="+ct;

    DaData.open("POST", url, true);
    DaData.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    // Check onreadystatechange for XMLHttpRequest
    DaData.onreadystatechange = function() {
    if(DaData.readyState == 4 && DaData.status == 200) {
      var return_data = DaData.responseText;
      document.getElementById("status").innerHTML = return_data;
     }
    }
    // OK Send data items and process data transfer
    DaData.send(vars); 
    document.getElementById("status").innerHTML = "Processing...";
   }
   </script>    


Then to receive data being sent here is catchby.php coding...

 <?php
 $customerInformation = "";
 if(!empty( $_REQUEST["name"]))
  {
   $customerInformation .= "Name: ";
   $customerInformation .= $_REQUEST["name"];
   }

   // this net line will show the Yes or No reflecting the checked status instead of just the work "on"
  $customerInformation .= "<br/>The financial requested was: ".$_REQUEST["ftype"];

  // convert localStorage array to data items
  $zscdStuff  = json_decode($_REQUEST["scdStuff"]);
  // determine # of array items
  $length = count($zscdStiff);
  for ($i = 0; $i < $length; $i++) {
  // define items in each line of array
  // then 
  // do what is needed with each line item
 }
 ?>

函数dosomething(){
//启动XMLHttpRequest
var-DaData=new-XMLHttpRequest();
//向何处传递数据
var url=“catchby.php”;
//要传递的数据项..lname是表单中的字段
var nm=document.getElementById(“lname”).value;
/*financial_type是转换为是或否的复选框输入的id,避免发送常量“on”*/
if($(“#财务类型”).attr('checked')){
var cb=“是”;
}否则{
var cb=“否”;
}
/*svdStorage是本地存储的名称,该名称不属于表单的一部分,该表单是其他已保存的输入用户在其客户端计算机上输入并保存的,用于下次web访问……在本例中,是一个数组,其中包含要发送的任何表单外数据*/
var ct=localStorage.getItem('svdStorage');
//通过代码中的$u请求在catchby.php中可用的变量
var vars=“nname=“+nm+”&ftype=“+cb+”&scdStuff=“+ct;
打开(“POST”,url,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
//检查XMLHttpRequest的ReadyStateChange
DaData.onreadystatechange=函数(){
if(DaData.readyState==4&&DaData.status==200){
var return_data=DaData.responseText;
document.getElementById(“status”).innerHTML=返回数据;
}
}
//确定发送数据项并处理数据传输
数据发送(vars);
document.getElementById(“status”).innerHTML=“处理…”;
}
然后接收发送到这里的数据的是catchby.php编码。。。
注意…XMLHttpRequest将同时异步处理表单项和要传输的所有其他变量,而submit只传输表单项


使用1)完全相同的名称,2)在提交行的每个项目中使用参数id和名称参数,3)将选中/取消选中状态转换为明确的文本项目,如“是”和“否”“你的结果将是你所期望的

您的第一个if语句没有结束符
endif
,您的输入标记需要用
/>
关闭,否则{!empty$err\u finanical\u type=请选择一个返回false;}
没有意义。刚刚被告知键入错误。。。在2)中,它表示“如果有疑问,请同时执行name=“item1”和id=“item2”,并且应该表示“如果有疑问,请同时执行name=“item1”和id=“item1”
 <button type="button" onclick="dosomething();" class="btn btn-primary btn-lg">Complete </button>
<script>
function dosomething() {
    // Start XMLHttpRequest
    var DaData = new XMLHttpRequest();
    // where to pass data to
    var url = "catchby.php";
    // Data items to pass..  lname is a field in the form
    var nm = document.getElementById("lname").value;
    /* financial_type is the id of checkbox input converted to Yes or No avoiding the constant "on" being sent */
    if($('#financial_type').attr('checked')) {
       var cb = "Yes";
      } else {
       var cb = "No";
      }

    /* svdStorage is the name of localStorage that is not part of the form that is other saved input user has entered saved on their client side computer that stays for next web visits... in this case an array that includes any outside the form data to send. */
    var ct = localStorage.getItem('svdStorage');

    // variables to be available in catchby.php via $_REQUEST in code
    var vars = "nname="+nm+"&ftype="+cb+"&scdStuff="+ct;

    DaData.open("POST", url, true);
    DaData.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    // Check onreadystatechange for XMLHttpRequest
    DaData.onreadystatechange = function() {
    if(DaData.readyState == 4 && DaData.status == 200) {
      var return_data = DaData.responseText;
      document.getElementById("status").innerHTML = return_data;
     }
    }
    // OK Send data items and process data transfer
    DaData.send(vars); 
    document.getElementById("status").innerHTML = "Processing...";
   }
   </script>    


Then to receive data being sent here is catchby.php coding...

 <?php
 $customerInformation = "";
 if(!empty( $_REQUEST["name"]))
  {
   $customerInformation .= "Name: ";
   $customerInformation .= $_REQUEST["name"];
   }

   // this net line will show the Yes or No reflecting the checked status instead of just the work "on"
  $customerInformation .= "<br/>The financial requested was: ".$_REQUEST["ftype"];

  // convert localStorage array to data items
  $zscdStuff  = json_decode($_REQUEST["scdStuff"]);
  // determine # of array items
  $length = count($zscdStiff);
  for ($i = 0; $i < $length; $i++) {
  // define items in each line of array
  // then 
  // do what is needed with each line item
 }
 ?>