Sunday, February 21, 2016

Manually Setting Up WordPress Project On OpenShift Server Using JBOSS Developer Studio


.
Manually Setting Up WordPress Project On OpenShift Server Using JBOSS Developer Studio

This tutorial demonstrates the step to manually upload WordPress installation files pre-packed with sqlite using JBOSS Developer Studio.

0) Pre-Requisite

0.1) OpenShift account created.
0.2) JBOSS Developer Studio installed.
0.3) PHP Application project created and cloned to JBOSS.
In this example, the cloned path is c:\p\jbossgit\myapp1. You need to know the path so that you would be able to put the wordpress installer file correctly.
0.4) Download WordPress installation file.
Download from here (wordpress422-sqlite.zip)

1) Unzip WordPress Installer and copy to the Project folder

1.1) Use tools like 7Zip to extract the content and copy to the target project folder.
Rename (optional)

2) Commit and Push to Remote Repository

3) Test on Remote Site

.

JBOSS Create PHP Project On RedHat OpenShift Server


.
JBOSS Create PHP Project On RedHat OpenShift Server
Managing application projects on RedHat OpenShift is a lot easier through the JBOSS Developer Studio Application.
This tutorial demonstrates the steps to create a PHP Application Project using JBOSS Developer Studio.
You have to create a free account at https://www.openshift.com first before following the steps.

0) Pre-requisite

0.1) Create OpenShift account.
0.2) Install JDK (This tutorial uses JDK 8 32-bit jdk-8u73-windows-i586.exe for Windows installed as a portable tool) (alternative download site)
0.3) Install JBOSS Developer Studio (This tutorial uses jboss-devstudio-9.0.0.GA-installer-eap.jar package from http://tools.jboss.org/downloads/devstudio/mars/9.0.0.GA.html )
In case you get jboss rejects your Java JDK version, try installing a different version.

1) Create New Project

1.1) Select File/New/OpenShift Application.

1.2) Sign in to OpenShift (must have an account first)

Wait...

1.3) Create SSH Key (if no SSH Keys created yet)

1.3.1) Click New…
1.3.2) Enter details
Wait…
1.3.3) SSH Key created.
Wait...

1.4) Create an application.

Choose your preferred cartridges.
Find PHP.
And then add MySQL with PHPMyAdmin.
Next.
Set up as a new OpenShift project.
Define your Git Clone path.
Wait...
Receive the connection details.
Confirm connection to the OpenShift site.
Connection established. Project has been clone to your local machine.

2) Working with cloned project

2.1) Try editing the file.

Open index.php using internal Text Editor (JBOSS Editor)

2.2) Change the content

e.g. Your PHP changed to MY PHP (on line no. 211)
Initial content:
Edited content:

2.3) Commit

2.4) Push

Wait...
Done.

2.5) See the effects of Commit and Push in web browser

Before Push To Upstream
After Push To Upstream
.

Sunday, February 14, 2016

PHP SQLite Form Send JSON Data


.
PHP SQLite Form Send JSON Data

This tutorial demonstrates the concept of form data serialization.
In order to process the submitted data, the server should be able to recognize the JSON Format and decode it.

0) Starting Up

This tutorial extends from the previous tutorial, http://php-steps.blogspot.my/2016/02/php-and-sqlite-form-validate-submit-and.html .
We will clone the script file submitbuddy.php into submitjsonbuddy.php.
We will use addbuddy.pphp as the target script for JSON data processing since it has been programmed to process JSON data format. (refer tutorial http://php-steps.blogspot.my/2016/02/php-sqlite-json-decode-encode-http.html )

1) Write the script logic

File: submitjsonbuddy.php

<html>
<body>
<form action="addbuddy.php" method="post">
    <!-- when name field changes(onchange) call setJsonData() -->
    <!-- when email field changes(onchange) call setJsonData() -->
    <!-- when name file ischanged(onchange) call previewFile() -->
    <!-- hide photo text field using CSS "style='display:none'"-->
    <!-- hide jsondata text field using attribute "hidden" -->
</form>
<script>
//function to update the image preview
function previewFile() {
    <!-- update the image src with dataurl -->
    <!-- call setJsonData() -->
}
function setJsonData(){
        <!-- call formToJson() -->
    <!-- update hidden field 'jsondata' -->
}
function formToJson(form) {
    <!-- convert all field values (except hidden field) into JSON format -->
    return jsonformat
};
</script>
</body>
</html>

2) Write PHP codes


The form data values are transformed into JSON format and stored in a hidden field “jsondata”.
The image is encoded as base 64 string and stored in a field “photo”. Since the hidden attribute is already used for storing JSON format, the field uses CSS to hide from users ie the users do not have to see the 64 string format.
File: submitjsonbuddy.php

<html>
<body>
<form action="addbuddy.php" method="post">
<p>Name: <input type="text" name="name" required
        onchange="setJsonData()"></p>
<p>E-mail: <input type="email" name="email"
        required placeholder="Enter a valid email address"
        onchange="setJsonData()"></p>
<p>Photo: <input type="text" name="photo" style="display:none;">
<input type="file" onchange="previewFile()">
<p><img src="" height="100" alt="Image preview..."></p>
<input type="hidden" name="jsondata" value="0">
<p><input type="submit" value='Submit' name="submit"></p>
</form>
<script>
//function to update the image preview
function previewFile() {
var preview = document.querySelector('img');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
   reader.addEventListener("load", function () {
     preview.src = reader.result;
    var file = document.querySelector('input[type=file]').files[0];
    document.getElementsByName("photo")[0].value = preview.src;    
    setJsonData();
   }, false);
  if (file) {
    reader.readAsDataURL(file);
  }
}
function setJsonData(){
        var jsondata= formToJson(document.forms[0]);
        document.getElementsByName("jsondata")[0].value = jsondata;
        
}
function formToJson(form) {
    if (!form || form.nodeName !== "FORM") {
        return
    }
    var i, j, q = [];
   
    for (i = form.elements.length - 1; i >= 0; i = i - 1) {
        if (form.elements[i].name === "") {
            continue
        }
        switch (form.elements[i].nodeName) {
            case "INPUT":
                switch (form.elements[i].type) {
                        case "hidden": break;
                    case "text":
                    case "email":
                    case "password":
                    case "button":
                    case "reset":
                    case "submit":
                    q.push('"' + form.elements[i].name + '":"' + (form.elements[i].value) + '"');
                        break;
                    case "checkbox":
                    case "radio":
                        if (form.elements[i].checked) {
                          q.push('"' + form.elements[i].name + '":"' + (form.elements[i].value) + '"')
                        }
                        break;
                    case "file":
                        break
                }
                break;
            case "TEXTAREA":
            q.push('"' + form.elements[i].name + '":"' + (form.elements[i].value) + '"');
                break;
            case "SELECT":
                switch (form.elements[i].type) {
                    case "select-one":
                    q.push('"' + form.elements[i].name + '":"' + (form.elements[i].value) + '"');
                        break;
                    case "select-multiple":
                        for (j = form.elements[i].options.length - 1; j >= 0; j = j - 1) {
                            if (form.elements[i].options[j].selected) {
                              q.push('"' + form.elements[i].name + '":"' + (form.elements[i].options[j].value) + '"')
                            }
                        }
                        break
                }
                break;
            case "BUTTON":
                switch (form.elements[i].type) {
                    case "reset":
                    case "submit":
                    case "button":
                    q.push('"' + form.elements[i].name + '":"' + (form.elements[i].value) + '"');
                        break
                }
                break
        }
    }
    var jsonformat = "{" + q.toString() + "}";
    return jsonformat
};
</script>
</body>
</html>
OUTCOME.

REFERENCES

The codes for function previewFile() were modified from the original script published at https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL 
The codes for function formToJson() were modified from the original script published at http://www.bulgaria-web-developers.com/projects/javascript/serialize/ 

.

Labels