A
A
Andrew2016-01-24 23:42:11
Yii
Andrew, 2016-01-24 23:42:11

How to submit data on a button?

Dear colleagues. I have a form

<?php $form = $this->beginWidget('CActiveForm', array(
    'id' => 'form-Office-reservd',
    'enableAjaxValidation' => false,
    'action' => "/site/Ocenka31",
    'method' => "POST"
)); ?>

<div class="container container-brd-none">
    <h2>Оцените качества обслуживания</h2>

    <div class="containers">
    
        <div class="tr">
    <div class="table tbl_reserv">
            <div class="td">В какое подразделение Вы бращались?</div>
            <div class="td">
                <select id="Office_type" class="default-field" name="Office[Office_type_id]">
                    <?php foreach ($Office_types as $Office_type): ?>
                        <option
                            value="<?php echo $Office_type->id ?>"
                            <?php echo ($selected_type->id == $Office_type->id) ? 'selected="selected"' : '' ?>
                            capacity="<?php echo $Office_type->capacity ?>"
                            >
                            <?php echo $Office_type->name; ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
                
    </div>
</div>
</div>

<div class="container reserv_info">
    <button id="room-reservd2" type="button" class="button"
            onclick="Ocenka($(this))">
        <span class="btn-text">Неудовлетворительно</span>
    </button>
    <button id="Office-reservd" type="button" class="button"
            onclick="Ocenka3($(this))">
        <span class="btn-text">Удовлетворительно</span>
    </button>
    <button id="room-reservd4" type="button" class="button"
            onclick="Ocenka($(this))">
        <span class="btn-text">Хорошо</span>
    </button>
    <button id="room-reservd5" type="button" class="button"
            onclick="Ocenka($(this))">
        <span class="btn-text">Отлично</span>
    </button>
    <h2>Спасибо за вашу оценку!</h2>
</div>

<?php $this->endWidget(); ?>

</div>

Buttons handled by script
function Ocenka3($el) {
console.log($el);
    $el.jax(
        function () {
            //complete
        }, function (data) {
            //alert('Спасибо за Вашу оценку. мы учтем это в нашей работе.');
            //$('#reserved_captha_button').click();
            //$('#reserved_captha input').val('');
           // $('#form-room-reservd input').val('');
        },
        function (data) {
          //  $('#reserved_captha_button').click();
           // $('#reserved_captha input').val('');
            for (key in data.message) {
               // alert(data.message[key]);
            }
        });
}

 $.fn.jax = function (complete, success, success_error) {

        var $element = $(this);
        var data, url;
        var $form = $('#form-' + $element.attr('id'));
console.log($form);

        if ($form.length !== 0) {
            data = $form.serialize();
            url = $form.attr('action')
        }

        button.disable($element);

        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: data,
            success: function (data) {

                if (data != null)
                    if (data.status === 'success') {

                        if (typeof success !== "undefined") {
                            success(data);
                        }

                    } else {

                        if (typeof success_error !== "undefined") {
                            success_error(data);
                        } else {
                            for (key in data.message) {
                                alert(data.message[key]);
                            }
                        }
                    }

            },
            complete: function () {
                button.undisable($element);
                if (typeof complete !== "undefined") {
                    complete();
                }
            },
            error: function () {
                button.undisable($element);
                alert("Непредвиденная ошибка");
            }

        });

        return $element;

    };

and controller
public function actionOcenka31($id=3)
    {

        $ocenka = new ocenka();
        //if($_POST) { echo ''; print_r($_POST); echo ''; }
    $ocenka->date_from=new CDbExpression('NOW()');
    $ocenkas=OfficeType::model()->findByPk($_POST['Office']['Office_type_id']);
    
    $ocenka->full_name=$ocenkas->name;
    //$ocenka->ocenka='3';
    $ocenka->save(); 
    Yii::app()->user->setFlash('success',"Спасибо за Вашу оценку. мы учтем это в нашей работе");
 $respons3 = array('status' => 'success');
  echo json_encode($respons3);
     
    }

Question: How can I pass the data of the pressed button to the controller? when you click on "Satisfactory" - "3" should get into the controller, there I process it through if and write it to $ocenka->ocenka='what came there';
You can do it but it's not pretty
<?php echo CHtml::button('Button Text', array('submit' => array('site/Ocenka3'))); ?>

each evaluation has its own block in the controller. tell me plz. how to be better
I’ll say right away I tried to do this
<button id="Office-reservd" type="button" class="button" data-ocenka="3"
onclick="Ocenka3($(this))">
<span class="btn-text">Удовлетворительно</span>
</button>

But when I made changes in the controller
public function actionOcenka31($id=3)
{

$ocenka = new ocenka();
if($_POST) { echo ''; print_r($_POST); echo ''; }
$ocenka->date_from=new CDbExpression('NOW()');
$ocenkas=OfficeType::model()->findByPk($_POST['Office']['Office_type_id']);

$ocenka->full_name=$ocenkas->name;
//$ocenka->ocenka='3';
$ocenka->ocenka=$_POST['Office']['Ocenka'];
$ocenka->save(); 
Yii::app()->user->setFlash('success',"Спасибо за Вашу оценку. мы учтем это в нашей работе");
$respons3 = array('status' => 'success');
echo json_encode($respons3);

}

then 500 error Undefined index: Ocenka (D:\server\app-new\protected\controllers\SiteController.php:811)
#0 D:\server\app-new\protected\controllers\SiteController.php(811): CWebApplication ->handleError()
#1 unknown(0): SiteController->actionOcenka31()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
polar-bear, 2016-01-25
@asder117

If I understand correctly, then by setting the name and value attributes for the buttons, you can solve the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question