Simple form
In order to begin targeting PHP with haXe, in this example we're going to creat a log in form.
Let's create our main class: Main.hx
package;
import php.Lib;
import php.Web;
/*Our main class*/
class Main
{
static function main()
{
new Main();
}
public function new()
{
/* Catch all GET and POST parameters in a Hash table*/
var params = Web.getParams();
/*Create a new instance of our Form class*/
var form = new Form();
/*Use check() Form method to verify parameters*/
var result = form.check(params);
/* Display the result.
result is a string variable, if it isn't empty display it, else display back the form
*/
Lib.print((result!='')?result:form.display());
}
}
/*Our Form class*/
class Form
{
public static inline var __login = 'myLogin';
public static inline var __pwd = 'password';
public function new()
{
}
/*Use to display the form*/
public function display()
{
/*HTML form*/
var form = '<div id="form">
<form method="post" action="index.php">
<label for="login">Login</label>
<input name="login" id="login" type="text"/>
<label for="pwd">Password</label>
<input type="password" name="pwd" id="pwd"/>
<input type="submit" value="Send"/>
</form></div>';
/* return the string*/
return form;
}
/*Use to check parameters*/
public function check(params:Hash<String>)
{
/*the function will return a string*/
var retour = '';
/*We first check if parameters exist*/
if(params.exists('login') && params.exists('pwd'))
{
/* Set parameters in variables */
var login = params.get('login');
var pwd = params.get('pwd');
if(login !='' && pwd!='')
{
/* If they match login and password granted*/
if(__login == login && __pwd == pwd)
{
/* we'll return this message*/
retour = 'You are logged in';
}
}
}
return retour;
}
}
Page(s) :
1
Contact
Follow me