Mark Hendriks
Mark Hendriks

Reputation: 439

If statement cakephp input value and run a javascript code

I am trying to make an if statement with cakePHP but I'm really an amateur. I checked the cookbook and stackoverflow but couldn't find it. (using 2.x.x version of cake)

So what I'm trying to do is:- if the ticket-1-amount is not zero, remove the invisible class.

Something I tried but didn't work:-

if ( $('#ListTypeTicket-1-amount').val() != '' ) {
     $("#invisibleBox").removeClass("invisible");
}

Also tried this:-

 if (empty($this->request->data ['ticket-1-amount'] != 0)) {
        echo '$("#invisibleBox").removeClass("invisible");</script>';
 } ;

My cakePHP form:-

 <?=$this->Form->input(
    'ticket-1-amount',
    array('label' => false, 
    'class' => 'ticket-1-amount', 
    'id' => 'ticket-1-amount')
 ); ?>

This is the actual div

<div id='invisibleBox' class="invisible">
 ..........
</div>

Upvotes: 2

Views: 146

Answers (2)

Rayann Nayran
Rayann Nayran

Reputation: 1135

Create a JavaScript file and put the code below inside it.

App = {
    init: function () {
        this.checkTicketAmount();

    },

    checkTicketAmount: function(){
        if ($('#ticket-1-amount').val() != '0') {
            $("#invisibleBox").removeClass("invisible");
        }
    }
};

$(document).ready(function () {
    App.init();
});

After the page is loaded, jQuery will check the ticket amount value and remove the invisible class, or not.

Upvotes: 1

Manohar Khadka
Manohar Khadka

Reputation: 2195

Keep things simple, Just try:

<div id='invisibleBox' class="<?php if($this->request->data['ticket-1-amount'] == 0) {echo 'invisible'}; ?>">
  ..........
</div>

Upvotes: 1

Related Questions