Button submit does not work

Hi to all. i am in a very big trouble and i don’t know what’s going on on my code because my submit button on my view does not go to the actionPrincipale and save the data in the DB.
Can someone help me?

view Principale code

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/** @var yii\web\View $this */
/** @var app\models\Pagina $model */
/** @var ActiveForm $form */
?>

<script>
function visibilita() {
    const element = document.getElementById("pagina-geometria_pezzo");

   if(element.value == 'Cilindro' ){
         document.getElementById("x").style.display = "none";
         document.getElementById("y").style.display = "none";
         document.getElementById("z").style.display = "none";
         document.getElementById("r").style.display = "inline";
         document.getElementById("l").style.display = "inline";
   }
  else if(element.value == 'Parallelepipedo'){
    document.getElementById("x").style.display = "inline";
    document.getElementById("y").style.display = "inline";
    document.getElementById("z").style.display = "inline";
    document.getElementById("r").style.display = "none";
    document.getElementById("l").style.display = "none";
  }
}
</script>

<script>
function calcoloVolume(){
  const volume = document.getElementById("pagina-volume_pezzo");
  const geometria = document.getElementById("pagina-geometria_pezzo");
  raggio = document.getElementById("pagina-r_cilindro");
  altezza = document.getElementById("pagina-l_cilindro");
  x = document.getElementById("pagina-x_parallelepipedo");
  y = document.getElementById("pagina-y_parallelepipedo");
  z = document.getElementById("pagina-z_parallelepipedo");

  if(geometria.value =='Cilindro' && (raggio.value != null && raggio.value != 0) && (altezza.value != null && altezza.value != 0) ){

    volume.value = 3.14159265 * (raggio.value * raggio.value) * altezza.value;
  }
  else if (geometria.value =='Parallelepipedo' && (x.value != null && x.value != 0) && (y.value != null && y.value != 0) && (z.value != null && z.value != 0) ){

    volume.value = x.value * y.value * z.value;
  }
  console.log(geometria.value =='Cilindro' && (raggio.value !=  null && raggio.value !=  0) && (altezza.value !=  null && altezza.value != 0))
  console.log(document.getElementById("r").value);
}
</script>

<script>
function calcoloSuperficie(){
  const geometria = document.getElementById("pagina-geometria_pezzo");
  const superficie = document.getElementById("pagina-superficie_pezzo");
  raggio = document.getElementById("pagina-r_cilindro");
  altezza = document.getElementById("pagina-l_cilindro");
  x = document.getElementById("pagina-x_parallelepipedo");
  y = document.getElementById("pagina-y_parallelepipedo");
  z = document.getElementById("pagina-z_parallelepipedo");

  if(geometria.value =='Cilindro' && (raggio.value != null && raggio.value != 0) && (altezza.value != null && altezza.value != 0) ){

    superficie.value = 2 * 3.14159265 * raggio.value * altezza.value + 2 * 3.14159265 * ( raggio.value * raggio.value) ;
  }
  else if (geometria.value =='Parallelepipedo' && (x.value != null && x.value != 0) && (y.value != null && y.value != 0) && (z.value != null && z.value != 0) ){

    superficie.value = (x.value * y.value) * 2 +  (x.value * z.value) *2 + (y.value * z.value) * 2 ;
  }
}
</script>
<script>
function calcoloPezzoGrezzo(){
  volume = document.getElementById("pagina-volume_pezzo");
  pesoSpecifico = document.getElementById("pagina-peso_specifico");
  costoAlKg = document.getElementById("pagina-costo_materiale_kg");
  costoPezzoGrezzo = document.getElementById("pagina-costo_pezzo_grezzo");
  if (volume.value != null && pesoSpecifico.value != null && costoAlKg.value != null && costoPezzoGrezzo.value != null){
          costoPezzoGrezzo.value = (volume.value / 1000000000) * pesoSpecifico.value * costoAlKg.value;
          costoPezzoGrezzo.style.color = "#00af12";
}
}
</script>
<script>
function calcoloPezzoSingolo(){
  const costoTrattamento = parseFloat(document.getElementById("pagina-costo_trattamento").value);
  costoPezzoGrezzo = parseFloat(document.getElementById("pagina-costo_pezzo_grezzo").value);
  const costoLavorazione = parseFloat(document.getElementById("pagina-costo_lavorazione").value);
  costoPezzoSingolo = document.getElementById("pagina-costo_pezzo_singolo");
  costoPezziTotale = document.getElementById('pagina-costo_pezzi_totale');
  quantita = document.getElementById('pagina-quantita');

  costoPezzoSingolo.value = costoTrattamento + costoPezzoGrezzo + costoLavorazione;
  costoPezzoSingolo.style.color = "#00af12";

   console.log(costoPezzoSingolo.value);
}
</script>
<script>
function calcoloPezziTotale(){
  costoPezzoSingolo = document.getElementById("pagina-costo_pezzo_singolo");
  quantita = document.getElementById('pagina-quantita');
  costoPezziTotale = document.getElementById('pagina-costo_pezzi_totale');

  costoPezziTotale.value = costoPezzoSingolo.value * quantita.value;
  costoPezziTotale.style.color = "#00af12";

   console.log(costoPezzoSingolo.value);
}
</script>
<div class="principale">

    <?php $form = ActiveForm::begin([/*'action' => ['site/principale'] ,*/ 'method'=>'post']); ?>
    <div class="row">
      <div class="col-md-4">    <?= $form->field($model, 'Disegno') ?></div>
      <div class="col-md-4" onchange ="calcoloPezziTotale()">    <?= $form->field($model, 'Quantita') ?></div>
      <div class="col-md-4">    <?= $form->field($model, 'Descrizione') ?></div>
    </div>

  <div class="row">
       <div class="col-md-4" onchange="visibilita()" id ="prova">   <?= $form->field($model, 'Geometria_Pezzo')->
                                                DropDownList(
                                                            [
                                                              'Cilindro' => 'Cilindro',
                                                              'Parallelepipedo' => 'Parallelepipedo'
                                                            ],['prompt'=>'Seleziona geometria']); ?> </div>

    <div class="col-md-1" id ="x" onchange ="calcoloVolume();calcoloSuperficie()">    <?= $form->field($model, 'x_Parallelepipedo')->hint('[mm]') ?> </div>
    <div class="col-md-1" id ="y" onchange ="calcoloVolume();calcoloSuperficie()">    <?= $form->field($model, 'y_Parallelepipedo')->hint('[mm]') ?> </div>
    <div class="col-md-1" id ="z" onchange ="calcoloVolume();calcoloSuperficie()">    <?= $form->field($model, 'z_Parallelepipedo')->hint('[mm]') ?> </div>
    <div class="col-md-1" id ="r" onchange ="calcoloVolume();calcoloSuperficie()">    <?= $form->field($model, 'L_Cilindro')->hint('[mm]') ?> </div>
    <div class="col-md-1" id ="l" onchange ="calcoloVolume();calcoloSuperficie()">    <?= $form->field($model, 'R_Cilindro')->hint('[mm]') ?> </div>
    <div class="col-md-2" > <?= $form->field($model, 'Volume_Pezzo')->hint('[mm^3]') ?> </div>
    <div class="col-md-1" > <?= $form->field($model, 'Superficie_Pezzo')->hint('[mm^2]') ?> </div>
  </div>
      <div class ="row">
        <div class="col-md-4">  <?= $form->field($model, 'Materiale') ?> </div>
        <div class="col-md-4" onchange ="calcoloPezzoGrezzo()">  <?= $form->field($model, 'Peso_Specifico')->hint('[Kg/m^3]') ?> </div>
        <div class="col-md-4" onchange ="calcoloPezzoGrezzo()">  <?= $form->field($model, 'Costo_Materiale_Kg') ?> </div>
      </div>
      <div class ="row">
        <div class="col-md-12" onchange ="calcoloPezzoSingolo()">  <?= $form->field($model, 'Costo_Pezzo_Grezzo') ?> </div>
      </div>
      <div class ="row">
        <div class="col-md-6">  <?= $form->field($model, 'Trattamento') ?> </div>
        <div class="col-md-6" onchange ="calcoloPezzoSingolo()">  <?= $form->field($model, 'Costo_Trattamento') ?> </div>
      </div>

      <div class ="row">
        <div class="col-md-12" onchange ="calcoloPezzoSingolo()">  <?= $form->field($model, 'Costo_Lavorazione') ?> </div>
      </div>

      <div class ="row">
        <div class="col-md-12" onmouseover = "calcoloPezziTotale()">  <?= $form->field($model, 'Costo_Pezzo_Singolo') ?> </div>
      </div>

        <?= $form->field($model, 'Costo_Pezzi_Totale') ?>


        <?= Html::submitButton('vai', ['class' => 'btn btn-primary']) ?>

    <?php ActiveForm::end(); ?>

</div><!-- principale -->

controller code:

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Login action.
     *
     * @return Response|string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }

        $model->password = '';
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return Response
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return Response|string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }

    public function actionPrincipale()
{
    $model = new \app\models\Pagina();

   if ($this->request->isPost && $model->load($this->request->post()))  {
        if ($model->validate()) {
            $model->save();
        }
    }

    return $this->render('principale', [
        'model' => $model,
    ]);
}
}

Did you reached this if? I suggest to check output of $model->load($this->request->post()) after you submit the form.

1 Like

Thanks for the reply. My program reach the if but it doesn’t enter in that because the condition is always in false. I don’t know why i have the method post on active form tag…

i have resolve it. Thanks for the help!

1 Like