Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.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运行post操作而不是表单提交_Php_Html_Forms - Fatal编程技术网

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

使用PHP运行post操作而不是表单提交,php,html,forms,Php,Html,Forms,这是家庭作业,我们需要使用PHP。除非绝对必要,否则我们不能使用javascript 我的当前表单在我的第一个PHP文档中。单击“提交”按钮后,它将向自身发布以验证和检查数据。我使用它在一个文档中使用PHP获得样式更改 如果一切都得到验证,它将使用刚刚发布的相同数据将操作发布到新的PHP 我的表单由3个文本输入、一组单选按钮(3个按钮)和一组复选框(4个复选框)组成。一旦它们都得到验证,我需要将这些数据发布到一个新的PHP文档中进行处理 这是我目前的代码: // Are

这是家庭作业,我们需要使用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;
            }

只需包含另一个文件中的函数,并在所有检查都通过时运行。或者,您可以重定向到新的url,但这将成为一个get而不是post。通常,您在一个位置处理所有post请求,并将http头重定向到某个结果页。

如果您安装了模块,您可以使用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;
}
(摘自)

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

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

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

这并没有像我想象的那样有效,但确实有效。我将此标记为应答,因为它在技术上是应答的。谢谢你的帮助。