Wednesday, December 10, 2014

Converting data submitted in post to a json file at server


Updating dropdown list

[
Example of refreshing on change in drop down list
]
var select = registry.byId('visitorname');
                        select.on('change', function(evt) {
                        visitorformrefresh();
                        visitorpsyrefresh();

                        });
<select name=visitorname id=visitorname data-dojo-type="dijit/form/Select" autocomplete=true >
                                    <option value=Guru>Guru</option>
                                    <option value=Ravindra>Ravindra</option>
                                    <option value=Harsha>Harsha</option>

</select>

Converting data submitted in post to a json file at server

[

The data received in backend is assigned to array and stored in json file

Example

        $jsonprofile = json_encode($profile);

        file_put_contents('profile.json', $jsonprofile);
]

public function actionregister() {

        $name = "";

        $description = "";

 

        if (isset($_POST['name'])) {

            $name = $_POST['name'];

        }

        if (isset($_POST['description'])) {

            $description = $_POST['description'];

        }

 

        $profile = array(

             'name' => $name,

             'description' => $description,

        );

 

        $jsonprofile = json_encode($profile);
       file_put_contents('profile.json', $jsonprofile);
       $this->actionprocessowner();

        // echo "Data saved";

    }

 

Using post to submit a form

Using post to submit a form

[
Example of submitting a form on click of a button.
Form name is regForm.
]

on(dom.byId('button0'),'click',function(){
                                     request.post("http://localhost/yii/g3labs/?r=Group/register",{data:domForm.toObject('regForm')}).then(

                                                function(data){

                                                alert(data);

                                    query("#resultDiv").html(data).style({color:'green',fontWeight:'bold',fontSize:'20px',border:'10 dashed red'});

                                                },

                                                function(err){

                                    //          alert('err');

                                                            query("#resultDiv").text(err.message).style({color:'red',fontWeight:'bold',fontSize:'26px'});          

                                                }

                                    );
                       //          registry.byId("dialog0").hide();
                        ownerformrefresh();  
                         ownerpsyrefresh();

                        });

 

Trigger when button clicked, Getting json data in post call

Trigger when a button is clicked.
[
Example of triggering function, on clicking a button
]

on(dom.byId('peerseen'),'click',function(){

                        displayasseen("peer");

                        });
<input type=button id=peerseen data-dojo-type="dijit/form/Button" data-dojo-props="label:'Peer'" />

Using post to get json data

[
Example of using post to get data
]

on(dom.byId('processpsybutton'),'click',function(){
                                   request.post("http://localhost/yii/g3labs/?r=Group/processowner").then(
                                               function(data){
                                                ownerpsyrefresh(data);
                                               },

                                                function(err){
                                               }

                                    );

Example of getting a json data and creating new html elements.


(note the difference between updating value of field and html)
[
Example shows, passing variable to request.get() in url, by adding other variable.
domConstruct.empty(dom.byId('profileasseen'));- will clear the dom
domConstruct.create('p',..) – create new dom element of type <p>
domStyle.set(nd, ..) – sets style for the dom element created above
domConstruct.place(nd,..) – places dom element in place identified.
]
function displayasseen(other)
{      
var uri = "http://localhost/yii/g3labs/?r=Group/getownerdetailsforothers&other="+other;
            request.get(uri).then(                                

                                                function(data){
                                                var   qaobj = JSON.parse(data);                      
                                    domConstruct.empty(dom.byId('profileasseen'));

                                      var paras = eval(qaobj.paragraphs);
                                    for(var i=0;i<paras.length;i++){

                                    var p=paras[i];

                                    var paragraphtags = p.paragraphtags;

                                    var paragraphpsy = p.paragraphpsy;

                                    var paragraphtext = p.paragraphtext;
           var nd=domConstruct.create('p',{innerHTML:'[Tags:'+paragraphtags.join()+']' });

                                    domStyle.set(nd,{width:700,  color:'red',margin:'1px' });

                                    domConstruct.place(nd,dom.byId('profileasseen'),'last');     

            var nd=domConstruct.create('p',{innerHTML:'[Psychology choice:'+paragraphpsy.join()+']' });

                                    domStyle.set(nd,{width:700,  color:'red',margin:'1px' });

                                    domConstruct.place(nd,dom.byId('profileasseen'),'last');     

                                   

            var nd=domConstruct.create('p',{innerHTML:paragraphtext });

                                    domStyle.set(nd,{width:700,background:'gray',margin:'1px' });

                                    domConstruct.place(nd,dom.byId('profileasseen'),'last');     

                       

            }         

            });

            }

Introduction

I have used Dojo for frontend development. Used Json data to communicate with PHP in backend using Yii framework.

  1. Example of receiving data in json format, and updating a text field
  2. Example of getting a json data and creating new html elements
  3. Trigger when a button is clicked.
  4. Using post to get json data
  5. Using post to submit a form
  6. Updating on change in dropdownlist
Example of receiving data in json format, and updating a text field
 
 


function visitorpsyrefresh(){
[
Assumes you are getting data from webservice link for a user “Raju”

http://localhost/yii/g3labs/?r=Group/getvisitorpsydata&visitorname=Raju



var n1 = registry.byId("visitorname").get("value");
var uri="http://localhost/yii/g3labs/?r=Group/getvisitorpsydata&visitorname="+n1;
request.get(uri).then(

]
[
Assume the json data format is
{"visitorname":"Guru",
"values":[
{
"name": " Adventurousness ",
"value": 80
},
{
"name": "Openness",
"value": 90
}
]
}
]