使用PHP运行后操作而不是从表单提交

时间:2019-04-02 03:35:22

标签: php html forms

这是家庭作业,我们需要使用PHP。除非绝对必要,否则我们不能使用javascript。

我当前的表单在我的第一个PHP文档中。单击提交按钮后,它会自行发布以验证和检查数据。我用它来在一个文档中使用PHP进行样式更改。

如果一切都经过验证,它将以刚刚发布的相同数据操作发布到新的PHP。

我的表单是由3个文本输入,一组单选按钮(3个按钮)和一组复选框(4个复选框)构建的。它们全部验证后,我需要将该数据发布到新的PHP文档中进行处理。

这是我当前的代码:

            // Are all fields fill
            if (isset($_POST['post'])) {
                $isCodeCorrect = checkStatusCodeCorrect();
                $isTextCorrect = checkStatusTextCorrect();
                $isShareOptionCorrect = checkRadioButtonCorrect();
                $isDateCorrect = checkDateCorrect();
                $isPermissionsCorrect = checkPermissionsCorrect();

                if($isCodeCorrect&&$isTextCorrect&&$isShareOptionCorrect&&$isDateCorrect&&$isPermissionsCorrect){
                    $mainFormAction='action="poststatusprocess.php"';
                }
            }

            function checkStatusCodeCorrect() {
                if(!empty($_POST['statusCode'])){
                    $str1 = $_POST['statusCode'];
                    //Only S0001 style code
                    $statusCodePattern = "/^S\d{4}$/";
                    if(preg_match($statusCodePattern, $str1)){
                        return true;
                    }else return false;
                }else return false;
            }
            function checkStatusTextCorrect() {
                if(!empty($_POST['statusText'])){
                    $str2 = $_POST['statusText'];
                    //Only a-z, A-Z, commas, explanation marks, question marks, apostraphese, and full stops. Case insensitive and global searching and any white space
                    $statusTextPattern = "/([a-z,!?'\.\s])/i"; 
                    if(preg_match($statusTextPattern, $str2)){
                        return true;
                    }else return false; 
                }else return false;
            }
            function checkRadioButtonCorrect() {
                return !empty($_POST['shareOption']);
            }
            function checkDateCorrect() {
                if(!empty($_POST['date'])){
                    $str3 = $_POST['date'];
                    //Only 2 digits then forward slash, then 2 digits then forward slash and then only 2 or 4 digits for the year
                    $datePattern = "/^((\d\d)+\-\d{2}\-\d{2})$/"; 
                    if(preg_match($datePattern, $str3)){
                        return true;
                    }else return false;
                }else return false;
            }
            function checkPermissionsCorrect(){
                if((!empty($_POST['allowLike']))||(!empty($_POST['allowComment']))||(!empty($_POST['allowShare']))||(!empty($_POST['allowNone']))){
                    return true;
                }else return false;
            }

2 个答案:

答案 0 :(得分:1)

如果已安装模块,则可以使用CURL,或者,因为这是家庭作业,您可能希望学习并了解如何手动进行。


/**
 * Send a POST request without using PHP's curl functions.
 *
 * @param string $url The URL you are sending the POST request to.
 * @param array $postVars Associative array containing POST values.
 * @return string The output response.
 * @throws Exception If the request fails.
 */
function post($url, $postVars = array()){
    //Transform our POST array into a URL-encoded query string.
    $postStr = http_build_query($postVars);
    //Create an $options array that can be passed into stream_context_create.
    $options = array(
        'http' =>
            array(
                'method'  => 'POST', //We are using the POST HTTP method.
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $postStr //Our URL-encoded query string.
            )
    );
    //Pass our $options array into stream_context_create.
    //This will return a stream context resource.
    $streamContext  = stream_context_create($options);
    //Use PHP's file_get_contents function to carry out the request.
    //We pass the $streamContext variable in as a third parameter.
    $result = file_get_contents($url, false, $streamContext);
    //If $result is FALSE, then the request has failed.
    if($result === false){
        //If the request failed, throw an Exception containing
        //the error.
        $error = error_get_last();
        throw new Exception('POST request failed: ' . $error['message']);
    }
    //If everything went OK, return the response.
    return $result;
}

(取自here

此函数将从PHP代码中发送POST请求,然后使用单页表单验证了所有字段后,可以使用以下命令进行调用:

try{
    $result = post('processValidated.php', array(
        'foo' => 'bar',
        'field' => 'Value'
    ));
    echo $result;
} catch(Exception $e){
    echo $e->getMessage();
}

如上所述,这实际上不是处理表单输入和数据验证的标准方法。如果是小型独立表单,则无效和有效表单提交都将由一个php脚本处理;如果使用OOP,则由包含的类处理。另外,MVC框架将使用控制器等来验证和处理表单提交并做出相应的响应

答案 1 :(得分:-1)

只需包含其他文件中的函数,并在所有检查均通过的情况下运行。或者,您可以重定向到新的url,但这将成为获取而不是发布。通常,您将所有发布请求都放在一个地方,然后将http标头重定向到某个结果页面。

相关问题