Tuesday, July 29, 2014

Decimal to binary conversion program in php

the following code will do the conversion of decimal to binary.....no error has been occured

<html>
<body>
<form method="POST" action="deci.php">
<input type="text" name="t1" placeholder="enter a value here">
<br>
<br>
<input type="submit" value="find" name="submit">
</form>
<?php
$sum="";
if(isset($_POST['submit']))
{
$val=$_POST['t1'];
$n=$val;
while($n!=0)
{
 $rem=$n%2;
 $sum.=$rem;
 $quot=$n/2;
 $n=floor($quot);
 
}


echo strrev($sum);

}
?>
</body>
</html>

Finding factorial values in php

<html>
<body>
<form name="hi" method="POST" action="factorial.php">
<input type="text" name="t1">
<br>
<br>
<input type="submit" value="submit">
</form>
<?php
$tot=1;
$num=$_POST['t1'];
for($i=$num;$i>0;$i--)
{
$tot=$tot*$i;
echo"<br>";
}
echo $tot;
?>
</body>
</html>

above code works well to find factorial....no error will occur

Adding digits in php

here is the code to get value from a text box and adds every digits of a number and prints total value

<html>
<body>
<form method="POST" action="adding.php">
<input type="text" name="t1" placeholder="enter a number">
<br>
<input type="submit" name="submit" value="go">
</form>
<?php
if(isset($_POST['submit']))
{
$val=$_POST['t1'];
$n=$val;
while($n!=0)
{
 $rem=$n%10;
 $sum+=$rem;
 $quot=$n/10;
 $n=$quot;
        }
echo "the added  value is:".$sum;
}
?>
</body>
</html>

the above code will work with no errors

getting multiple values from text box without reloading and adding them in php

Yesterday i tried that the below code to execute the task
getting multiple values from text box without reloading and adding them in php...and i executed it....it would work definitely 

<html>
<body>
<form method="POST" action="multi.php">
<input type="text" id="t1" name="t1" placeholder="enter numbershere" onKeyup="return myFunc(this.value)">
<br>
<input type="hidden" id="hidden" name="hidden" >
<br>
<input type="submit" name="submit" value="submit" onClick="myFunc1()">
</form>
<script>
var i=0;
var arr=new array(5);
function myFunc(val){
                         
var x=document.getElementById("t1");
                         if(isNaN(x.value))
                         return false; //returns if value is a character//
                         else{
                         arr[i]=x.value;
i++;
x.value="";
                            return true;
                             }
}
function myFunc1()
{
var y=document.getElementById("hidden");
y.value=arr;
}
</script>
<?php
if(isset($_POST['submit']))
{
$val=$_POST['hidden'];
$sum=0;
$no1;
$no2;
$no3;
$no4;
$no5;
sscanf($val,"%d,%d,%d,%d,%d",$no1,$no2,$no3,$no4,$no5);
if($no1!="")
$sum+=$no1;
if($no2!="")
$sum+=$no2;
if($no3!="")
$sum+=$no3;
if($no4!="")
$sum+=$no4;
if($no5!="")
$sum+=$no5;
echo $sum;
}
?>

</body>
</html>

in above coding, i have used keyup() function to avoid multiple submit buttons or multiple times loading a whole page...if you guys have any idea...please share it...