Home » Jquery » javascript – Is there any missing functions in this code to pop-up the materialize data?

javascript – Is there any missing functions in this code to pop-up the materialize data?

Posted by: admin February 22, 2020 Leave a comment

Questions:

The materialize pop-up does not appear.

$(document).ready(function() {
  $('#cdn_customer_id').on('click', function() {
    //alert("hi00");
    var countCust = '<?php count($customers) ?>';
    if (countCust < 1) {
      Materialize.toast('Add a Sales/Client Master', 3000, 'red rounded')
    }
  });
<select class="js-example-basic-single" name="cust_id" id="cdn_customer_id">
  <option value="">SELECT CLIENT</option>
  <?php foreach ($customers as $cus) { ?>
    <option value="<?php echo $cus->cust_id; ?>" <?php if(!empty($inv)){if($inv[0]->cust_id!=0){ echo ($inv[0]->cust_id==$cus->cust_id)?'selected':''; }} ?> >
    <?php echo strtoupper($cus->cust_name); ?>
  </option>
  <?php } ?>
</select>
How to&Answer:

Try the following

There’s two changes I’d make to your code from what I can see, I don’t know if it will fix everything but from the information you’ve given us, this is the best I can come up with.


First Issue:

You are setting an on click listener on a drop-down, surely you want this to fire when someone selects an option, not when they click on the select, I’d change the following line:

$('#cdn_customer_id').on('click', function() {

To a change listener by simply changing the word click:

$('#cdn_customer_id').on('change', function() {

Second Issue:

I don’t think the customer count is being set properly, you haven’t used echo to output a value, it’s not best practice to place a PHP value in JavaScript but seeing as you’ve done this and I have limited information available to change it, we’ll leave it as it is but actually set the value properly by changing the following line:

var countCust = '<?php count($customers) ?>';

To echo the value using the short-hand version, providing you are using PHP 5.4 or later, this shouldn’t be an issue:

var countCust = '<?=count($customers) ?>';

However, if for some reason you’re using an earlier version of PHP I might as well include the longer version:

var countCust = '<?php echo count($customers) ?>';

I’d say if you make these two changes, see if anything improves and get back to me with your feedback and hopefully solve the next issue if there is one.

Answer:

It looks like this is a materializecss specific question.

Your markup looks wrong for one. The docs use this structure:

M.toast({html:'Add a Sales/Client Master'})

Codepen here.

So an object inside the toast. Not sure why you are passing other style data in.

Another way is to declare the html first, then pass it in. From the docs:

 var toastHTML = '<span>I am toast content</span><button class="btn-flat toast-action">Undo</button>';
  M.toast({html: toastHTML});

https://materializecss.com/toasts.html