orionxxi
Newbie | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Все сделал так же, за исключением изменения цвета выводимого сообщения, однако не работает. Код: <?php ini_set('display_errors', 'On'); require ('xajax_core/xajax.inc.php'); function checkName($sName) { if(preg_match('/^([ a-zA-Z]){0,50}$/', $sName)) { $checkVal = "ok!"; if(strlen($sName) < 2) { $checkVal = "too short!"; } if($sName == "") { $checkVal = ""; } } else { if(strlen($sName) > 0) { $checkVal = "not a name"; } } $objResponse = new xajaxResponse(); /* Setup the xajaxResponse object to update the HTML object having ID 'ncheck' */ $objResponse->assign("ncheck", "innerHTML", $checkVal); /* If the value of $checkVal is 'ok!' we make the element green. In other cases we make it red */ if ($checkVal == "ok!") { $objResponse->script("document.getElementById('ncheck').style.color = 'green';"); } else { $objResponse->script("document.getElementById('ncheck').style.color = 'red';"); } /* return the output */ return $objResponse; } function checkAge($nLeeftijd) { /* Check the age. */ $checkVal = "ok!"; if((!is_numeric($nLeeftijd)) && (strlen($nLeeftijd) > 0)) { $checkVal = "not a number"; } else { if($nLeeftijd < 18) { $checkVal = "too young"; } if($nLeeftijd > 65) { $checkVal = "too old"; } if($nLeeftijd == "") { $checkVal = ""; } } $objResponse = new xajaxResponse(); if ($checkVal == "ok!") { $objResponse->script("document.getElementById('lcheck').style.color = 'green';"); } else { $objResponse->script("document.getElementById('lcheck').style.color = 'red';"); } $objResponse->assign("lcheck", "innerHTML", $checkVal); return $objResponse; } $objAjax = new xajax(); $objAjax->registerFunction("checkName"); $objAjax->registerFunction("checkAge"); $objAjax->processRequest(); ?> <head><? $objAjax->printJavascript(); ?></head> <h1>Demo 2: Real-time server-side form validation</h1> <p>When filling out these fields, real-time validation is performed on the server to check whether some simple requirements are met.</p> <h2>Notice: This form is only for people from 18 up to and including 65 years of age.</h2> <div id="div1"> </div> <br/> <div id="aanmeldformulier"> <form action="validate.php" method="post"> <table border="0"> <tr> <td><label for="naam">Name:</label></td> <td><input autocomplete="off" class="textbox" type="text" name="naam" id="naam" onkeyup="xajax_checkName(document.getElementById('naam').value);" /></td> <td><span id="ncheck"></span></td> </tr> <tr> <td><label for="leeftijd">Age:</label></td> <td><input autocomplete="off" class="textbox" type="text" name="leeftijd" id="leeftijd" onkeyup="xajax_checkAge(document.getElementById('leeftijd').value);" /></td> <td><span id="lcheck"></span></td> </tr> </table> </form> </div> <p><a href="demonstratie2.phps">View the source code</a></p> <p id="menulink"><a href="demonstratie.php">» Back to main page</a></p> |
|