User Tools

Site Tools


form_class

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
form_class [2019/10/22 20:02] sahlform_class [2021/03/11 19:44] (current) – [Easy forms] Adjusted file name sahl
Line 52: Line 52:
 In order to provide an easier way to design forms, one can prepare a special html file In order to provide an easier way to design forms, one can prepare a special html file
  
-<code html testform.frm>+<code html testform.form>
 <h2>Please fill this form</h2> <h2>Please fill this form</h2>
 <text label="Type something" name="field_title"> <text label="Type something" name="field_title">
Line 61: Line 61:
  
 <code php> <code php>
-$form = new Form('form_id', 'testform.frm');+$form = new Form('form_id', 'testform.form');
 </code> </code>
  
Line 96: Line 96:
  
 The validation function is passed the form, and should return true if everything is OK, otherwise it should trigger an error on the appropriate form field and return false. The validation function is passed the form, and should return true if everything is OK, otherwise it should trigger an error on the appropriate form field and return false.
 +
 +===== Handling server side errors, when posting with javascript =====
 +
 +When doing a normal post, server side errors are automatic added to the form, but when posting using javascript, you need to pass server side errors back to the form. Platform provides easy functions to do that.
 +
 +<code javascript frontend.js>
 +$.post('backend.php', form.serialize(), function(data) {
 +  if (data.status == 1) {
 +    // Everything is fine
 +  } else {
 +    // There was backend errors. Add them to the form.
 +    add_errors_to_form(form, data.errors);
 +  }
 +}, 'json');
 +</code>
 +
 +<code php backend.php>
 +if ($form->validate()) $result = array('status' => 1);
 +else $result = array('status' => 0, 'errors' => $form->getAllErrors());
 +echo json_encode($result);
 +</code>
form_class.1571774555.txt.gz · Last modified: 2019/10/22 20:02 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki