Wednesday, January 19, 2022

[FIXED] CakePHP fetching data from form

Issue

I am trying to get the value of input data in a cakephp form so that I can use it in a sql query to auto fill other data. Currently i'm trying to use $this->request->data but it's not showing any results when I change my customer. I want it to display the customer that is selected but currently it's just blank. Not sure if i'm trying to get the data correctly or displaying it incorrectly.

enter image description here My add.ctp

<div class="invoices form large-9 medium-8 columns content">
    <?= $this->Form->create($invoice) ?>
    <fieldset>
        <legend><?= __('Add Invoice') ?></legend>
        <?php

            echo $this->Form->input('customer_id', ['options' => $customers, 'empty' => true,'id'=>'customers']);        ?>

    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>


<script>
            document.getElementById('customers').addEventListener('change',function(){
                alert(<?php echo $this->request->data('customers');
                ?>)
                });

            </script>

Solution

Replace your code with this

document.getElementById('customers').addEventListener('change',function(){
       alert(this.value);
 });


Answered By - Subhankar Mitra

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.