5

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(); ?>
2
  • Does your myFunction return false (or prevent event propagation in some other way)?
    – raina77ow
    Aug 12, 2013 at 3:50
  • Very good observation. There were a cpl other gotchas but I did get it to work. Thanks! Aug 12, 2013 at 3:55

1 Answer 1

5
<?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!

2
  • 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 Jan 9, 2017 at 7:34
  • You have to say .value, so try this.value or maybe this["fname"].value Feb 14, 2017 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.