在发送之前检查数据

时间:2015-06-19 23:42:15

标签: php

如何验证我网站上的字段,然后才能将其发送到处理它们的其他网站?现在它仍然将用户发送到另一个站点。

现在,处理数据的网站会在其网站上显示其自定义错误消息。这很不方便。这是代码的确切顺序。

PHP代码

<?php
    if(isset($_POST['submit'])) 
      {
             $name = $_POST['name'];
             $email = $_POST['email'];
             $phone = $_POST['phone']; 
             $error_message = "";

            if(empty($name) || empty($email) || empty($phone) )
              {
               $error_message = "<span class='error-message'>Fill all fields</span>";   
              }
      }
                      ?>

之后的HTML代码
<form method="post" action="Some other site that processes everything" class="big_form" >
    <input type="hidden" name="redirect" value="http://www.example.com" />

    <!-- Some more form fields that are not necessary for this example-->

</form>

1 个答案:

答案 0 :(得分:1)

PHP是服务器端代码,因此,如果您希望保持同一页面并使用响应进行POST,那么您将需要以某种方式使用Javascript。更好的是,您应该在将表单发送到PHP之前使用Javascript验证您的表单。您可以从表单中观看onsubmit事件。

<form onsubmit="validate();">

function validate() {
   //Check to see if everything is set otherwise error
}

另外http://www.w3schools.com/tags/att_input_required.asp

相关问题