Enter several values in the same input? Example: multiple mail recipients

0

How can I do to enter several values in the same input? For example, if I want to send an email notification to several recipients, using PHP, Javascript, etc., in the Yii framework?

In the one form (not HTML) I have the following:

public function createAlert(){        
    $id = $_POST['idAlert'];
    $alertModel = Model_Email::model()->findByPk($id);
    if($alertModel != NULL){
        $alertModel->email = $this->email;
        $alertModel->id_search = $this->id_search;
        $alertModel->time_of_recurrence_unit = $this->time_of_recurrence_unit;
        $alertModel->date_start = $this->date_start;
        $alertModel->id_preference = $this->id_preference;
        $alertModel->status = 'ENABLED';
        $alertModel->save();
    }
    else{
    $emailModel = new Model_Email();
    $emailModel->email = $this->email;
    $emailModel->id_search = $this->id_search;
    $emailModel->time_of_recurrence_unit = $this->time_of_recurrence_unit;
    $emailModel->date_start = $this->date_start;
    $emailModel->id_preference = $this->id_preference;
    $emailModel->status = 'ENABLED';
    if($emailModel->validate() && $emailModel->save()){
        return true;
    }
    else{
        return false;
        }
    }          
}

What creates a notification that will be sent to the user and in case there is already such notification will modify it.

Meanwhile, in the Controller I have the following:

public function actionSave(){
    if(!Yii::app()->user->isGuest){            
        $emailModel = new Form_search_email_notification();            
        if(isset($_POST['ajax']) && $_POST['ajax'] === 'email-form'){
            echo CActiveRecord::validate($emailModel);                
            Yii::app()->end;
        }            
        if(isset($_POST['Form_search_email_notification'])){                    
            $emailModel->email = $_POST['Form_search_email_notification']['email'];
            $emailModel->id_search = $_POST['Form_search_email_notification']['id_search_combo'];
            $emailModel->id_preference = $_POST['Form_search_email_notification']['id_preference'];
            $emailModel->time_of_recurrence_unit = $_POST['Form_search_email_notification']['time_of_recurrence_unit'];                
            if($emailModel->validate() && $emailModel->createAlert()){
                Yii::app()->user->setFlash('success', 'Notificación creada');
                $this->redirect('/alert/list');                    
            }
        }
        $this->redirect(array('alert/list'));
    }
}

I appreciate beforehand if someone can help me, that the code I put it just to show what I do, what I want to know is how to apply it for several values at the same time, that is, several recipients in this case.

Oh, by the way, the idea would be to enter a recipient, press enter and enter another, and so on, something like when they put tags in some places.

    
asked by Ccccccccc 03.10.2017 в 14:48
source

2 answers

1

There is an extension for yii2 of kartik of multiple select Do what you want and when sending it sends you in an array, and only you would have to manage the information so that with the data you can do what you want from the side of the backend

link

If you are new to yii2 I recommend you take a look at all extensions of kartik and two friends, they have very good that will facilitate many things.

kartik: link

two friends: link

    
answered by 03.10.2017 в 16:54
0

You can separate emails with a semicolon, for example. If you had a character filter on the field before recovering its value you should deactivate it and filter the emails once separated.

When collecting the content of the input on the server you have a string with all the addresses, separated by semicolons (or the one you will use), separates them and valid ones and do what you have to do with them.

For what you want to do, add each email to an input and keep it in a list with the enter, then add another, use JS. It is concatenated in a string that you can show in an element, for example of paragraph type while keeping this string in a hidden type input that will be from where you collect the data when sending it to the server.

    
answered by 03.10.2017 в 20:28