PHP: Using Netbeans

This entry is part 3 of 54 in the series PHP Tutorial

I just found out that it is relatively easy to enable PHP development in Netbeans.

We will first have to download and install the WAMP server that includes Apache, PHP Engine and MySQL.

Try to install WAMP to C:\wamp if possible.

Download Netbeans if you do not already have it. Always download the All bundle version.

1.  Start Netbeans

2.  Go to Tools->Plugins.  We need to download the PHP engine plugin for Netbeans.

3.  In the Available Plugins, look for PHP and activate it.

php

4. Now we want to the Netbeans to use the PHP engine that you have downloaded together with WAMP.

5. Go to Tools -> Options.

php2

6.  In the PHP 5 Interpreter, browse to the PHP engine.  For example, the path in my notebook is C:\wamp\bin\php\php5.5.12\php.exe.

7.  Now start the WAMP server so that the Apache is started.  Ensure that you get a green icon in the task bar on the bottom right corner.

8. Now we will start a new PHP project.

9.  From Netbeans, go to File -> New Project.

php3

10.  Select PHP and PHP Application

php4

 

11.  In the Project Name, give it a new project name.  In the screenshot, I am using NewPHPProject.

12.  For the Source Folder, we put it under the www folder in WAMP.

13.  So it will be C:\Wamp\www\NewPHPProject.

14.  Click on Next.

php5

 

15.  Since we have the Apache server in WAMP, we use the default setup with Local Web Site.

16. Click Next and then Finish.

17.  A PHP index.php file will be found in C:\wamp\www\newphpproject\

php6

18.  Put an echo line in the code.

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        echo '<p>Hello World</p>';
        ?>
    </body>
</html>

19.  To run the code, just click on the green arrow button in the menu.

20.  We will have the following output in the browser.

php7

 

Posted in

PHP Form Validation: Email addresses

This entry is part 7 of 7 in the series PHP Form Validation

Checking an email address is arguably the most common form validation task for a form.

A common method is to use regular expression.

The regular expression ^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$ matches most common email addresses and fails to match common mistypings of addresses.

Use it with preg_match( ) as shown below.

<?php

// Logic to do the right thing based on
// the hidden submit_check parameter
if ($_POST['submit_check']) {
    // If validate_form( ) returns errors, pass them to show_form( )
    if ($form_errors = validate_form()) {
        show_form($form_errors);
    } else {
        process_form();
    }
} else {
    show_form();
}

// Do something when the form is submitted
function process_form()
{
    print 'Hello, ' . $_POST['email'];
}

// Display the form
function show_form($errors = '')
{
    // If some errors were passed in, print them out
    if ($errors) {
        print 'Please correct these errors: <ul><li>';
        print implode('</li><li>', $errors);
        print '</li></ul>';
    }

    print <<< html_output
<form method='POST' action='$_SERVER[PHP_SELF]'>

Your email: <input type='text' name='email'>
<br>

<input type='submit' value='Say Hello'>
<input type='hidden' name='submit_check' value='1'>
</form>
html_output;
}

// Check the form data
function validate_form()
{
    // Start with an empty array of error messages
    $errors = array();
    // Add an error message if the name is too short
    if (!preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $_POST['email'])) {
        $errors[] = 'Please enter a valid e-mail address';
    }
    // Return the (possibly empty) array of error messages
    return $errors;
}

?>

PHP Form Validation: Number ranges

This entry is part 6 of 7 in the series PHP Form Validation

To check whether a number falls within a certain range, first make sure the input is a number.

if ($_POST['age'] != strval(intval($_POST['age']))) {
    $errors[0] = "Your age must be a number.";
} elseif (($_POST['age'] < 18) || ($_POST['age'] > 65)) {
    $errors[0] = "Your age must be at least 18 and no more than 65.";
}

Then, use an if() statement to test the value of the input.

<?php

// Logic to do the right thing based on
// the hidden submit_check parameter
if ($_POST['submit_check']) {
    // If validate_form( ) returns errors, pass them to show_form( )
    if ($form_errors = validate_form()) {
        show_form($form_errors);
    } else {
        process_form();
    }
} else {
    show_form();
}

// Do something when the form is submitted
function process_form()
{
    print 'Your Age is, ' . $_POST['age'];
}

// Display the form
function show_form($errors = '')
{
    // If some errors were passed in, print them out
    if ($errors) {
        print 'Please correct these errors: <ul><li>';
        print implode('</li><li>', $errors);
        print '</li></ul>';
    }

    print <<< html_output
<form method='POST' action='$_SERVER[PHP_SELF]'>

Your Age: <input type='number' name='age'>
<br>
<input type='submit' value='Say Hello'>
<input type='hidden' name='submit_check' value='1'>
</form>
html_output;
}

// Check the form data
function validate_form()
{
    // Start with an empty array of error messages
    $errors = array();

    if ($_POST['age'] != strval(intval($_POST['age']))) {
        $errors[0] = "Your age must be a number.";
    } elseif (($_POST['age'] < 18) || ($_POST['age'] > 65)) {
        $errors[0] = "Your age must be at least 18 and no more than 65.";
    }
    // Return the (possibly empty) array of error messages
    return $errors;
}

?>

PHP Form Validation: Alter a value in $_Post

This entry is part 5 of 7 in the series PHP Form Validation

If you want to use the whitespace-trimmed value subsequently in your program, alter the value in $_POST and the test the altered value.

$_POST['name'] = trim($_POST['name']);

if (strlen($_POST['name']) == 0) {
    $errors[] = 'Your name is required.';
}

Because $_POST is auto-global, a change to one of its elements inside the validate_form() function persists to other uses of $_POST after the change in another function, such as process_form().

PHP Form Validation: Removing whitespace

This entry is part 4 of 7 in the series PHP Form Validation

When validating elements (particularly string elements), it is often helpful to remove leading and trailing whitespace with the trim() function.

You can combine this with the strlen() test for required elements to disallow an entry of just space characters.

The combination of trim() and strlen() is shown below:

<?php

// Logic to do the right thing based on
// the hidden submit_check parameter
if ($_POST['submit_check']) {
    // If validate_form( ) returns errors, pass them to show_form( )
    if ($form_errors = validate_form()) {
        show_form($form_errors);
    } else {
        process_form();
    }
} else {
    show_form();
}

// Do something when the form is submitted
function process_form()
{
    print 'Hello, ' . $_POST['my_name'];
    print '<br>';
    print 'Your email is, ' . $_POST['email'];
}

// Display the form
function show_form($errors = '')
{
    // If some errors were passed in, print them out
    if ($errors) {
        print 'Please correct these errors: <ul><li>';
        print implode('</li><li>', $errors);
        print '</li></ul>';
    }

    print <<< html_output
<form method='POST' action='$_SERVER[PHP_SELF]'>
Your name: <input type='text' name='my_name'>
<br/>
Your email: <input type='text' name='email'>
<br>
<input type='submit' value='Say Hello'>
<input type='hidden' name='submit_check' value='1'>
</form>
html_output;
}

// Check the form data
function validate_form()
{
    // Start with an empty array of error messages
    $errors = array();

    // Add an error message if the name is too short
    if (strlen(trim($_POST['my_name'])) < 3) {
        $errors[0] = 'Your name must be at least 3 letters long.';
    }
    if (strlen($_POST['email']) == 0) {
        $errors[1] = 'You must enter an email address.';
    }

    // Return the (possibly empty) array of error messages
    return $errors;
}

?>