user6247099
user6247099

Reputation:

Call JS function inside PHP code

I have a foreach loop that generates an input with type of checkbox with items (from DB). Right now, there is no default checkbox that is checked and every time a user checks the checkbox it calls to JS function.

Now, the clients wants me to check the first checkbox by default. I know how to check it but how do I call the JS function by default?

This is my HTML - the update_addon() is JS func.

<? foreach ($pros as $pro):?>
    <label class="checkbox text-left">
        <input type="checkbox" name="checkbox" 
         onchange="update_addon(<?=$pro['id']?>,this)">
         <?=$pro['name']?> ( <?=$pro['price']?> )
    </label>
<? endforeach; ?>

The update_addon() is an AJAX call.

Upvotes: 1

Views: 79

Answers (2)

user6247099
user6247099

Reputation:

Finally, I did it. did a click() event on js file to click the first checkbox. thanx

Upvotes: 0

Sanan Guliyev
Sanan Guliyev

Reputation: 761

You have a syntax problem. You must put question mark on this part "<?=$pro['id']>".

 <? foreach ($pros as $pro):?>
     <label class="checkbox text-left">
         <input type="checkbox" name="checkbox" 
          onchange="update_addon(<?=$pro['id']?>,this)">
          <?=$pro['name']?> ( <?=$pro['price']?> )
      </label>
  <? endforeach; ?>

Upvotes: 1

Related Questions