Sunday, January 31, 2016

How to Create a Basic PHP Form using Classes

We use forms in the web all the time.

This is how we can create a basic form using PHP.

We need to create a class describing the form first.

In a "Form.class" file we can add the following code:

<?php
class Form
{
private $fields = array();
private $actionValue;
private $submit = "Submit Form";
private $Nfields = 0;

function __constructor($actionValue,$submit)
{
$this->actionValue = $actionValue;
$this->submit = $submit;
}

function displayForm()
{
echo "\n<form action='($this->actionValue)' method='POST'>\n";
for($j=1;$j<=sizeof($this->fields);$j++)
{
echo "<p style='clear: left; margin: 0; padding: 0; padding-top: 5px'>\n";
echo "<label style='float: left; width: 20%;'> {$this->fields[$j-1]['label']}: </label>\n";
echo "<input style='width: 200px' type='text' name='{$this->fields[$j-1]['name']}'></p>\n";
}
echo "<input type='submit' value='{$this->submit}' style='margin-left: 25%; margin-top: 10px'>\n";
echo "</form";
}

function addField($name,$label)
{
$this->fields[$this->Nfields]['name'] = $name;
$this->fields[$this->Nfields]['label'] = $label;
$this->Nfields = $this->Nfields + 1;
}
}
?>

To use the form defined above we need to create a php file as follows:

<?php
require_once("Form.class");
echo "<html><head><title>Phone form</title></head><body>";
$phone_form = new Form("process.php","Submit Phone");
$phone_form->addField("first_name", "First Name");
$phone_form->addField("last_name", "Last Name");
$phone_form->addField("phone", "Phone");
echo "<h3>Please fill out the following form:</h3>";
$phone_form->displayForm();
echo "</body></html>";
?>

After adding this to a file named "formexample.php, make sure that you add the "Form.class" file in to the same location.

Run the "form example.php" file and check your newly created form.

Write Data to a File using PHP Functions

A method to write data to a file saved in the local machine using PHP functions.

<?php
//write to file | a-append w-overwrite
function writeToFile($filename,$datatosave,$mode)
{
if (!file_exists($filename))
{
echo "file not found";
echo "<br/>";
}
else
{
$fh = fopen($filename,$mode) or die("Can't open file");
fwrite($fh,$datatosave);
fclose($fh);
echo "File written successfully.";
}
return;
}

echo "<br/>";
writeToFile("test.txt","$today\n","a");
echo "<br/>";

Read Data from a File using PHP

A method to read data from a file using php functions.

<?php
//read from file
function readFromFile($filename)
{
if (!file_exists($filename))
{
echo "file not found";
echo "<br/>";
}
else
{
$fh = fopen($filename,"r") or die("Can't open file");
while(!feof($fh))
{
$line = fgets($fh);
echo "$line<br/>";
}
fclose($fh);
echo "<br/>";
echo "File read successfully.";
}
return;

}

echo "<br/>";
readFromFile("test.txt");
echo "<br/>";
?>

Change Date/Time Zone for PHP Development in a MAMP Installation

We can set the Date/Time zone correctly after a MAMP installation by changing the date.timezone value of the php configuration file.

  • "/MACINTOSH/User/Applications/MAMP/conf/php[version]/php.ini"
  • date.timezone="Asia/Maldives"

Where to Find PHP Errors when using MAMP

When using a MAMP installation as your PHP development environment the errors are not displayed in the browser web page itself.

Instead they are stored in the following location:

  • "/MACINTOSH/User/Applications/MAMP/logs/php_error.log"
You can also find the error logs for MySQL and Apache in the same location in files named "mysql_error_log.err" and "apache_error.log" respectively.

Note: You can also enable error display on the browser webpage itself via changing the following setting in the PHP configuration file, but it is highly not recommended as it displays sensitive information to visitors of the site.
  • /MACINTOSH/User/Applications/MAMP/conf/php[version]/php.ini"
  • find "display_errors" uncommented entry and change it from "Off" to "On"

How to Connect to a MySQL DB and Get Data in PHP using mysqli Syntax

<?php

$dburl = "localhost";
$dbuname = "USERNAME";
$dbpwd = "PASSWORD";
$dbname = "databasename";

$mysqli = new mysqli($dburl, $dbuname, $dbpwd, $dbname);

$query = 'SELECT name, price FROM Products ORDER BY name';

if ($mysqli->errno)
{
printf("Unable to connect to database:<br/> %s", $mysqli->error);
exit();
}
else
{
$result = $mysqli->query($query, MYSQLI_STORE_RESULT);
while(list($name, $price)= $result->fetch_row())
{
printf("Price of %s is: \$%s <br/>", $name, $price);
}
printf("<br>Task Completed.");
}
?>

Intro

I started my professional life as a .NET developer and I worked with SharePoint technologies at the time. I shared some of the things I found interesting via my blogs Points of SharePoint on Blogspot and Points of Sharepoint on Wordpress.

At the time it was a nice experience to share solutions some of the issues I faced while researching and learning in one place so that I could refer it quickly and also someone researching on the same issue could find it too.

I have had a strong interest on the web technologies and the mobile technologies alike for a long time. And decided to share some of the things that I do to keep updated with the technologies here so that I can refer it whenever a need arises as well it would help someone facing the same issues.

We will see how beautiful the mobile and the web is.

See you soon.