September 22, 201313 yr Hi there, I found this script for validating forms with Javascript and PHP. http://livevalidationphp.leihitu.nl/index.php?page=home It all works perfectly fine, apart from the fact I cannot see where to actually send the information in the form... Here's a basic example of a working form: <?php include("livevalidationphp.class.php"); // the LiveValidationPHP scripts include("rules.inc.php"); // hold the rules $html=""; $frmTestLive=new LiveValidationMassValidatePHP("frmTestLive",$_POST); $frmTestLive->addRules($formRules["frmTestLive"]); $html=$frmTestLive->generateAll(); // check for a post action $frmTestErrors=array(); if(isSet($_POST["action"])) { if($_POST["action"]=="send_test_form") { $frmTestErrors=$frmTestLive->validate(); } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>LiveValidationPHP | Example 1</title> <link href="livevalidation/livevalidation.css" rel="stylesheet" type="text/css" media="screen"> <script language="javascript" type="text/javascript" src="livevalidation/livevalidation.js"></script> </head> <body> <h1>Example 1</h1> <?php if(count($frmTestErrors)>0) { print "<strong>You have errors:</strong>"; print "<pre>"; print_r($frmTestErrors); print "</pre>"; } ?> <form name='frmTest' id='frmTest' action='' method='post'> <input type='hidden' name='action' value='send_test_form'> <input type='text' name='element_1' id='element_1' value='' placeholder='Name'> <input type='text' name='element_2' id='element_2' value='' placeholder='Email'> <input type='text' name='element_3' id='element_3' value='' placeholder='Message'> <input type='submit' value='go'> </form> <script> <?php print $html; ?> </script> </body> </html> When I've done this before I've always used the action'' part on the form to link to a contact.php or something, but none of the examples are using this, just wondering if anyone knows how to simply send the form once it's valid? The only other relevant file I can think of would be this: http://pastebin.com/raw.php?i=24vA5RZh The others are just one PHP file for the error array messages, and a second for the javascript validation.
September 25, 201313 yr It post to page itself. so if POST is isset it will call validate() function if(isSet($_POST["action"])) { if($_POST["action"]=="send_test_form") { $frmTestErrors=$frmTestLive->validate(); } }
September 26, 201313 yr Yup you should check your validate(); function, if you wanna show a message after sending just put make something like this and test it out if you see a message: if(isSet($_POST["action"])) { if($_POST["action"]=="send_test_form") { $frmTestErrors=$frmTestLive->validate(); echo $frmTestErrors; echo "Thnx for sending the form!"; } }
September 26, 201313 yr Author Cheers guys, Yup you should check your validate(); function, if you wanna show a message after sending just put make something like this and test it out if you see a message: if(isSet($_POST["action"])) { if($_POST["action"]=="send_test_form") { $frmTestErrors=$frmTestLive->validate(); echo $frmTestErrors; echo "Thnx for sending the form!"; } } This works fine, however there seems to be more than one validation function. I actually checked this and couldn't really see where to tell it to send... function validate() { $payload=array(); $hasErrors=false; $count=count($this->rules); for($i=0;$i<$count;$i++) { $currentRule=$this->rules[$i]; if($resultArray=$currentRule->validate()) { if($resultArray["success"]==false) { $hasErrors=true; $this->errors[]=$resultArray["payload"]; } } } return $this->errors; } function validate() { $payload=array(); $payload["success"]=false; if($this->method!="") { $actionFunction=$this->method; if(method_exists($this,$actionFunction)) { if($this->$actionFunction()==false) { $payload["payload"]="[" . $this->display . "] " . $this->args["failureMessage"]; } else { $payload["success"]=true; unset($payload["payload"]); } } } return $payload; } function validate() { $errors=array(); $countElements=count($this->fields); for($q=0;$q<$countElements;$q++) { $currentElement=$this->fields[$q]; $errors=array_merge($currentElement->validate(),$errors); } return $errors; } http://pastebin.com/raw.php?i=24vA5RZhThere's some generate functions at the bottom which look more like that I'm after, but I'm still a bit lost to be fair. Any ideas?
Create an account or sign in to comment