Вы можете использовать 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;
}
(взято с здесь )
Эта функция отправит запрос POST из кода PHP, а затем, когда вы проверили все поля с помощью одностраничной формы, вы можете вызвать его с помощью:
try{
$result = post('processValidated.php', array(
'foo' => 'bar',
'field' => 'Value'
));
echo $result;
} catch(Exception $e){
echo $e->getMessage();
}
Как уже упоминалось, это не совсем стандартный способ или обработка ввода данных и проверки данных.Если это небольшая отдельная форма, то недопустимые и допустимые представления формы обрабатываются одним сценарием php или включенными классами при использовании ООП.В качестве альтернативы инфраструктура MVC будет использовать контроллеры и т. Д. Для проверки и обработки представлений форм и соответственно отвечать