Viewed   136 times

I have a JS function inside my PHP view file. What I want to do is enable a button when the function verifies that the input does not exist in the database. So here's my function:

var name = 0; 
$("#edit-name").on("change", edit_name);

function edit_name() {
    if ($('#edit-name').val()== ""){
        $('#edit-name').addClass('required');
        $('#warning').html('Please fill required input.');
        name = 0;
    }

    else {
        <?php foreach ($name_list as $list) { ?>
            if ($('#edit-name').val() == '<?php echo $list->name ?>') {
                name = 0;
                $('#warning').html('Name already exists');
            }

            else {
                $('#warning').html('');
                name = 1;
            }
        <?php } ?>
    }

    enableButton();
}

function enableButton(){
    if (name == 1)
        document.getElementById('edit-btn').disabled = false;
    else
        document.getElementById('edit-btn').disabled = true;
}

I don't know what's wrong but even if the inputs are correct, my code is not working properly: It works when it checks if there is an input or not. But nothing happens to the button even if I input a new name. When I input an existing name, the warning doesn't appear.

 Answers

2

Look at your HTML-Code that is generated than you see your mistake.

If you have for example a Array ["name" => "test"], ["name" => "test2"] than you view generates the following HTML:

        if ($('#edit-name').val() == 'test') {
            name = 0;
            $('#warning').html('Name already exists');
        }

        else {
            $('#warning').html('');
            name = 1;
        }


        if ($('#edit-name').val() == 'test1') {
            name = 0;
            $('#warning').html('Name already exists');
        }

        else {
            $('#warning').html('');
            name = 1;
        }

You see "else" is always true.

You need to write:

 <? 
 $stringarray = "";

 foreach($name_list as $list){
   $stringarray .= '"'.$list->name.'",';
 }

 // Del last ,
 $stringarray = substr($stringarray,0,-1);
 ?>

 var names = [<?php echo $stringarray;?>];
 if (names.indexOf($('#edit-name').val()) != -1) {
            name = 0;
            $('#warning').html('Name already exists');
        }else {
            $('#warning').html('');
            name = 1;
        }
Sunday, November 6, 2022
 
radu
 
3

The reason for the "overwriting" is $tempChild = $child;.

That will not deep copy the contents of $child but make both $tempChild and $child point towards the same data structure, which obviously isn't desirable in this case.

You should use clone as in the below example.

$tempChild = clone $child;

  • PHP: Object Cloning - Manual
Saturday, November 12, 2022
 
jojodmo
 
3

change in controller

     $result_criteria = $this->app_model->manualQuery("select b.nik,b.hubkel
      from biodata_karyawan bk left join bpjs b
     on b.nik = bk.nik where bk.status_karyawan = 'Aktif' and " .        $bagianWhere ." order by b.nik");

     $bc['dt_karyawan'] = $this->db->query($result_criteria)->result();

change in views as following

  foreach($dt_karyawan as $row)
 {
 if($row->hubkel=='pegawai')
 {
  $query_pegawai = "select bk.no_kk as 'no_2' ....";
   foreach($query_pegawai->result() as $data1)
   {
    echo '<tr align="center">';                                
    echo '<td>'.$no.'</td>';
    echo '<td>'.$kutip.$data1->no_2.'</td>';
    .
    .
    }
  }
 if($row->hubkel=='istri')
 { 
 .
 .
 .
 }
 }
Tuesday, November 29, 2022
 
jemmons
 
1

First of all check your query using echo $this->db->last_query(); put this code in controller where you write $data['invoices'] and then check its working or not then use below code in your view part.

<div class="form-group">
    <label for="int">Clients Id <?php echo form_error('clients_id') ?></label>
    <select class="form-control select2 select2-hidden-accessible" style="width: 100%;">   
        <center><option selected="selected">--- Select ---</option></center>
        <?php
            foreach ($invoices as $row) 
            {

                echo '<option class="form-control select2" value="'.$row['client_id'].'">'.$row['first_name'].'</option>';
            }
        ?>
    </select>
</div>
<div class="form-group">
    <label for="int">Suppliers Id <?php echo form_error('suppliers_id') ?></label>
    <select class="form-control select2 select2-hidden-accessible" se="width: 100%;">   
        <center><option selected="selected">--- Select ---</option></center>
        <?php
            foreach ($invoices as $row) 
            {

                echo '<option class="form-control select2"  value="'.$row['service_id'].'">'.$row['service_name'].'</option>'
            }
        ?>
    </select>
</div>
Tuesday, September 27, 2022
 
2

you just have issue in your model function foreach loop just replace that with:

$query = $this->db->query('SELECT id, date, text FROM news');
$result = $query->result_array();
return $result;
Saturday, October 15, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :