1: <?php
2:
3: /**
4: * Description of form
5: *
6: * @author SARTAJ
7: */
8:
9: namespace Sphp\Comp\Form{
10:
11: class HTMLForm extends \Sphp\tools\Component {
12: private $recID = 'txtid2';
13: private $txtid2 = '';
14: private $onvalidation = '';
15: private $blnajax = false;
16: private $blnsocket = false;
17: private $socketctrl = "index";
18: private $socketevt = "";
19: private $socketevtp = "";
20: private $ajax = null;
21: private $target = "";
22:
23: protected function genhelpPropList() {
24: parent::genhelpPropList();
25: $this->addHelpPropFunList('setAjax','Set Form post as AJAX','','');
26: $this->addHelpPropFunList('setOnValidation','JS Function call on form validation and return false stop form to submit','','$jscode');
27: }
28: // call outside to load js lib for need form use via ajax lator
29: public function fu_setupJSLib() {
30: addFileLink($this->myrespath . "/assets/validation.js", true);
31: addFileLink($this->myrespath . "/assets/jquery.form.js", true);
32: }
33: protected function oninit() {
34: $this->tagName = "form";
35: if(!$this->element->hasAttribute("name")){
36: $this->HTMLName = $this->name;
37: }else{
38: $this->HTMLName = $this->getAttribute("name");
39: }
40: addFileLink($this->myrespath . "/assets/validation.js", true);
41: $this->recID = $this->HTMLName . 're';
42: if (\SphpBase::sphp_request()->request($this->recID) !== "") {
43: $this->txtid2 = urldecode(\SphpBase::sphp_request()->request($this->recID));
44: if(isSecureVal($this->txtid2)){
45: $this->txtid2 = secure2Val($this->txtid2);
46: if($this->txtid2 == "") setErr("App", "Invalid Form Data");
47: }
48: }
49: }
50:
51:
52: public function fu_setAjax() {
53: $this->blnajax = true;
54: addFileLink($this->myrespath . "/assets/jquery.form.js", true);
55: }
56:
57: public function fu_setSocket($ctrl,$evt="",$evtp="") {
58: $this->blnsocket = true;
59: $this->socketctrl = $ctrl;
60: $this->socketevt = $evt;
61: $this->socketevtp = $evtp;
62: }
63:
64: public function fu_setAjaxTarget($val) {
65: $this->target = $val;
66: }
67:
68: public function getRecID() {
69: return $this->txtid2;
70: }
71:
72: public function setRecID($v) {
73: $this->txtid2 = $v;
74: }
75:
76: public function fu_setOnValidation($val) {
77: $this->onvalidation = $val;
78: }
79: protected function onprejsrender() {
80: $valdx = "";
81: if ($this->blnajax) {
82: \SphpBase::JSServer()->getAJAX();
83: if ($this->target == '') {
84: $divt = '<div style="visibility:hidden;"><img src="' . \SphpBase::sphp_settings()->res_path . '/'. \SphpBase::sphp_settings()->slib_version . '/comp/server/res/ajax-loader.gif" />' . "</div><div id=\"" . $this->name . "res\"></div>";
85: $this->target = $this->name . "res";
86: } else {
87: $divt = "";
88: }
89:
90: $this->setPreTag($divt);
91: $subcode = "$('#{$this->name}').find(\"input[type='submit']\").attr('disabled',true);
92: $('#" . $this->name . "').ajaxSubmit({
93: dataType: 'text',
94: success: function(html) {
95: if(document.getElementById('ajax_loader')!=null){
96: document.getElementById('ajax_loader').style.visibility = 'hidden';
97: }
98: $('#{$this->name}').find(\"input[type='submit']\").attr('disabled',false);
99: sartajpro(html,function(res){});
100: }
101: });
102: ";
103: //$("#testform").serialize())
104: /*
105: if(!isset($this->parameterA['action'])){
106: $subcode ="
107: getURL('".getThisGateURL('',true)."',$('#$this->name').serialize());
108: ";
109: }else{
110: $subcode ="
111: getURL('".$this->parameterA['action']."',$('#$this->name').serialize());
112: ";
113: }
114: *
115: */
116:
117: //addHeaderJSFunctionCode('ready', $this->name, "$('#" . $this->name . "').ajaxForm(); ");
118: }else if($this->blnsocket){
119: \SphpBase::JSServer()->getAJAX();
120: $subcode = " frontobj.getSphpSocket(function(wsobj1){
121: var formData = $('#" . $this->name . "').serializeAssoc();
122: delete formData['sphpajax'];
123: wsobj1.callProcessNativeGate('{$this->socketctrl}','{$this->socketevt}','{$this->socketevtp}',formData);
124: });";
125:
126: } else {
127: $subcode = "
128: if(val==''){
129: objc1.submit();
130: }else{
131: objc1.action=val;
132: objc1.submit();
133: }
134: ";
135: }
136: if ($this->onvalidation != '') {
137: $valdx = "if(blnSubmit==true){
138: blnSubmit = " . $this->onvalidation . ";
139: }
140: ";
141: }
142: addHeaderJSCode($this->name . "csubmit2", 'var ' . $this->name . 'st1 = true;$("#'. $this->name .'").on("keydown",clearValidationError);');
143: addHeaderJSFunction($this->name . "_submit2", "function " . $this->name . "_submit2(val){ var vt = false;"," if({$this->name}st1){ {$this->name}st1 = false; vt = " . $this->name . "_submit(val); } setTimeout(function(){{$this->name}st1 = true;},4000); return vt;}");
144: addHeaderJSFunction($this->name . "_submit", "function " . $this->name . "_submit(val){
145: var blnSubmit = true ;
146: var ctlReq = Array();
147: var ctlEmail = Array();
148: var ctlNums = Array();
149: var ctlMins = Array();
150: var ctlMax = Array();
151: clearValidationError('');
152: ", "
153:
154: if(blnSubmit==true && checkTextEmpty(ctlReq)==false){
155: blnSubmit = false ;
156: }
157: if(blnSubmit==true && checkmax(ctlMax)==false){
158: blnSubmit = false ;
159: }
160: if(blnSubmit==true && checkmin(ctlMins)==false){
161: blnSubmit = false ;
162: }
163: if(blnSubmit==true && checkemails(ctlEmail)==false){
164: blnSubmit = false ;
165: }
166: if(blnSubmit==true && checknums(ctlNums)==false){
167: blnSubmit = false ;
168: }
169: $valdx
170: if(blnSubmit==true ){
171: var objc1 = document.getElementById('" . $this->name . "');
172: $subcode
173: }
174: return false;
175: }");
176: }
177:
178: protected function onrender() {
179: $this->setAttributeDefault("role","form");
180: $this->setAttributeDefault("method","post");
181: $this->setAttributeDefault("enctype","multipart/form-data");
182: $this->setAttributeDefault("action",getThisGateURL("",false,".app"));
183: //$this->setAttributeDefault('onsubmit', "var vt = " . $this->name . "_submit2('');return false;");
184: if(!isset($this->onsubmit)){
185: addHeaderJSFunctionCode("ready", $this->name .'rd1', "$('#" . $this->name . "').on('submit',function(e){e.preventDefault(); var vt = " . $this->name . "_submit2(''); return vt;}); ");
186: }
187: $v1 = "";
188: if($this->txtid2 != ""){
189: $v1 = 't';
190: $this->txtid2 = urlencode(val2Secure($this->txtid2));
191: }
192: $hdn = "<input type=\"hidden\" name=\"txtid\" value=\"" . $v1 . "\" />";
193: $hdn .= "<input type=\"hidden\" name=\"" . $this->recID . "\" value=\"" . $this->txtid2 . "\" />";
194: if($this->blnajax) $hdn .= "<input type=\"hidden\" name=\"sphpajax\" value=\"1\" />";
195: $this->appendHTML($hdn);
196: $parenttag = $this->wrapTag("div");
197: $parenttag->setAttribute("id","wrp" . $this->name);
198: $parenttag->setAttribute("class", "px-4 py-4");
199: }
200:
201: }
202:
203: }
204: