1

I have an issue that I have to retrieve the PHP Variable from the Grid to the Modal.

The situation is like this:

This is one of my columns which I name as 'flag', there is a condition state in the column that when the row contains 'test', the column will output a button.

[
                'headerOptions' => ['style' => 'text-align: center; width: 3%; vertical-align: middle;'],
                'contentOptions' => ['style' => 'text-align: center;'],
                'attribute'=>'flag',
                'format'=>'raw',
                'value'=>function($model){
                    // $result = $model->flag?
                    $getCode = $model->customer_code;

                    $getBillID = CustomerBillingParty::find()->select('id')->where(['customer_code'=>$getCode])->scalar();

                    $getMain = CustomerBillingParty::find()->select('to_main')->where(['id'=>$getBillID])->scalar();
                    $getbillto = CustomerBillingParty::find()->select('bill_to')->where(['id'=>$getBillID])->scalar();
                    $getbilladdress = CustomerBillingParty::find()->select('billing_address')->where(['id'=>$getBillID])->scalar();
                    
                    if($model->change_billing_party =="TEST")
                    {
                        $result= HTML::SubmitButton('ALERT',['name'=>'fname','value'=>$getCode,'id'=>'btnclickvalue','bill_address'=>$getbilladdress,'bill_to'=>$getbillto,'BillID'=>$getBillID,'To_Main'=>$getMain,'sspc_ref'=>$getSSPC,'customer_code'=>$getCode,'class'=>'twelve btn-new-billing'],['data-method' =>'POST']);
                  
                    }
                    else{
                        $result="N/A";
                    }
                    return $result;
                } 
            ],

The button contains a class that will output a Modal when clicked. This is performed by using javascript/jquery to show the modal.

$(document).on('click','.btn-new-billing',function(){
    $('#modal-new-billingParty').modal('show');

    $('#customerbillingparty-customer_code').val($(this).attr('customer_code'));
    $('#customerbillingparty-to_main').val($(this).attr('to_main'));
    $('#customerbillingparty-bill_to').val($(this).attr('bill_to'));
    $('#customerbillingparty-billing_address').val($(this).attr('bill_address'));
    //The 4 lines above are used to populate the form inside the Modal based on the row on the Grid.
});

The button also contains a set of attributes that carries a value to be used inside of a Modal.

I need to pass the value of the button when clicked in the Modal.

This is the modal code (The modal code is coded above the GRID): The modal is pop out when the button is clicked using the javascript/jquery.

Modal::begin([
    'header' => 'Billing Party',
    'id'=>'modal-new-billingParty',
    'size'=>'modal-lg',
]);

    
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "EMPTY";
    } else {
        echo "HELOOOOOOOOOOOOOOOOO";
    }
}

  //echo "HELLOOOOO";
   
    echo    '<div class="customer-billing-party-form">';

    $form = ActiveForm::begin([

        'action' =>['customer-billing-party/create']
    ]);

    echo    $form->field($modelBill, 'customer_code')->textInput(['maxlength' => true,'readonly'=>true]);

 
        echo    $form->field($modelBill, 'to_main')->dropDownList(
        
       ArrayHelper::map(CustomerBillingParty::find()->where(['id'=>***NEED TO PASS VALUE HERE***])->all(),'id','to_main'),
   
        [
           'prompt'=>'-Select To Main-',
        ]
    );


    //echo    $form->field($modelBill, 'to_main')->textInput(['maxlength' => true]);
    //$form->field($modelBill, 'to_main')->textInput(['maxlength' => true]);

    echo    $form->field($modelBill, 'bill_to')->textInput(['maxlength' => true,'readonly'=>true]);

    echo    $form->field($modelBill, 'billing_address')->textInput(['maxlength' => true,'readonly'=>true]);

    echo    '<div class="form-group">';
    echo        Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']);
    echo    '</div>';

    ActiveForm::end();

    echo    '</div>';

Modal::end();

This Modal is an input form that will pop out of the screen when the button is clicked.

The issue is that I need to pass the PHP variable inside the Grid view which is $getCode to the Modal. I need to pass the value in the dropdownlist in the Modal.

You can see the note 'NEED TO PASS VALUE HERE'

How do I solve this issue?

  • Why you checking the `$_POST` when you call modal? You can simply add the `data-myId="= $id ?>"` attribute to your link or some info what you need to pass to JS, and get this value on `event listener` – Serghei Leonenco May 11 '22 at 04:33
  • Appreciate your advice Mr. Serghei. I'm still new Yii2. What do you mean by adding the "data-myId="= $id ?>" to my link? On which part of the code should I added that line? appreciate you can show me an example on that. Plus, how to use the event listener to get the value. Would like to get more clarifications on that – firdaus jamdi May 11 '22 at 04:41
  • You can check it here: https://stackoverflow.com/questions/14935191/adding-data-attribute-to-dom – Serghei Leonenco May 11 '22 at 04:45
  • Thank you for the link, But still did not quite get it how to use the data attributes and event listener – firdaus jamdi May 11 '22 at 04:52
  • So get the value on event listener at the Modal or the Javascript that is use to pop out the Modal? – firdaus jamdi May 11 '22 at 04:59

0 Answers0