Gunners80
Gunners80

Reputation: 1

Error returning value null with using toggle switch in Javascript

<div class="form-group col-3">
  <div class="custom-control custom-switch custom-switch-off-danger custom-switch-on-success">
    <input type="checkbox" class="custom-control-input" name="abonnement_apres_essai" id="abonnement_apres_essai" onclick="myFunction()">
    <label class="custom-control-label" for="abonnement_apres_essai">Abonnement après essai</label>
  </div>
</div>
<script>
  window.onload = function myFunction() {
    const val1 = 1;
    const val2 = 0;
    var abonnementapresessai = document.getElementById("abonnement_apres_essai");
    var afficher_menu_abonnment = document.getElementById("afficher_menu_abonnement");
    if (abonnementapresessai.checked == true) {
      afficher_menu_abonnement.style.display = "block";
      document.getElementById("abonnement_apres_essai").value = val1;
    } else if (abonnementapresessai.checked == false) {
      afficher_menu_abonnement.style.display = "none";
      document.getElementById("abonnement_apres_essai").value = val2;
    }
  }
</script>

Hi guys,

When running the checkbox (toggle switch) is enabled the returned value is 1 is saved in the database,

On the other hand, if the checkbox is deactivated, the value is null and not the value 0 and displays an error in MySQL.

The objective is to return a value 1 or 0 during the checkbox, by default the checkbox is deactivated the returned value 0,and the checkbox is enabled the return value 1

Anyone have any solutions for this problem?

Thanks,

Upvotes: 0

Views: 129

Answers (1)

Gunners80
Gunners80

Reputation: 1

Thanks for your feedback,

I used the controller function for database:

    public function store(Request $request)
{
    //
   $abonnement= new Abonnement;
   $abonnement->client_id =  request('abonnement_select_utilisateur');      
   $abonnement->forfait_id = request('abonnement_select_forfait');
   $abonnement->debut_periode_essai_date = request('abonnement_debut_periode_essai_date');
   $abonnement->fin_periode_essai_date = request('abonnement_fin_periode_essai_date');
   $abonnement->abonnement_apres_essai = request('abonnement_apres_essai');
   $abonnement->date_abonnement = request('abonnement_date');
   $abonnement->date_fin_abonnement  = request('abonnement_fin_date');
   $abonnement->date_desabonnement  = request('abonnement_desabonnement_date');
           
   $abonnement->save();
    
     return redirect("/afficher_abonnement");         
    
}

And i use a route with the POST method

Upvotes: 0

Related Questions