简体   繁体   English

yii2我怎样才能获得差价

[英]yii2 how can I access post value

I have the following code in my controller trying to get it to work before adding validation etc 我在控制器中有以下代码,试图在添加验证等之前使其正常工作

This code is from here however I will be adapting it anyway 这段代码是从这里开始的,但是我还是要改编它

    $email = $_POST['Newsletter[email]'];

    $session->save_email($email);
    // Subscribe User to List
    $api_key = new sammaye\mailchimp\Mailchimp(['apikey' => 'xxxxxxxxxxx']);
    $list_id = "xxxxxxxxxxx";

    $Mailchimp = new Mailchimp( $api_key );
    $Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
    $subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => $email ) );

However I get the following error (Does this mean my most data is in an array newbie) 但是我收到以下错误(这是否意味着我的大部分数据在新手数组中)

Undefined index: Newsletter[email]

Is this something I need to set within my yii2 form so that instead of the name field being Newsletter[email] its just email? 这是我需要在yii2表单中设置的内容,以便其名称字段不只是Newsletter [email]而是电子邮件吗?

As was already stated you can either use $_POST or the request -object. 如前所述,您可以使用$_POSTrequest -object。 That object encompasses everything that enters your application on startup. 该对象包含启动时进入应用程序的所有内容。 So yes, \\Yii::$app->request->post() gives you all incoming POST-data and \\Yii::$app->request->post('name') will give you a single one. 所以是的, \\Yii::$app->request->post()给您所有传入的POST数据,而\\Yii::$app->request->post('name')将给您一个。 Same with the get function. get函数相同。

However given your code that is not how you should be using Yii. 但是鉴于您的代码,您不应该使用Yii。 The name of your variables suggests that the post is done using a model, so you might want to use that again, makes it a lot easier on validation. 变量的名称表明该帖子是使用模型完成的,因此您可能希望再次使用它,从而使验证更加容易。

If you don't have the model, it can look like so: 如果没有该模型,则它可能如下所示:

class Newsletter extends \yii\base\Model
{
   public $email;

   public function rules()
   {
      return array(
        array('email', 'email', 'skipOnEmpty' => false)
      );
   }
}

The actual code in your controller could be more amongst the lines: 控制器中的实际代码可能更多:

$request = \Yii::$app->request;
if ($request->isPost) {
   $newsletter = new Newsletter;
   if ($newsletter->load($request->post()) &&  $newsletter->validate()) {
      // do your thing 
   }
}

You can do this as follows: 您可以按照以下步骤进行操作:

if (Yii::$app->request->post()) {
    $data = Yii::$app->request->post();
    $email = $data['NewsLetter']['email'];
}

It means, it is inside an array. 这意味着它在数组内部。

Try to do the following instead : 尝试改为执行以下操作:

// Using isset will make sure, you don't trigger a Notice (when the variable does not exist)
$email = isset($_POST['Newsletter']['email']) ? $_POST['Newsletter']['email'] : null;

// Make sure you are receiving an email address
if ($email && filter_var($email, FILTER_VALIDATE_EMAIL))
{
    $session->save_email($email);
    // Subscribe User to List
    $api_key = new sammaye\mailchimp\Mailchimp(['apikey' => 'xxxxxxxxxxx']);
    $list_id = "xxxxxxxxxxx";

    $Mailchimp = new Mailchimp( $api_key );
    $Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
    $subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => $email ) );
}
// Display an error message ?
else 
{
    // @todo
}

If the HTML form field is as below 如果HTML表单字段如下

<input name="Newsletter[email]" type="text">

then the code with in controller should be 那么在控制器中的代码应该是

$data = Yii::$app->request->post();
$email= $data['Newsletter']['email'];

您只需尝试$_POST['Newsletter']['email']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM