Laravel Form run JS function onsubmit

17,829
<?php echo Form::open(array(null, null, 'onsubmit' => 'myFunction(this); return false;')); ?>
    <?php echo Form::submit('Search', null, array('id'=>'searchbtn', 'class'=>'button radius right')); ?>
<?php echo Form::close(); ?>

Yayy. Now I could use my Enter button!

Share:
17,829
Adrian Bartholomew
Author by

Adrian Bartholomew

Jack of all trades, master of none. But wish I could be both.

Updated on June 26, 2022

Comments

  • Adrian Bartholomew
    Adrian Bartholomew almost 2 years

    I have a function that I want called on submission of my Laravel Form for Ajax purposes. How can I go about this where the page does not submit and reload?

    This works: (but the Enter btn does not execute)

    <?php echo Form::open(); ?>
        <?php echo Form::button('Search', array('id'=>'searchbtn', 'class'=>'button radius right', 'onclick'=>'myFunction(this.form)')); ?>
    <?php echo Form::close(); ?>
    

    This does not:

    <?php echo Form::open(array('onsubmit' => 'myFunction(this)')); ?>
        <?php echo Form::submit('Search', null, array('id'=>'searchbtn', 'class'=>'button radius right')); ?>
    <?php echo Form::close(); ?>
    
  • Connor Leech
    Connor Leech over 7 years
    how do you access the form data? when I log out the value of this I get the html of the form, not the values
  • Zachary Weixelbaum
    Zachary Weixelbaum over 7 years
    You have to say .value, so try this.value or maybe this["fname"].value