Showing posts with label Navya. Show all posts
Showing posts with label Navya. Show all posts

Monday, 10 April 2017

calculator database using php

Hi all this post explains implementation of calculator database in php
you are required to write the following set of instructions here.


<TITLE>PHP Calculator v1</TITLE>
<form name="calc" action="?page=calc" method="POST"> <!--implementing pagination with the page name appended to url -->
Number 1: <input type=text name=value1><br>
Number 2: <input type=text name=value2><br>
Operation: <input type="submit" name=oper value="add"/> <input type="submit" name=oper value="subtract"/>
<input type="submit" name=oper value="divide"/> <input type="submit" name=oper value="multiply"/><br>
</form>

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST')//checking if the form method is post
{
$page = $_GET['page'];  
// Defining the "calc" class
class calc {
var $number1;  //variable declaration
var $number2;  
function add($number1,$number2)  {
$con=db();       //creating connection variable            
$result =$number1 + $number2;
echo("The sum of $number1 and $number2 is $result<br><br>");
echo("$number1 + $number2 = $result");
mysqli_query($con,"insert into plus values('$number1 + $number2',$result)");
exit;            
}  
function subtract($number1,$number2)  {
$con=db();                  
$result =$number1 - $number2;
echo("The difference of $number1 and $number2 is $result<br><br>");
echo("$number1 &#045 $number2 = $result");
mysqli_query($con,"insert into plus values('$number1 - $number2',$result)");
exit;          
 }  
function divide($number1,$number2)  {
$con=db();                  
$result =$number1 / $number2;
echo("$number1 divided by $number2 is $result<br><br>");
echo("$number1 &divide $number2 = $result");
mysqli_query($con,"insert into plus values('$number1 / $number2',$result)");
exit;          
 }  
function multiply($number1,$number2)  {
$con=db();                  
$result =$number1 * $number2;
echo("The product of $number1 and $number2 is $result<br><br>");
echo("$number1 x $number2 = $result");
mysqli_query($con,"insert into plus values('$number1 * $number2',$result)");
exit;            
}
}
$calc = new calc();
 ?>

<?php
if($page == "calc"){
$n1 = $_POST['value1'];
$n2 = $_POST['value2'];
$number1=abs(ltrim($n1,''));
$number2=abs(ltrim($n2,''));
$oper = $_POST['oper'];
function db () {
static $con;
if ($con===NULL){        
$con = mysqli_connect ("localhost", "root", "", "plus");
     }
return $con;
 }
if(!$number1){
echo("You must enter number 1!");
exit;    
 }
if(!$number2){
echo("You must enter number 2!");
exit;      
}
if(!$oper){
echo("You must select an operation to do with the numbers!");
exit;      
}
if(!preg_match("/[0-9]/", $number1)){
echo("Number 1 MUST be numbers!");
exit;      
}
if(!preg_match("/[0-9]/", $number2)){
echo("Number 2 MUST be numbers!");
exit;      
}
if($oper == "add"){          
$con=db();
$query = mysqli_query($con,"SELECT * FROM plus WHERE exp='$number1 + $number2'");
if (!$query) {
        echo 'MySQL Error: ' . mysqli_error();
        exit;
    }
$numrows = mysqli_num_rows($query);
if ($numrows == 0) {
$calc->add($number1,$number2);
      }

}
if($oper == "subtract"){
$con=db();
$query = mysqli_query($con,"SELECT * FROM plus WHERE exp='$number1 - $number2'");
$numrows = mysqli_num_rows($query);
if ($numrows == 0) {        
$calc->subtract($number1,$number2);
      } }
if($oper == "divide"){
$con=db();
$query = mysqli_query($con,"SELECT * FROM plus WHERE exp='$number1 / $number2'");
$numrows = mysqli_num_rows($query);
if ($numrows == 0) {  
          $calc->divide($number1,$number2);    
 }
}
 if($oper == "multiply"){
$con=db();
$query = mysqli_query($con,"SELECT * FROM plus WHERE exp='$number1 * $number2'");
$numrows = mysqli_num_rows($query);
if ($numrows == 0) {  
          $calc->multiply($number1,$number2);       } }
while ($row = mysqli_fetch_assoc($query)) {
print("Accessed from database.......<br>");
print($row['exp'] . " = " . $row['res']);
 }    
}
}
?>











Tuesday, 4 April 2017

Authenticating User Details from xml file using Java Server Pages

Our main aim is to authenticate user details from xml file.Now this xml file can be normal one or u can take web.xml for implementation also.

I will explain u both the ways one after the other

1.Normal XML file using Document Builder

The folder structure for this jsp program is as follows:

This is the xml file which we have to use:



Below you can find the index.html file which accepts username and password:


This is the jsp file xret.jsp where the actual logic is specified:



These are the output screens cross check with xml file:










2)Using InitParam in web.xml





The output for this is as follows:








Sunday, 26 March 2017

Java Script programs on displaying country's capital

Write an HTML page that contains a selection box with a list of 5 countries. When the user selects a country, its capital should be printed next to the list. Add CSS to customize the properties of the font of the capital (color, bold and font size).  


As we all know Javascript can change HTML content.One of javascript HTML method is getElementById()
This example uses the method to "find" an HTML element (with id="txtSelectedCapital") and changes the element content (innerHTML) to "selectedValue":




dropDown.options[dropDown.selectedIndex].value;
This line allows us to select the drop down list value based on the index and store it in some variable for displaying in the browser.

Sunday, 19 March 2017

USING PREPARED STATEMENT IN JSP

The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized query.
Improves performance: The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once.

Following is the example implementing the same interface in jsp:

In this jsp execution you are not required to explicitly keep mysqlconnector.jar as it should be present in tomcats lib folder like below:








Create emp_table in mysql as follows:

 This is the output for your jsp:






Friday, 17 March 2017

JSP AGE VALIDATION ALONG WITH CSS


A web application that takes name and age from an HTML page. If the age is less than 18 it should send a page with “Hello <name>, you are not authorized to visit the site “message, where <name> should be replaced with the entered name. Otherwise it should send “Welcome <name> to this site “message.

Below is the folder structure that is to be followed for every tomcat application


The html form nameage consist of the link to stylesheet along with the form details:


This is the jsp code for processing age criteria:

Once you start your application in tomcat automatically the corresponding class and java files are generated in the below folder:


Following are the output screens where in based on the age criteria the user will be prompted with the respective messages: