Example Forms

Check the code folder for more comments.

You can't say PHP without mentioning forms. Let cover that!

Read more about this here

The code used in this example can be found here.

How to start

Create a folder named foobar (please use a better name; any name will do) and create folders bin and src. See example below:

+ foobar
    + bin
    + src
        - Main.hx
    - build.hxml

The Main.hx

Open your favorite editor, copy/paste the code and save it in the src folder.

package ;

import php.Lib;
import php.Web;

class Main
{
    function new()
    {
        var params = Web.getParams();
        var nm = params.exists('name') ? params.get('name') : 'none';
        var password = params.exists('password') ? params.get('password') : 'none';

        if ((nm == "none") && (password == "none"))
        {
            Lib.print ('
                <FORM METHOD="post" ACTION="./index.php">
                    Name:  <INPUT TYPE="TEXT" NAME="name" SIZE="25">
                    Password:  <INPUT TYPE="TEXT" NAME="password" SIZE="25">
                    <INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">
                </FORM>
            ');

        } else {
            Lib.print ('Welcome: $nm, with password: $password');
        }
    }

    static public function main()
    {
        var main = new Main();
    }
}

The Haxe build file, build.hxml

There are a lot of different arguments that you are able to pass to the Haxe compiler. These arguments can also be placed into a text file of one per line with the extension hxml. This file can then be passed directly to the Haxe compiler as a build script.

# // build.hxml
-cp src
-main Main
-php bin/www
-dce full

Build PHP with Haxe

To finish and see what we have, build the file and see the result

  1. Open your terminal
  2. cd to the correct folder where you have saved the build.hxml
  3. type haxe build.hxml
  4. press enter

And if everything went according to plan, you should see something simular like this:

You could build everything directly in the terminal.

haxe -cp src -main Main -php bin/www -dce full

Visit http://localhost/?name=Haxe (assuming that your webserver is pointing at the generated folder www). It will display the request parameters for the URL that were sent by the browser. So you must try http://localhost/ just to see the difference!

results matching ""

    No results matching ""