最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Yii2: call javascript function with a button - Stack Overflow

programmeradmin4浏览0评论

I want to call a javascript function from a button in php

this is the php code:

//the view
    <?php

    $form = yii\widgets\ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); 

    ?>

 <?=

$form->field($msubs, 'id_subespecifica', ['options' => ['class' => 'col-md-12']])
  ->widget(Select2::classname(),[
        'data' => ArrayHelper::map($vectorConsul,'sesp', 'sesp'),
        'options' => ['id' => 'prueba'],
        ])

  ->label(false);


 ?>


    <button class="btn btn-primary" onclick="myFunction()">Add New</button>


<?php
yii\widgets\ActiveForm::end(); 
?>

And this is the Javascript code:

//Javascript
function myFunction() {
$.ajax({
  url:'partidaasociada/get-linea',
  type:'POST',
  dataType:'json',
  data:{pruebaId:$('#prueba').val()}
  // alert(pruebaId);
});

In the javascript function, i need to send the $('#prueba').val() to a php function in the controller:

//Controller
public function actionGetLinea($pruebaId)
{
    $model = new PartidaAsociada();
    log($pruebaId);
}

But i am getting some errors, i think the button is reloading the whole form and don't recognize the previous data y sent to the form.

Note: The 'alert()' in the javascript function is on mentary because it wont let me use, the form stay loading when i put the alert. thanks beforehand.

I want to call a javascript function from a button in php

this is the php code:

//the view
    <?php

    $form = yii\widgets\ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); 

    ?>

 <?=

$form->field($msubs, 'id_subespecifica', ['options' => ['class' => 'col-md-12']])
  ->widget(Select2::classname(),[
        'data' => ArrayHelper::map($vectorConsul,'sesp', 'sesp'),
        'options' => ['id' => 'prueba'],
        ])

  ->label(false);


 ?>


    <button class="btn btn-primary" onclick="myFunction()">Add New</button>


<?php
yii\widgets\ActiveForm::end(); 
?>

And this is the Javascript code:

//Javascript
function myFunction() {
$.ajax({
  url:'partidaasociada/get-linea',
  type:'POST',
  dataType:'json',
  data:{pruebaId:$('#prueba').val()}
  // alert(pruebaId);
});

In the javascript function, i need to send the $('#prueba').val() to a php function in the controller:

//Controller
public function actionGetLinea($pruebaId)
{
    $model = new PartidaAsociada();
    log($pruebaId);
}

But i am getting some errors, i think the button is reloading the whole form and don't recognize the previous data y sent to the form.

Note: The 'alert()' in the javascript function is on mentary because it wont let me use, the form stay loading when i put the alert. thanks beforehand.

Share Improve this question asked Mar 6, 2015 at 14:49 LuisRoxLuisRox 671 gold badge1 silver badge9 bronze badges 1
  • In the console of google chrome it says that myFunction is not defined as well. – LuisRox Commented Mar 6, 2015 at 15:01
Add a ment  | 

1 Answer 1

Reset to default 4

I think part of the problem is you aren't preventing the default action of a button click.

We can clean things up a bit too. Hit control+shift+i and choose the console tab to see console.log output.

HTML:

<button class="btn btn-primary _addNew">Add New</button>

Javascript:

$('._addNew').on('click', function(event){
    event.preventDefault();
    var data = {};
    data.pruebaId = $('#prueba').val();

    var success = function(data){
       console.log("Success!", data);
    }
    var error = function(data){
       console.log("Error!", data);
    }
    $.ajax({
      url:'partidaasociada/get-linea',
      type:'POST',
      dataType:'json',
      data:data
   }, success, error);
});
发布评论

评论列表(0)

  1. 暂无评论