I’m a beginner when it comes to zend framework.
I created a form with a submit button, using zend_form, and zend_form_element_submit. Upon clicking submit, the code performs data manipulation based on the input. If no input is keyed in, nothing happens.
When I click the submit button, it reloads my web page even though there are no changes.
Any way I can prevent that page load? Could I use a zend_form_element_button that would trigger an event? how would I capture it?
Any help will be mostly appreciated!
Thank you.
A submit button will always cause the form to submit which generally results in a page being reloaded whether or not the data in any of the form elements are “correct” or not.
Using Zend Framework, you could add a JavaScript
onsubmitevent to your form that could inspect the form elements and decide if the form should be submitted or not. Or you could use Ajax to submit the form which wouldn’t result in the page being reloaded.Here is an example of using
onsubmit. You would create your form on the controller, assign it to the view, and then in your view, add theonsubmitattribute and relevant code.view.phtml
You will have to come up with the logic for
form_passes_validation, but ifonsubmitreturns false, then the form will not be sent.Keep in mind, PHP is all server side. You can’t do any PHP processing to determine if the form should send, this all has to be client side, or you will have to live with your form reloading the page even if no data is entered.