I hope this hasn't been beaten to death...I looked through the forums and didn't find what I needed.

I have a form where an input (textbox) Change event populates several other inputs within the form via ajax...everything works famously when I comment out the Form::open('edit_product'), Form::button('submit', 'Save Product'),and Form::close() in the view file. Otherwise, with the Form methods enabled the form attempts to submit (edit_product) and everything goes to hades from there. Clearly the form is submitting on change, but that's not the behavior I expected. I expect the fields to simply be populated. Here is an abbreviated example:

Controller (Kohana 3):

public function action_get_data( $base = 0 )
    {
        if ( ! empty($_GET['base']) )
        {
            $base = $_GET['base'];
            unset($_GET['base']);
        }

        // Populate with DB data
        $cnt = 0;
        foreach ( ORM::factory('data')->find_all() as $record )
        {
            $data[$cnt] = array(
                       "price" => $this->round_to(($record->percent/100) * $base, 0.05),
                               );
            $cnt++;
        }

        if ( Request::$is_ajax )
        {
            $this->auto_render=false;
            $this->request->headers['Content-Type'] = 'application/json';
            return $this->request->response = json_encode($data);
        }
        else
        {
            echo "ERROR";
            return;
        }
    }

View::

Form::open('edit_product');
        Form::input('base',array('class' => 'base'));
        Form::input('update[1]',array('class' => 'update'));
        Form::input('update[2]',array('class' => 'update'));
        Form::button('submit', 'Save Product');
    Form::close();

JS::

$(document).ready(function(){
    $("input[type='text'][name='base']").change(function() {
        var base = $(this).val();
        $.ajax({
            type: "GET",
            url: "../../productmanager/get_data",
            data: "base=" + base,
            success: function(data) {
                if (data.length <= 0){
                    alert('A swing and a miss');
                    return false;
                }
                //loop through all items in the JSON array 
                for (var x = 0; x < data.length; x++) {
                    $('input[name=update\\[x\\]]').val(data[x].price);
                }
                return;
            },
            error: function() {
                alert("Didnt work...again!");
                return false;
            }
        });
    });
    });

I am familiar with completely disabling submit by:

JS::

$('form').submit(function(){
        return false;
    });

But this does no good, since I would like to submit the form at some point. Further, the previous disables all the forms with this js file included. Ultimately, I simply want the form to submit ONLY when a 'submit button' is depressed. I assume there is a tchnique to set a flag or some such to get this done, but my JS experience is really cramping my style right now.

Any advice or direction would be greatly appreciated-
UJ

Recommended Answers

All 2 Replies

I wish I could really help but I really don't have that kind of experience I'm the newbie mostly asking here.
But not far I've seen some form jquery plug who disables/enables form field and even toggles. See if it fits you someway even by reorganizing your code behavior.
http://plugins.jquery.com/project/disable

Thanks kidwon I'll give this a look when I get home. For now I set a global flag:

window.submit_ready

as true/false depending on the circumstance, but it's pretty awkward which I try to stay away from. I'll update this thread once I try out the plugin.

Thanks again-
UJ

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.