Web Development - Page 2 of 8 -

How to create a table in MySQL using NodeJs

Now i will create a table in mysql (phpmyadmin) using nodejs code .First create write this code for details of db . I am using notepad++ you can use any code editor like sublime.

var mysql = require(‘mysql‘); Loading module .

Before executing this code you must download and install the mysql module.To access a mysql database with nodejs you need mysql driver.How to download mysql module: – Go to cmd and type npm install mysql. This line will download and install mysql on your local computer.

First load module mysql using the require function of node js. There are many modules in nodejs url , https, http ,realdline ,formidable etc and all the modules can be load using require function .

Now if you will check your db students table would have been created .

See our other tutorials

Nodejs file system

Nodejs file system allows us to work with your computer system. To work with nodejs file system you need to specifiy ‘fs’ in require() method:

Some use of file system :– To create a file , To delete a file , To update a file, To rename a file . Save this file on your folder app.js

If you run this code in your browser, It will generate this file in your folder newFile.php 

Nodejs first program

To run nodejs in your localhost you must have to download nodejs from nodejs site /https://nodejs.org/  . You can perform nodejs codes in notepad++. Now copy this code and paste in notepad++ & how to run this code I will tell you.This first application name is myfile.js

Now go to C Drive and open users folders and save this code inside your name folder (What is your computer user name):- C:\users\dheeraj\myfirst.js Now as you can see here dheeraj is my computer name.

Now open command prompt from the same folder where you have saved this file using shift + right click Or See this image

Nodejs run project

just type node myfile.js  & hit enter button this command will start your local server and go to browser type: http://localhost:8000 It will give this output.

Nodejs program

You can change port name from here }).listen(8000);  Always give free port which is not in use .

JavaScript array

Using array we can store multiple values in a single javascript variable. The array is a special variable which can store more than one value lets see a small example .

Now three variables for three values but if we have 100 car brands then we have to write 100 variables for 100 values.  So to avoid this situation we use an array. Now see the syntax of the array.

Array elements are numbered and starts with zero

If you run this code your output would be SUZUKI Why ?? 0 , 1 ,2 This is indexing DUKATI is at 0 , YAMAHA is at 1 & SUZUKI is at 2

Output :-

JavaScript Arrays


Now see more example of array indexing

Ouput of code

How to use array value in html

using document.getElementByID().innerHTML we can print array value in html form or paragraph tag or div tag .

It will print Bebbo Randy & Webmi  .So below is output

JavaScript Arrays in html

How to change array elements

Changing array elements is very simple . Lets see one example

Ronic will replace student Woler as you can see the index of Woler is 1. Output of code

How to change elemets of array

How to append or extract elemetns from array

push() is used to append or add elemetns to your array

Output this code

How to append elements in array()




How to use loop in Array

Let see example of for loop

Output of this code

Lopping in array

Now see more example

Output of code

Javascript DOM selectors

In this tutorial you will learn about DOM selectors .

How to select DOM elements in javascript

Javascript is used to get or modify the value of the html elements on website or page .We can apply some effect on webpage like animation , hide or show data . But , these all things happen with help of DOM selectors. If you don’t know how to target HTML element then you cant perform these tasks .

How to select elements

Selecting DOM elements is essential part of javascript . If you have to perform any animation like progress bar that time you would need to use DOM elements . Now we will see some examples .

document.body.style.background = “green”; using this code we can change bg color of body & document.head.firstElementChild.nodeName; this code is used to target <head> elements as you can see there are two elements in HEAD Tag one meta and another is title .

Select HTML elements by id

You can select html element by its id document.elementByID()  so let see one example of it

Output of code

Selecting html element by its id

I am javascript

I am jquery & i am the best library of javascript

document.getElementById(“para1”); if no matching element found null will be returned & the value of attribute id should be unique in the page.

How to select element by class

Using getElementsByClassName() , we can also use class name for select element

Output of code

Select element by class

I am javascript



I am nodejs

I am angularjs

To color both attributes i used for loop using loop we can color thousands of lines .

Javascript inneHTML method

Javascript can display output in four different ways

Using innerHTML()
Using consol.log()
Using document.write()
Using window.alert()

To access users element we use innerHTML() .The id and class attributes define the HTML elements.

document.getElementById(“numberMult”).innerHTML this line will do multiplication & send output <p id=”numberMult”></p>

So output will be 129

Now lets see another example of innerHTML

You can concatenate javascript and html tags using + sign. Here i have concatenated html font tag with javascript variable with help of + sign .

Output of code

Javascript innerHTML Example

Lets see form example using innerHTML

Output of Code



We have called innerHTML inside a function and function called in onclick event of button.

See more tutorials web development

Javascript JSON parsing

Json is a text-based format like xml . JSON is easy to read and easy to understand for humans and computers but Json occupy less bandwith compare to xml . JSON is the most popular and lightweight data-interchange format for website applications. Below is json object.

We have seen Json object , Now below is Json array inside Json object

 

How to parse Json data in javascript

Lets suppose we have below data which we received from a server

Now we will use JSON.parse() method to convert this data into javascript object and access its value using dot symbol (.) See the below example

Output of code

Dheeraj
22222222
Jaipur

PHP Json data parsing

Json is a text-based format like xml . JSON is easy to read and easy to understand for humans and computers but Json occupy less bandwith compare to xml . JSON is the most popular and lightweight data-interchange format for website applications. Below is json object

JSON data structures are very similar to PHP arrays. Encoding and decoding of json data in php is very easy .PHP uses these two functions json_encode() and json_decode() for encoding and decoding of json data . Both functions only works with UTF-8 .

We have seen Json object , Now below is Json array inside Json object

How to encode Json data in php : – json_encode() function is uses to encode data to json format . The below example explanes how to use json_encode() in php for associative array .

Output of this code

{“Peter”:65,”Harry”:80,”John”:78,”Clark”:90}

How to decode Json data in php :-

Decoding json value is as simple as encoding .Now here we will use php json_decode() function convert the json encoded string to php data types

Output of code

9002020
23030030
78020292

See more tutorials on json & php

How to use chaining in Jquery

Jquery chaining allows us to run multiple methods togther .Simple chaining example :-

Now as you can see there is three methods css() , slideUp() & slideDown() and we can bind them together with help of Jquery chaining . You can also write this way . You can download Jquery from here www.jquery.com

Now i will show you complete example of chaining

Now run this code on your broswer Output :-

Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum has been the industrys standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book It has survived not only five centuries

See more examples of Jquery  

How to use json data using php

Json is javascript object notation . It used for data exchange on server . Json is light weight data exchange format which is useful for server & website communication .

Take a real life example Suppose Website A in java and Website B is in PHP but if they want to communicate with each other or wants to exchange data with each other then they will use Json or xml .

Json is very easy to use & language independent .Json is used to storing and transport data on server . Json is text format so it can be transported anywhere

XML is difficult to parse than JSON.
JSON is parsed into a ready-to-use JavaScript object. JSON can use array for multiple data

Different Json data types

a string , a number , boolean , an array , an object (JSON object)

this is json object and we can convert it to javascript object using JSON.parse();  We write json data or object withing curly braces { } .Json data must be in key and value format , both key and value separated by single colon .

A common use of Javascript object notation is to exchange data to/from a web server.

When receiving data from a server, the data is always a string form.

we parse the data using JSON.parse()&then data becomes javascript object

Now using php code we can convert to json

Output of code

{“name”:”dheeraj”,”age”:”37″,”state”:”haryan”} This is output is in JSON format & we got this output by using json_encode(); function of php

Lets see another example of json ( How to use loop in json)

Output of code

Using loop in json

Employee Job
Email
Cell Number

Now how to access its property through loop

Output of code :

Using loop in json

comupting
[email protected]
211882

How array works in json

Output of code

Chemistry

data = jObj.subjects[2]; I gave index 2 so output is chemistry