php-ページで呼び出されたオブジェクトから$_POST配列にアクセスする
フォームからPOSTデータを受け取るページがあります。また、FormValidationオブジェクトがページに作成されたときに$_POST配列にアクセスしようとするフォーム検証クラスもあります。ただし、FormValidationオブジェクトは$_POST配列にアクセスできないようです。なぜこれが起こっているのですか、どうすればこれを修正できますか?
編集:コードを追加:)
このコードはregister.phpからのものです
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
var_dump($_POST);
$errors = validate_form();
if(count($errors) > 0)
{
display($errors);
}
else
{
act();
}
}
else
{
display();
}
function validate_form()
{
$validator = new FormValidation('POST');
$validator->add_field('first_name', 'First Name');
$validator->add_field('last_name', 'Last Name');
$validator->add_field('email_address', 'Email Address');
$validator->add_field('password', 'Password');
$validator->add_field('password_confirmation', 'Password Confirmation');
$validator->add_rule('first_name', 'required');
$validator->add_rule('last_name', 'required');
$validator->add_rule('email_address', 'required');
$validator->add_rule('email_address', 'is_valid_scarsdaleschools_email');
$validator->add_rule('password', 'required');
$validator->add_rule('password_confirmation', 'required');
$validator->add_rule('password_confirmation', 'matches', array('password', 'Password'));
return $validator->validate();
}
フォーム検証コードは次のようになります
class FormValidation
{
var $fields;
var $rules; //Associative array that maps field_name to array of rules
var $errors;
var $form_method_type;
function FormValidation($form_method_type)
{
$this->form_method_type = strtoupper($form_method_type);
$this->fields = array(); //maps fields to field labels
$this->rules = array();
$this->errors = array();
}
function validate()
{
foreach(array_keys($this->fields) as $field_name)
{
if(!array_key_exists($field_name, $this->rules))
{
continue;
}
foreach(array_keys($this->rules[$field_name]) as $rule_name)
{
call_user_func_array(array($this, $rule_name), $this->rules[$field_name][$rule_name]);
}
}
return $this->errors;
}
function add_field($field_name, $field_label)
{
$this->fields[$field_name] = $field_label;
}
function add_rule($field_name, $rule_name, $parameters=array())
{
if(!isset($this->rules[$field_name]))
{
$this->rules[$field_name] = array();
}
array_unshift($parameters, $field_name, $this->fields[$field_name]);
$this->rules[$field_name][$rule_name] = $parameters;
}
function var_isset($field_name)
{
global ${'$_' . $this->form_method_type};
var_dump(${'$_' . $this->form_method_type}[$field_name]);
return isset(${'$_' . $this->form_method_type}[$field_name]);
}
function get_value($field_name)
{
global ${'$_' . $this->form_method_type};
return ${'$_' . $this->form_method_type}[$field_name];
}
////////////////////////////////////////////////////////////
/////RULES//////////////////////////////////////////////////
////////////////////////////////////////////////////////////
function required($field_name, $field_label)
{
if(!$this->var_isset($field_name))
{
$this->errors[$field_name] = "$field_label is a required field";
}
}
function is_valid_email($field_name, $field_label)
{
if($this->var_isset($field_name))
{
if(!validEmail($this->get_value($field_name)))
{
$this->errors[$field_name] = "$field_label requires a valid email address";
}
}
}
function is_alpha($field_name, $field_label)
{
if($this->var_isset($field_name))
{
if(!ctype_alpha($this->get_value($field_name)))
{
$this->errors[$field_name] = "$field_label requires alphabetical characters only";
}
}
}
function is_alphanumeric($field_name, $field_label)
{
if($this->var_isset($field_name))
{
if(!ctype_alnum($this->get_value($field_name)))
{
$this->errors[$field_name] = "$field_label requires alphanumeric characters only";
}
}
}
function is_integer_number($field_name, $field_label)
{
if($this->var_isset($field_name))
{
if(!is_int($this->get_value($field_name)) || preg_match('[-+]?[0-9]+', $this->get_value($field_name)) == 0)
{
$this->errors[$field_name] = "$field_label must be an integer";
}
}
}
function is_float_number($field_name, $field_label)
{
if($this->var_isset($field_name))
{
if(!is_float($this->get_value($field_name)) || preg_match('[-+]?[0-9]*\.?[0-9]+', $this->get_value($field_name)) == 0)
{
$this->errors[$field_name] = "$field_label must be a number";
}
}
}
function is_valid_scarsdaleschools_email($field_name, $field_label)
{
if($this->var_isset($field_name))
{
if(!validEmail($this->get_value($field_name)))
{
$this->errors[$field_name] = "$field_label requires a valid email address";
}
$email = $this->get_value($field_name);
if(!(endsWith($email, 'scarsdaleschools.org', $case=false) || endsWith($email, 'scarsdaleschools.com', $case=false) || endsWith($email, 'scarsdaleschools.k12.ny.edu', $case=false)))
{
$this->errors[$field_name] = "$field_label requires a Scarsdale Schools email address";
}
}
}
function max_length($field_name, $field_label, $max_length)
{
if($this->var_isset($field_name))
{
if(strlen($this->get_value($field_name)) > $max_length)
{
$this->errors[$field_name] = "$field_label cannot be longer than $max_length characters";
}
}
}
function min_length($field_name, $field_label, $min_length)
{
if($this->var_isset($field_name))
{
if(strlen($this->get_value($field_name)) > $min_length)
{
$this->errors[$field_name] = "$field_label must be at least $min_length characters";
}
}
}
function matches($field_name, $field_label, $match_field_name, $match_field_label)
{
if($this->var_isset($field_name) && !$this->var_isset($match_field_name))
{
$this->errors[$field_name] = "$field_label must match $match_field_label";
}
elseif(!$this->var_isset($field_name) && $this->var_isset($match_field_name))
{
$this->errors[$field_name] = "$field_label must match $match_field_label";
}
if($this->var_isset($field_name) && $this->var_isset($match_field_name))
{
if(strcmp($this->get_value($field_name), $this->get_value($match_field_name)) != 0)
{
$this->errors[$field_name] = "$field_label must match $match_field_label";
}
}
}
}
助けてくれてありがとう!
答え :
解決策:
コードに小さなエラーがあります。あなたが書いていたのは本質的に$$_POST
でした。変数名から余分なドル記号を削除する必要があります(以下の修正された方法)。さらに、$_POSTはスーパーグローバルまたは自動グローバルであるため、 global $_POST;
を呼び出すことを心配する必要はありません。
更新: ${'_' . $this->form_method_type}
を介した$_POST
の取得が機能していないようです。私が送信したコードは、クラスの外部では機能しますが、内部では機能しません。私の発見とその違いをあなたが理解していることを確認したかったのです。したがって、${'_'。 $ formMethodType}はクラスの外部で機能し、内部では次のようなものを使用する必要があります:
const TYPE_POST ='POST';
const TYPE_GET ='GET';
const TYPE_SESSION ='SESSION';
const TYPE_SERVER='サーバー';
const TYPE_REQUEST ='REQUEST';
public $ fields = array();
public $ rules = array();
public $ errors = array();
パブリック関数__construct($ formMethodType)
{{
$ r = new ReflectionClass($ this);
$ constants = $ r-> getConstants();
$ formMethodType = strtoupper($ formMethodType);
if(!array_key_exists('TYPE_'。$formMethodType、$ constants)){
throw new InvalidArgumentException('$ formMethodTypeに一致するタイプが見つかりませんでした:'。$ formMethodType);
}
$ this-> form_method_type = $ formMethodType;
}
パブリック関数var_isset($ field_name)
{{
$ values = $ this-> get_values();
var_dump($ values [$ field_name]);
isset($ values [$ field_name]);を返します。
}
パブリック関数get_value($ field_name)
{{
$ values = $ this-> get_values();
$ values[$field_name]を返します;
}
パブリック関数get_values()
{{
スイッチ($ this-> form_method_type){
ケースself::TYPE_POST:
$ values = $_POST;
壊す;
ケースself::TYPE_GET:
$ values = $ _GET;
壊す;
ケースself::TYPE_REQUEST:
$ values = $ _REQUEST;
壊す;
ケースself::TYPE_SERVER:
$ values = $ _SERVER;
壊す;
ケースself::TYPE_SESSION:
$ values = $ _SESSION;
壊す;
}
$valuesを返します。
}
答え :
解決策:
まだコードが投稿されていませんが、依存性注入を使用して $_POST
データを取得することをお勧めしますクラス内からスーパーグローバルにアクセスするのではなく、フォーム検証クラスに追加します。
コードをテストするときに$_POST
が正しく入力されているとは限らないため、{-code-1のコンテンツを挿入することをお勧めします。 }
はそれが使用されるクラスのコンストラクターへのパラメーターとして。これにより、後でデバッグしやすくなります。
class MyClass {
//ポスト配列データを保持するパブリックプロパティ
public $ postdata;
//コンストラクターはパラメーターとして$_POSTを受け取ります
function __construct($ param1、$ param2、$ postdata){
//
$ this-> postdata = $ postdata;
}
}
//$_POSTを挿入してクラスをインスタンス化します
$ x = new MyClass($ a、$ b、$_POST);
コード投稿後の補遺
検証時に実際にget_value()
メソッドを呼び出すメソッドがクラスにありません。 var_isset()
と{以外で、そのクラスの
メソッド。 $_POST
のコンテンツにアクセスしたことがあるかどうかは不明です。 -code-5}
答え :
解決策:
/**
* This should work for $_REQUEST and $_POST the mysql_real_escape_string
* was added to escape urls. It is best not to allow URLs as parameters.
*/
foreach( $_POST as $key=> $val)
{
${$key} = mysql_real_escape_string($val);
}
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。